日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python操作Redis中的hash

發(fā)布時間:2023/12/20 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python操作Redis中的hash 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Redis 數(shù)據(jù)庫hash數(shù)據(jù)類型是一個string類型的key和value的映射表,適用于存儲對象。Redis 中每個 hash 可以存儲 232 - 1 鍵值對(40多億)。?
Python的redis模塊實現(xiàn)了Redis哈希(Hash)命令行操作的幾乎全部命令,包括HDEL、HEXISTS、HGET、HGETALL、HINCRBY、HKEYS、HLEN 、HMGET 、HMSET 、HSET 、HSETNX 、HVALS 。但是無法支持HINCRBYFLOAT 、HSCAN 等命令。

函數(shù)說明

  • HDEL: 刪除對應(yīng)哈希(Hash)表的指定鍵(key)的字段,hdel(self, name, key)
  • HEXISTS: 檢測哈希(Hash)表對應(yīng)鍵(key)字段是否存在,返回布爾邏輯,hexists(self, name, key)
  • HGET: 獲取哈希(Hash)指定鍵(key)對應(yīng)的值,hget(self, name, key)
  • HGETALL: 獲取哈希(Hash)表的鍵-值對(key-value pairs),返回python字典類型數(shù)據(jù),hgetall(self, name)
  • HINCRBY: 為哈希表(Hash)指定鍵(key)對應(yīng)的值(key)加上指定的整數(shù)數(shù)值(int,可為負(fù)值),參見 [Python操作Redis:字符串(String)],(http://blog.csdn.net/u012894975/article/details/51285733)hincrby(self, name, key, amount=1),Redis 中本操作的值被限制在 64 位(bit)有符號數(shù)字。
  • HKEYS: 返回哈希表(Hash)對應(yīng)鍵(key)的數(shù)組(Python稱之為列表List),hkeys(self, name)
  • HLEN: 獲取哈希表(Hash)中鍵-值對(key-value pairs)個數(shù),hlen(self, name)
  • HMGET: 獲取哈希表(Hash)中一個或多個給點字段的值,不存在返回nil(Redis命令行)/None(Python),hmget(self, name, keys),其中keys可以為列表(list)
  • HMSET: 設(shè)置對個鍵-值對(key-value pairs)到哈希表(Hash)中,python輸入值(mapping)為字典(dictionary)類型,hmset(self, name, mapping)
  • HSET: 為哈希表(Hash)賦值,若鍵(key)存在值(value)則覆蓋,不存在則創(chuàng)建,hset(self, name, key, value)
  • HSETNX:為哈希表(Hash)不存值(value)的鍵(key)賦值,存在操作無效,對應(yīng)值(value)無變化,hsetnx(self, name, key, value)
  • HVALS:返回哈希表(Hash)對應(yīng)值(value)的列表,hvals(self, name)
  • ?

    # -*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import redis import time ## Connect local redis service client =redis.Redis(host='127.0.0.1',port=6379,db=0) print "Connection to server successfully!" dicKeys = client.keys("*") print dicKeys#-----------------------------10----------------------------------------- ### Redis hash command part Start ### # hset: Set key to value with hash name,hset(self, name, key, value) # hget: Return the value of ``key`` within the hash ``name``, hget(self, name, key) client.hset('myhash','field1',"foo") hashVal = client.hget('myhash','field1') print "Get hash value:",hashVal#------------------------------3------------------------------------------ # Get none-value hashVal = client.hget('myhash','field2') print "None hash value:",hashVal#---------------------------------2-------------------------------------------- # hexists: Returns a boolean indicating if ``key`` exists within hash ``name`` keyList= ['field1','field2'] for key in keyList:hexists = client.hexists('myhash',key)if hexists :print "Exist in redis-hash key:",keyelse:print "Not exist in redis-hash key:",key# hgetall: Return a Python dict of the hash's name/value pairs client.hset('myhash','field2',"bar") valDict = client.hgetall('myhash') print "Get python-dict from redis-hash",valDict# hincrby: Increment the value of ``key`` in hash ``name`` by ``amount`` # default increment is 1, client.hset('myhash','field',20)#-----------------------------------5---------------------------------------- client.hincrby('myhash','field') print "Get incrby value(Default):",client.hget('myhash','field') client.hincrby('myhash','field',2) print "Get incrby value(step: 2):",client.hget('myhash','field') client.hincrby('myhash','field',-3) print "Get incrby value(step: -3):",client.hget('myhash','field')# no method hincrbyfloat #-----------------------------------6---------------------------------------- #hkeys: Return the list of keys within hash ``name`` kL = client.hkeys('myhash') print "Get redis-hash key list",kL#-----------------------------------7---------------------------------------- #hlen: Return the number of elements in hash ``name`` lenHash =client.hlen('myhash') print "All hash length:",lenHash#-----------------------------------8---------------------------------------- #hmget: Returns a list of values ordered identically to ``keys`` #hmget(self, name, keys), keys should be python list data structure val =client.hmget('myhash',['field','field1','field2','field3','fieldx']) print "Get all redis-hash value list:",val#-----------------------------------9---------------------------------------- #hmset: Sets each key in the ``mapping`` dict to its corresponding value in the hash ``name`` hmDict={'field':'foo','field1':'bar'} client.hmset('hash',hmDict) hmKeys=hmDict.keys()#設(shè)置完畢#-----------------------------------8---------------------------------------- val = client.hmget('hash',hmKeys) print "Get hmset value:",val#-----------------------------------1---------------------------------------- #hdel: Delete ``key`` from hash ``name`` client.hdel('hash','field') print "Get delete result:",client.hget('hash','field')#----------------------------------12--------------------------------------- #hvals: Return the list of values within hash ``name`` val = client.hvals('myhash') print "Get redis-hash values with HVALS",valprint"-----------------------------11----------------------------------------" #----------------------------------11--------------------------------------- #hsetnx: Set ``key`` to ``value`` within hash ``name`` if ``key`` does not exist. # Returns 1 if HSETNX created a field, otherwise 0. r=client.hsetnx('myhash','field',2)#這句話的意思是,試圖為myhash的field賦值為2 print "Check hsetnx execute result:",r," Value:",client.hget('myhash','field') r=client.hsetnx('myhash','field10',20) print "Check hsetnx execute result:",r,"Value",client.hget('myhash','field10')hashVal = client.hgetall('profile') print hashVal #Empty db # client.flushdb()#這句話會把所有數(shù)據(jù)都清除掉

    ?

    總結(jié)

    以上是生活随笔為你收集整理的Python操作Redis中的hash的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。