Databases Reference
In-Depth Information
QUERYING REDIS DATA STORES
As in the case of MongoDB and HBase you have had a chance to interact with Redis in the earlier
chapters. In the past few chapters you learned the essentials of data storage and access with
Redis. In this section, the subject of querying data is explored a bit further. In line with the other
illustrations in this chapter so far, a sample data set is fi rst loaded into a Redis instance.
For the purpose of demonstration, the NYC Data Mine public raw data on parking spaces available
online at www.nyc.gov/data is used. The data download is available in a comma-separated text fi le
format. The download fi le is named parking_facilities.csv . See Listing 6-2 for a simple Python
program that parses this CSV data set and loads it into a local Redis store. Remember to start your
local Redis server before you run the Python script to load up the data. Running the Redis-server
program, available in the Redis installation directory, starts a Redis server instance, which by
default listens for client connections on port 6379.
LISTING 6-2: Python program to extract NYC parking facilities data
Available for
download on
Wrox.com
import csv
import redis
f = open(“parking_facilities.csv”, “r”)
parking_facilities = csv.DictReader(f, delimiter=',')
r = redis.Redis(host='localhost', port=6379, db=0)
def add_parking_facility(license_number,
facility_type,
entity_name,
camis_trade_name,
address_bldg,
address_street_name,
address_location,
address_city,
address_state,
address_zip_code,
telephone_number,
number_of_spaces):
if r.sadd(“parking_facilities_set”, license_number):
r.hset(“parking_facility:%s” % license_number, “facility_type”,
facility_type)
r.hset(“parking_facility:%s” % license_number, “entity_name”,
entity_name)
r.hset(“parking_facility:%s” % license_number, “camis_trade_name”,
camis_trade_name)
r.hset(“parking_facility:%s” % license_number, “address_bldg”,
address_bldg)
r.hset(“parking_facility:%s” % license_number, “address_street_name”,
address_street_name)
r.hset(“parking_facility:%s” % license_number, “address_location”,
address_location)
r.hset(“parking_facility:%s” % license_number, “address_city”,
continues
Search WWH ::




Custom Search