NoSQL 입문
Jake Roach
Data Engineer
# Import redis, make a connection
r = redis.Redis(...)
# Store a key-value pair
r.set("username", "JDoe")
# Store another key-value pair
r.set("age", 27)
# Overwrite an existing key
r.set("username", "BSmith")
Redis 서버에 연결한 후:
.set()에 키와 값을 전달합니다int, float도 전달 가능하며 str로 저장됩니다키-값 쌍 조회
# Store a key-value pair
r.set("username", "JDoe")
# Retrive the key-value pair
username = r.get("username")
# Print the result
print(username)
JDoe
키-값 쌍 덮어쓰기
r.set("username", "BSmith")
username = r.get("username")
print(username)
BSmith
없는 키에 접근 시도
favorite_color = r.get("favorite_color")
print(favorite_color)
None
# Store a dictionary using .hset()
r.hset(
"shopping_cart",
mapping={
"item_id": "1003",
"quantity": 2,
"price": 79.99
}
)
# Retrieve the dictionary
r.hgetall("shopping_cart")
dict와 같은 더 복잡한 자료형도 저장할 수 있습니다:
.hset()은 키와 dict를 받습니다.hgetall()에 키를 전달합니다$$
{
'item_id': '1003',
'quantity': '2',
'price': '79.99'
}
NoSQL 입문