การจัดเก็บ Python dictionary ด้วย Redis
นอกจากการจัดเก็บ key-value pair แบบธรรมดาแล้ว ยังสามารถจัดเก็บข้อมูลประเภทที่ซับซ้อนกว่านั้นได้ เช่น dictionary ในแบบฝึกหัดสุดท้ายกับ Redis นี้ จะได้ฝึกจัดเก็บและดึงข้อมูล Python dictionary โดยใช้โมดูล redis ใน Python
เช่นเดิม ออบเจกต์การเชื่อมต่อถูกสร้างไว้แล้วและจัดเก็บในตัวแปร redis_conn ขอให้โชคดี!
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
NoSQL เบื้องต้น
คำแนะนำการฝึกหัด
- ใช้ตัวแปร
redis_connจัดเก็บ dictionarylondon_weather_mappingไปยัง key"london_weather" - ดึงข้อมูลและแสดงผล key-value pair
"london_weather"โดยใช้ตัวแปรredis_connและเมธอดที่เหมาะสม
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create a dictionary containing weather data
london_weather_mapping = {
"temperature": 42,
"humidity": 88,
"visibility": "low"
}
# Store the london_weather key-value pair
____.____(
"____",
mapping=____
)
# Retrieve and print the london_weather key-value pair
print(____.____("____"))