키-값 데이터 저장 및 조회

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로 저장됩니다
  • 기존 키-값 쌍을 덮어쓸 수 있습니다
NoSQL 입문

키-값 데이터 조회

키-값 쌍 조회

# 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
NoSQL 입문

복합 키-값 데이터 저장

# 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 입문

연습해 봅시다!

NoSQL 입문

Preparing Video For Download...