System environment: OS: OracleLinuxEnterprise5.6redis: redis-2.6.8python: Python-2.7.3redis python package version: redis-2.7.2.tar prerequisites:... system environment:
OS: Oracle Linux Enterprise 5.6
Redis: redis-2.6.8
Python: Python-2.7.3
Redis python package version: redis-2.7.2.tar
Prerequisites:
1. make sure that Redis is successfully installed and configured correctly. For more information, see
2. make sure that the Python environment is successfully configured. refer to the documentation.
Configure python to connect to redis:
1. install the Python package of Redis:
Use easy-install for installation. for the configuration of easy-install, refer to the above Python environment.
[Root @ njdyw bin] # easy_install2.7.3 redis
Searching for redis
Reading http://pypi.python.org/simple/redis/
Reading http://github.com/andymccurdy/redis-py
Best match: redis 2.7.2
Downloading http://pypi.python.org/packages/source/r/redis/redis-2.7.2.tar.gz#md5=17ac60dcf13eb33f82cc25974ab17157
Processing redis-2.7.2.tar.gz
Running redis-2.7.2/setup. py-q bdist_egg -- dist-dir/tmp/easy_install-8FAlft/egg-dist-tmp-JzQViJ
Zip_safe flag not set; analyzing archive contents...
Adding redis 2.7.2 to easy-install.pth file
Installed/usr/local/python2.7.3/lib/python2.7/site-packages/redis-2.7.2-py2.7.egg
Processing dependencies for redis
Finished processing dependencies for redis
-- Install the Parser package (optional)
Note: Parser can control how to parse the redis response content. Redis-py contains two Parser classes: PythonParser and HiredisParser. By default, if the hiredis module is installed, redis-py will use HiredisParser; otherwise, PythonParser will be used.
HiredisParser is written in C and maintained by the redis core team. its performance is more than 10 times higher than that of PythonParser. Therefore, it is recommended to use HiredisParser. Installation method, use easy_install:
[Root @ njdyw ~] # Easy_install2.7.3 hiredis
Searching for hiredis
Reading http://pypi.python.org/simple/hiredis/
Reading https://github.com/pietern/hiredis-py
Best match: hiredis 0.1.1
Downloading http://pypi.python.org/packages/source/h/hiredis/hiredis-0.1.1.tar.gz#md5=92128474f6fb027cfb8587fce724ea8e
Processing hiredis-0.1.1.tar.gz
Running hiredis-0.1.1/setup. py-q bdist_egg -- dist-dir/tmp/easy_install-ZanSCB/hiredis-0.1.1/egg-dist-tmp-XCZBQ0
Zip_safe flag not set; analyzing archive contents...
Adding hiredis 0.1.1 to easy-install.pth file
Installed/usr/local/python2.7.3/lib/python2.7/site-packages/hiredis-0.1.1-py2.7-linux-x86_64.egg
Processing dependencies for hiredis
Finished processing dependencies for hiredis
2. check whether the installation is successful.
-- The extension package installed with easy-install is in the site-packages directory of python by default.
[Root @ njdyw ~] # Whereis python2.7.3
Python2.7:/bin/python2.7.3/usr/local/python2.7.3
[Root @ njdyw ~] # Cd/usr/local/python2.7.3/lib/python2.7/site-packages/
[Root @ njdyw site-packages] # ll
Total 408
-Rw-r -- 1 root 239 03-21 easy-install.pth
-Rw-r -- 1 root 119 03-21 10:07 README
-Rw-r -- 1 root 60401 03-21 10: 45redis-2.7.2-py2.7.egg
-Rw-r -- 1 root 332125 03-21 setuptools-0.6c11-py2.7.egg
-Rw-r -- 1 root 30 03-21 10:12 setuptools. pth
You can see that the redis-2.7.2-py2.7.egg package has been successfully installed
3. test the connection
[Root @ njdyw site-packages] # python2.7.3
Python 2.7.3 (default, Mar 21 2013, 10:06:48)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Import redis
>>> RedisClient = redis. StrictRedis (host = '2017. 0.0.1 ', port = 127, db = 0)
>>> RedisClient. set ('test _ redis', 'Hello Python ')
True
>>> Value = redisClient. get ('test _ redis ')
>>> Print value
Hello Python
>>> RedisClient. delete ('test _ redis ')
True
>>> Value = redisClient. get ('test _ redis ')
>>> Print value
None
>>> Dir (redis)
['Authenticationerror', 'connection', 'connectionerror', 'connectionpool ', 'dataerror', 'invalidresponse', 'pubsubererror', 'rediserror ', 'responseerror', 'strictredis ', 'unixdomainsocketconnection', 'version', 'watcherror', '_ all _', '_ builtins __', '_ doc _', '_ file _', '_ loader _', '_ name _', '_ package __', '_ path _', '_ version _', '_ compat', 'client', 'connection', 'exception', 'from _ url ', 'utils']
>>> RedisClient = redis. StrictRedis (host = '2017. 0.0.1 ', port = 127, db = 0)
>>> Dir (redisClient)
['Response _ callbacks', '_ class _', '_ ins _', '_ delattr _', '_ delitem __', '_ dict _', '_ doc _', '_ format _', '_ getattribute _', '_ getitem __', '_ hash _', '_ init _', '_ module _', '_ new _', '_ reduce __', '_ performance_ex _', '_ repr _', '_ setattr _', '_ setitem _', '_ sizeof __', '_ str _', '_ subclasshook _', '_ weakref _', '_ zaggregate', 'append', 'bgrewriteaof ', 'bgsave', 'bitcount', 'bitop', 'blpop', 'brpop', 'brplpush', 'Client _ kill', 'Client _ list ', 'config _ get', 'config _ set', 'connection _ pool ', 'dbsize', 'debug _ object', 'desc', 'delete', 'echo ', 'eval', 'evalsha', 'execute _ command', 'exists', 'expire ', 'expireat', 'flushall', 'flushdb', 'from _ url ', 'GET', 'getbit ', 'getrange', 'getset', 'hdel', 'hexists', 'hget', 'hgetall', 'hincrby', 'hincrbyfloat ', 'hkeys', 'hlen', 'hmet ', 'hmset', 'hset', 'hsetnx', 'hvals', 'INC', 'encrbyfloat', 'info ', 'keys ', 'lastsave', 'lindex', 'linsert ', 'llen', 'lock', 'lpop', 'lptu', 'lpushhx', 'lrange ', 'lrem ', 'lset', 'ltrim', 'mget', 'Move ', 'mset', 'msetnx', 'object', 'parse _ response ', 'persist', 'pexpire ', 'pexpireat', 'ping', 'pipeline', 'pttl', 'publish ', 'pubsub', 'randkey ', 'register _ script', 'rename', 'renamenx', 'response _ callbacks', 'rpop ', 'rpoplpush', 'rptu', 'rpushx', 'sadd ', 'Save', 'scard ', 'script _ exists', 'script _ flush', 'script _ kill ', 'script _ load', 'sdid', 'sdiffstore ', 'set', 'set _ response_callback ', 'setbit', 'setex ', 'setnx', 'setrange', 'shutdown ', 'sinter', 'sinterstore ', 'sismember', 'slaveof ', 'smembers', 'smove', 'sort ', 'spop', 'srandmember', 'srem', 'strlen', 'substr ', 'sunion ', 'sunionstore', 'time', 'transaction', 'ttl', 'type', 'unwatch ', 'Watch', 'zadd', 'zcard ', 'zcount', 'zencrby', 'zinterstore', 'zrange', 'zrangebyscore ', 'zrank', 'zrem ', 'zremrangebyrank', 'zremrangebyscore ', 'zrevrange ', 'zrevrangebyscore ', 'zrevrank', 'zscore ', 'zunionstore']
>>>
4. test instance:
(1). import text data to redis
-- Imported data format
[Root @ njdyw ~] # More data.txt
Wolys # wolysopen111 # wolys@21cn.com
Coralshanshan #601601601 # zss1984@126.com
Pengfeihuchao # woaidami # 294522652@qq.com
Simulategirl # @ #$9608125 # simulateboy@163.com
Daisypp #12345678 # zhoushigang_123@163.com
Sirenxing424 # tfiloveyou # sirenxing424@126.com
Raininglxy #1901061139 # lixinyu23@qq.com
Leochenlei # leichenlei # chenlei1201@gmail.com
Z370433835 # lkp145566 # 370433835@qq.com
-- Create a command script
[Root @ njdyw ~] # Cat imp_red.py
Import redis
Import re
Pool = redis. ConnectionPool (host = '2017. 0.0.1 ', port = 127)
R = redis. Redis (connection_pool = pool)
Pipe = r. pipeline ()
P = re. compile (r' (. *) \ s # \ s (. *) \ s # \ s (.*)');
Pipe = r. pipeline ()
F = open ("data.txt ")
Matchs = p. findall (f. read ())
For user in matchs:
Key = 'users _ % s' % user [0]. strip ()
Pipe. hset (key, 'pwd', user [1]. strip (). hset (key, 'Email ', user [2]. strip ())
Pipe.exe cute ()
F. close ()
Note: strictly control spaces in python scripts
-- Execute the script
[Root @ njdyw ~] # Python2.7.3 imp_red.py
-- View imported data
[Root @ njdyw ~] # Redis-cli
Redis 127.0.0.1: 6379> keys *
1) "users_xiaochuan2018"
2) "users_coralshanshan"
3) "users_xiazai200901"
4) "users_daisypp"
5) "users_boiny"
6) "users_raininglxy"
7) "users_fennal"
8) "users_abc654468252"
9) "users_babylovebooks"
10) "users_xl200811"
11) "users_baby19881018"
12) "users_darksoul0929"
13) "users_pengcfwxh"
14) "users_alex126126"
15) "users_jiongjiongmao"
16) "users_sirenxing424"
17) "users_mengjie007"
18) "users_cxx0409"
19) "users_candly8509"
20) "users_licaijun007"
21) "users_ai3Min2"
22) "users_bokil"
23) "users_z370433835"
24) "users_yiling1007"
25) "users_simulategirl"
26) "users_fxh852"
27) "users_baoautumn"
28) "users_huangdaqiao"
29) "users_q1718334567"
30) "users_xldq_l"
31) "users_beibeilong012"
32) "users_hudaoyin"
33) "users_yoyomika"
34) "users_jacksbalu"
35) "users_wolys"
36) "users_kangte1"
37) "users_demonhaodh"
38) "users_ysdz8"
39) "users_leochenlei"
40) "users_llx6888"
41) "users_pengfeihuchao"
Redis 127.0.0.1: 6379>
Redis 127.0.0.1: 6379> hget users_pengfeihuchao email
294522652@qq.com"
Redis 127.0.0.1: 6379> hget users_llx6888 email
Linlixian200606@126.com"