Java Code Examples for redis.clients.jedis.Jedis#hkeys()

The following examples show how to use redis.clients.jedis.Jedis#hkeys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisClient.java    From apollo with GNU General Public License v2.0 6 votes vote down vote up
public Set<String> hKeys(String key) throws Exception {
    Jedis jedis = null;
    try {
        jedis = this.jedisPool.getResource();
        Set<byte[]> hkeys = jedis.hkeys(SafeEncoder.encode(key));
        logger.info("hkeys key:" + key);
        if (CollectionUtils.isEmpty(hkeys)) {
            return new HashSet<String>(1);
        } else {
            Set<String> keys = new HashSet<String>(hkeys.size());
            for (byte[] bb : hkeys) {
                keys.add(SafeEncoder.encode(bb));
            }
            return keys;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        this.jedisPool.returnBrokenResource(jedis);
        throw e;
    } finally {
        if (jedis != null) {
            this.jedisPool.returnResource(jedis);
        }
    }
}
 
Example 2
Source File: RedisJobStore.java    From redis-quartz with MIT License 6 votes vote down vote up
/**
 * Retrieves job from redis.
 *
 * @param jobKey the job key
 * @param jedis thread-safe redis connection
 * @return the job detail
 * @throws JobPersistenceException
 */
@SuppressWarnings("unchecked")
private JobDetail retrieveJob(JobKey jobKey, Jedis jedis) throws JobPersistenceException, ClassNotFoundException {
	String jobHashkey = createJobHashKey(jobKey.getGroup(), jobKey.getName());
	String jobDataMapHashKey = createJobDataMapHashKey(jobKey.getGroup(), jobKey.getName());
     if (!jedis.exists(jobHashkey)) {
        log.warn("job: " + jobHashkey + " does not exist");
        return null;
     }
	Class<Job> jobClass = (Class<Job>) loadHelper.getClassLoader().loadClass(jedis.hget(jobHashkey, JOB_CLASS));
	JobBuilder jobBuilder = JobBuilder.newJob(jobClass)
								.withIdentity(jobKey)
								.withDescription(jedis.hget(jobHashkey, DESCRIPTION))
								.storeDurably(Boolean.getBoolean(jedis.hget(jobHashkey, IS_DURABLE)));
							
	Set<String> jobDataMapFields = jedis.hkeys(jobDataMapHashKey);
	if (!jobDataMapFields.isEmpty()) {
		for (String jobDataMapField : jobDataMapFields)
			jobBuilder.usingJobData(jobDataMapField, jedis.hget(jobDataMapHashKey, jobDataMapField));							
	}
	
	return jobBuilder.build();		
}
 
Example 3
Source File: JedisScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> notExisted(String queueID, Set<String> resourceItemKeys) {
    if (!lockQueue(queueID)) {
        return Collections.emptySet();
    }
    Jedis jedis = jedisPool.getResource();
    try {
        final Set<String> hkeys = jedis.hkeys(makeDataKey(queueID));
        return Sets.filter(resourceItemKeys, new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return !hkeys.contains(input);
            }
        });
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 4
Source File: JedisScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public void addBatch(String queueID, Set<ResourceItem> resourceItems) {
    if (!lockQueue(queueID)) {
        return;
    }
    final Jedis jedis = jedisPool.getResource();
    final String dataKey = makeDataKey(queueID);
    try {
        final Set<String> hkeys = jedis.hkeys(dataKey);
        Set<ResourceItem> filterSet = Sets.filter(resourceItems, new Predicate<ResourceItem>() {
            @Override
            public boolean apply(ResourceItem input) {
                return !hkeys.contains(input.getKey());
            }
        });

        String poolQueueKey = makePoolQueueKey(queueID);
        for (ResourceItem resourceItem : filterSet) {
            jedis.hset(dataKey, resourceItem.getKey(), JSONObject.toJSONString(resourceItem));
            jedis.rpush(poolQueueKey, resourceItem.getKey());
        }
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 5
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> notExisted(String queueID, Set<String> resourceItemKeys) {
    if (!lockQueue(queueID)) {
        return Collections.emptySet();
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    try {
        final Set<String> existedSet = jedis.hkeys(makeDataKey(queueID));
        return Sets.filter(resourceItemKeys, new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return !existedSet.contains(input);
            }
        });
    } finally {
        unLockQueue(queueID);
    }
}
 
Example 6
Source File: JedisForbiddenQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> notExisted(String queueID, Set<String> resourceItemKeys) {
    if (!lockQueue(queueID)) {
        return Collections.emptySet();
    }
    Jedis jedis = jedisPool.getResource();
    try {
        final Set<String> hkeys = jedis.hkeys(makeDataKey(queueID));
        return Sets.filter(resourceItemKeys, new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return !hkeys.contains(input);
            }
        });
    } finally {
        unLockQueue(queueID);
        IOUtils.closeQuietly(jedis);
    }
}
 
Example 7
Source File: JedisUtil.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * 取得hash的 keys
 *
 * @param key
 * @return
 */
public static Set<byte[]> hkeys(final String key) {
    Jedis jedis = null;
    Set<byte[]> result = null;
    try {
        jedis = getResource();
        result = jedis.hkeys(getBytesKey(key));
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 8
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 返回指定hash中的所有存储名字,类似Map中的keySet方法
 * 
 * @param String
 *            key
 * @return Set<String> 存储名称的集合
 * */
public Set<String> hkeys(String key) {
	// ShardedJedis sjedis = getShardedJedis();
	Jedis sjedis = getJedis();
	Set<String> set = sjedis.hkeys(key);
	returnJedis(sjedis);
	return set;
}
 
Example 9
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
@Override
public void addBatch(String queueID, Set<ResourceItem> resourceItems) {
    if (!lockQueue(queueID)) {
        return;
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    final String dataKey = makeDataKey(queueID);
    try {
        final Set<String> hkeys = jedis.hkeys(dataKey);
        Set<ResourceItem> filterSet = Sets.filter(resourceItems, new Predicate<ResourceItem>() {
            @Override
            public boolean apply(ResourceItem input) {
                return !hkeys.contains(input.getKey());
            }
        });
        List<String> sliceQueue = sliceQueue(queueID);
        Set<String> newSlices = Sets.newHashSet();
        long index = size(queueID) + 1;
        String tailSlice = null;
        for (ResourceItem resourceItem : filterSet) {
            jedis.hset(dataKey, resourceItem.getKey(), JSONObject.toJSONString(resourceItem));
            String sliceID = String.valueOf(blockID(index));
            if (sliceID.equals(tailSlice) || sliceQueue.contains(sliceID)) {
                sliceID = sliceQueue.get(sliceQueue.size() - 1);
                tailSlice = sliceID;
            } else if (!newSlices.contains(sliceID)) {
                jedis.rpush(makeSliceQueueKey(queueID), sliceID);
                newSlices.add(sliceID);
            }
            jedis.rpush(makePoolQueueKey(queueID, sliceID), resourceItem.getKey());
            index++;
        }
    } finally {
        unLockQueue(queueID);
    }
}
 
Example 10
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> hkeys(String key) {
    Jedis jedis = null;
    Set<String> res = null;
    try {
        jedis = pool.getResource();
        res = jedis.hkeys(key);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 11
Source File: RedisHKeys.java    From ECFileCache with Apache License 2.0 5 votes vote down vote up
@Override
protected int doRequest(Jedis jedis, String redisAddress) {
  Set<byte[]> fields = jedis.hkeys(key.getBytes());
  redisFields[index] = fields;

  if (CollectionUtils.isEmpty(fields)) {
    String verbose = String.format("get fields [%s] from [%s] is empty", key, redisAddress);
    LOGGER.debug(verbose);
    return 1;
  }

  return 0;
}
 
Example 12
Source File: JedisClientPool.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> hkeys(String key) {
    Jedis jedis = jedisPool.getResource();
    Set<String> result = jedis.hkeys(key);
    jedis.close();
    return result;
}
 
Example 13
Source File: RedisClient.java    From Mykit with Apache License 2.0 5 votes vote down vote up
public List<String> hkeys(String key) {
	Jedis client = jedisPool.getResource();
	try {
		List<String> fields = new ArrayList<String>();
		Set<String> set = client.hkeys(key);
		fields.addAll(set);
		return fields;
	} finally {
		// 向连接池“归还”资源
		jedisPool.returnResourceObject(client);
	}

}
 
Example 14
Source File: JedisUtil.java    From Project with Apache License 2.0 5 votes vote down vote up
/**
 * 返回指定hash中的所有存储名字,类似Map中的keySet方法
 * 
 * @param  key
 * @return Set<String> 存储名称的集合
 */
public Set<String> hkeys(String key) {
	Jedis sjedis = getJedis();
	Set<String> set = sjedis.hkeys(key);
	sjedis.close();
	return set;
}
 
Example 15
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key返回所有的field
 * </p>
 *
 * @param key
 * @return
 */
public Set<String> hkeys(String key) {
    Jedis jedis = null;
    Set<String> res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.hkeys(key);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 16
Source File: RedisAccessTest.java    From ECFileCache with Apache License 2.0 4 votes vote down vote up
@Test
public void testJedis() {

  String host = "localhost";
  int port = 6379;
  String key = "test_xxx";
  String keyNotExist = "test_not_exist";

  String field = "field_1";
  String value = "value_1";

  String fieldNotExist = "field_not_exist";

  JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  jedisPoolConfig.setMaxTotal(Config.getInstance().getJedisPoolMax());

  JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, Config.getInstance().getJedisSocketTimeoutMs(), Config.getInstance().getRedisPassword());
  Jedis jedis = jedisPool.getResource();

  jedis.hset(key.getBytes(), field.getBytes(), value.getBytes());

  // hgetAll
  Map<byte[], byte[]> result = jedis.hgetAll(key.getBytes());
  Assert.assertTrue(MapUtils.isNotEmpty(result));

  result = jedis.hgetAll(keyNotExist.getBytes());
  Assert.assertTrue(MapUtils.isEmpty(result));

  // hkeys
  Set<byte[]> redisKeys = jedis.hkeys(key.getBytes());
  Assert.assertTrue(CollectionUtils.isNotEmpty(redisKeys));

  redisKeys = jedis.hkeys(keyNotExist.getBytes());
  Assert.assertTrue(CollectionUtils.isEmpty(redisKeys));


  // hget
  byte[] redisValue = jedis.hget(key.getBytes(), field.getBytes());
  Assert.assertTrue(!ArrayUtils.isEmpty(redisValue));

  redisValue = jedis.hget(key.getBytes(), fieldNotExist.getBytes());
  Assert.assertTrue(ArrayUtils.isEmpty(redisValue));

  redisValue = jedis.hget(keyNotExist.getBytes(), field.getBytes());
  Assert.assertTrue(ArrayUtils.isEmpty(redisValue));

}
 
Example 17
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Set<String> hkeys0(Jedis j, String key) {
	return j.hkeys(key);
}