Redis 사용하기

2014. 11. 10. 18:55BigDATA

반응형



Redis 자바 연동

  ㄴ jedis라는 라이브러리가 있음. 


Maven을 사용한다면! 참고


 

Jedis api : http://javadox.com/redis.clients/jedis/2.6.0/redis/clients/jedis/Jedis.html


몇몇을 살펴보자! ^-^

select

public String select(int index)
Select the DB with having the specified zero-based numeric index. For default every new client connection is automatically selected to DB 0.

Specified by:
select in interface BasicCommands
Parameters:
index -
Returns:
Status code reply

randomKey

public String randomKey()
Return a randomly selected key from the currently selected DB.

Time complexity: O(1)

Specified by:
randomKey in interface MultiKeyCommands
Returns:
Singe line reply, specifically the randomly selected key or an empty string is the database is empty

lrange

public List<String> lrange(String key,                           long start,                           long end)
Return the specified elements of the list stored at the specified key. Start and end are zero-based indexes. 0 is the first element of the list (the list head), 1 the next element and so on.

For example LRANGE foobar 0 2 will return the first three elements of the list.

start and end can also be negative numbers indicating offsets from the end of the list. For example -1 is the last element of the list, -2 the penultimate element and so on.

Consistency with range functions in various programming languages

Note that if you have a list of numbers from 0 to 100, LRANGE 0 10 will return 11 elements, that is, rightmost item is included. This may or may not be consistent with behavior of range-related functions in your programming language of choice (think Ruby's Range.new, Array#slice or Python's range() function).

LRANGE behavior is consistent with one of Tcl.

Out-of-range indexes

Indexes out of range will not produce an error: if start is over the end of the list, or start > end, an empty list is returned. If end is over the end of the list Redis will threat it just like the last element of the list.

Time complexity: O(start+n) (with n being the length of the range and start being the start offset)

Specified by:
lrange in interface JedisCommands
Parameters:
key -
start -
end -
Returns:
Multi bulk reply, specifically a list of elements in the specified range.

hget

public String hget(String key,                   String field)
If key holds a hash, retrieve the value associated to the specified field.

If the field is not found or the key does not exist, a special 'nil' value is returned.

Time complexity: O(1)

Specified by:
hget in interface JedisCommands
Parameters:
key -
field -
Returns:
Bulk reply

hkeys

public Set<String> hkeys(String key)
Return all the fields in a hash.

Time complexity: O(N), where N is the total number of entries

Specified by:
hkeys in interface JedisCommands
Parameters:
key -
Returns:
All the fields names contained into a hash.


위의 명령어들은 실제 Server로 가서 redis-cli를 통해서 수행할 수 있다.
select 0 등으로  DB를 선택하여 주고 
randomkey를 통해 랜덤하게 키를 뽑아낼 수 있다.
또한 hget이나 lrange를 통해서 값을 뽑아낼수 있다.

 - 끝~ - 


반응형

'BigDATA' 카테고리의 다른 글

층화추출법  (0) 2014.11.26