Movatterモバイル変換


[0]ホーム

URL:


Products
Resources
DocsPricing
LoginBook a meetingTry Redis

HVALS

Syntax
HVALS key
Available since:
Redis CE 2.0.0
Time complexity:
O(N) where N is the size of the hash.
ACL categories:
@read,@hash,@slow,

Returns all values in the hash stored atkey.

Examples

redis> HSET myhash field1 "Hello"(integer) 1redis> HSET myhash field2 "World"(integer) 1redis> HVALS myhash1) "Hello"2) "World"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
importredisr=redis.Redis(host="localhost",port=6379,db=0,decode_responses=True)res1=r.hset("myhash","field1","Hello")print(res1)# >>> 1res2=r.hget("myhash","field1")print(res2)# >>> Hellores3=r.hset("myhash",mapping={"field2":"Hi","field3":"World"})print(res3)# >>> 2res4=r.hget("myhash","field2")print(res4)# >>> Hires5=r.hget("myhash","field3")print(res5)# >>> Worldres6=r.hgetall("myhash")print(res6)# >>> { "field1": "Hello", "field2": "Hi", "field3": "World" }res7=r.hset("myhash","field1","foo")print(res7)# >>> 1res8=r.hget("myhash","field1")print(res8)# >>> foores9=r.hget("myhash","field2")print(res9)# >>> Noneres10=r.hset("myhash",mapping={"field1":"Hello","field2":"World"})res11=r.hgetall("myhash")print(res11)# >>> { "field1": "Hello", "field2": "World" }res10=r.hset("myhash",mapping={"field1":"Hello","field2":"World"})res11=r.hvals("myhash")print(res11)# >>> [ "Hello", "World" ]
importassertfrom'node:assert';import{createClient}from'redis';constclient=createClient();awaitclient.connect().catch(console.error);constres1=awaitclient.hSet('myhash','field1','Hello')console.log(res1)// 1constres2=awaitclient.hGet('myhash','field1')console.log(res2)// Helloconstres3=awaitclient.hSet('myhash',{'field2':'Hi','field3':'World'})console.log(res3)// 2constres4=awaitclient.hGet('myhash','field2')console.log(res4)// Hiconstres5=awaitclient.hGet('myhash','field3')console.log(res5)// Worldconstres6=awaitclient.hGetAll('myhash')console.log(res6)constres7=awaitclient.hSet('myhash','field1','foo')console.log(res7)// 1constres8=awaitclient.hGet('myhash','field1')console.log(res8)// fooconstres9=awaitclient.hGet('myhash','field2')console.log(res9)// nullawaitclient.quit();
importjava.util.HashMap;importjava.util.Map;importjava.util.List;importjava.util.Collections;importredis.clients.jedis.UnifiedJedis;import staticjava.util.stream.Collectors.toList;import staticorg.junit.jupiter.api.Assertions.assertEquals;import staticorg.junit.jupiter.api.Assertions.assertNull;publicclassCmdsHashExample{publicvoidrun(){UnifiedJedisjedis=newUnifiedJedis("redis://localhost:6379");Map<String,String>hGetExampleParams=newHashMap<>();hGetExampleParams.put("field1","foo");longhGetResult1=jedis.hset("myhash",hGetExampleParams);System.out.println(hGetResult1);// >>> 1StringhGetResult2=jedis.hget("myhash","field1");System.out.println(hGetResult2);// >>> fooStringhGetResult3=jedis.hget("myhash","field2");System.out.println(hGetResult3);// >>> nullMap<String,String>hGetAllExampleParams=newHashMap<>();hGetAllExampleParams.put("field1","Hello");hGetAllExampleParams.put("field2","World");longhGetAllResult1=jedis.hset("myhash",hGetAllExampleParams);System.out.println(hGetAllResult1);// >>> 2Map<String,String>hGetAllResult2=jedis.hgetAll("myhash");System.out.println(hGetAllResult2.entrySet().stream().sorted((s1,s2)->s1.getKey().compareTo(s2.getKey())).collect(toList()).toString());// >>> [field1=Hello, field2=World]Map<String,String>hSetExampleParams=newHashMap<>();hSetExampleParams.put("field1","Hello");longhSetResult1=jedis.hset("myhash",hSetExampleParams);System.out.println(hSetResult1);// >>> 1StringhSetResult2=jedis.hget("myhash","field1");System.out.println(hSetResult2);// >>> HellohSetExampleParams.clear();hSetExampleParams.put("field2","Hi");hSetExampleParams.put("field3","World");longhSetResult3=jedis.hset("myhash",hSetExampleParams);System.out.println(hSetResult3);// >>> 2StringhSetResult4=jedis.hget("myhash","field2");System.out.println(hSetResult4);// >>> HiStringhSetResult5=jedis.hget("myhash","field3");System.out.println(hSetResult5);// >>> WorldMap<String,String>hSetResult6=jedis.hgetAll("myhash");for(Stringkey:hSetResult6.keySet()){System.out.println("Key: "+key+", Value: "+hSetResult6.get(key));}// >>> Key: field3, Value: World// >>> Key: field2, Value: Hi// >>> Key: field1, Value: HelloMap<String,String>hValsExampleParams=newHashMap<>();hValsExampleParams.put("field1","Hello");hValsExampleParams.put("field2","World");longhValsResult1=jedis.hset("myhash",hValsExampleParams);System.out.println(hValsResult1);// >>> 2List<String>hValsResult2=jedis.hvals("myhash");Collections.sort(hValsResult2);System.out.println(hValsResult2);// >>> [Hello, World]jedis.close();}}
packageexample_commands_testimport("context""fmt""sort""github.com/redis/go-redis/v9")funcExampleClient_hset(){ctx:=context.Background()rdb:=redis.NewClient(&redis.Options{Addr:"localhost:6379",Password:"",// no password docsDB:0,// use default DB})res1,err:=rdb.HSet(ctx,"myhash","field1","Hello").Result()iferr!=nil{panic(err)}fmt.Println(res1)// >>> 1res2,err:=rdb.HGet(ctx,"myhash","field1").Result()iferr!=nil{panic(err)}fmt.Println(res2)// >>> Hellores3,err:=rdb.HSet(ctx,"myhash","field2","Hi","field3","World",).Result()iferr!=nil{panic(err)}fmt.Println(res3)// >>> 2res4,err:=rdb.HGet(ctx,"myhash","field2").Result()iferr!=nil{panic(err)}fmt.Println(res4)// >>> Hires5,err:=rdb.HGet(ctx,"myhash","field3").Result()iferr!=nil{panic(err)}fmt.Println(res5)// >>> Worldres6,err:=rdb.HGetAll(ctx,"myhash").Result()iferr!=nil{panic(err)}keys:=make([]string,0,len(res6))forkey,_:=rangeres6{keys=append(keys,key)}sort.Strings(keys)for_,key:=rangekeys{fmt.Printf("Key: %v, value: %v\n",key,res6[key])}// >>> Key: field1, value: Hello// >>> Key: field2, value: Hi// >>> Key: field3, value: World}funcExampleClient_hget(){ctx:=context.Background()rdb:=redis.NewClient(&redis.Options{Addr:"localhost:6379",Password:"",// no password docsDB:0,// use default DB})res7,err:=rdb.HSet(ctx,"myhash","field1","foo").Result()iferr!=nil{panic(err)}fmt.Println(res7)// >>> 1res8,err:=rdb.HGet(ctx,"myhash","field1").Result()iferr!=nil{panic(err)}fmt.Println(res8)// >>> foores9,err:=rdb.HGet(ctx,"myhash","field2").Result()iferr!=nil{fmt.Println(err)}fmt.Println(res9)// >>> <empty string>}funcExampleClient_hgetall(){ctx:=context.Background()rdb:=redis.NewClient(&redis.Options{Addr:"localhost:6379",Password:"",// no passwordDB:0,// use default DB})hGetAllResult1,err:=rdb.HSet(ctx,"myhash","field1","Hello","field2","World",).Result()iferr!=nil{panic(err)}fmt.Println(hGetAllResult1)// >>> 2hGetAllResult2,err:=rdb.HGetAll(ctx,"myhash").Result()iferr!=nil{panic(err)}keys:=make([]string,0,len(hGetAllResult2))forkey,_:=rangehGetAllResult2{keys=append(keys,key)}sort.Strings(keys)for_,key:=rangekeys{fmt.Printf("Key: %v, value: %v\n",key,hGetAllResult2[key])}// >>> Key: field1, value: Hello// >>> Key: field2, value: World}funcExampleClient_hvals(){ctx:=context.Background()rdb:=redis.NewClient(&redis.Options{Addr:"localhost:6379",Password:"",// no password docsDB:0,// use default DB})hValsResult1,err:=rdb.HSet(ctx,"myhash","field1","Hello","field2","World",).Result()iferr!=nil{panic(err)}fmt.Println(hValsResult1)// >>> 2hValsResult2,err:=rdb.HVals(ctx,"myhash").Result()iferr!=nil{panic(err)}sort.Strings(hValsResult2)fmt.Println(hValsResult2)// >>> [Hello World]}
usingStackExchange.Redis;publicclassCmdsHashExample{publicvoidrun(){varmuxer=ConnectionMultiplexer.Connect("localhost:6379");vardb=muxer.GetDatabase();boolres1=db.HashSet("myhash","field1","foo");RedisValueres2=db.HashGet("myhash","field1");Console.WriteLine(res2);// >>> fooRedisValueres3=db.HashGet("myhash","field2");Console.WriteLine(res3);// >>> Nullboolres4=db.HashSet("myhash","field1","Hello");RedisValueres5=db.HashGet("myhash","field1");Console.WriteLine(res5);// >>> Hellodb.HashSet("myhash",newHashEntry[]{newHashEntry("field2","Hi"),newHashEntry("field3","World")});RedisValueres6=db.HashGet("myhash","field2");Console.WriteLine(res6);// >>> HiRedisValueres7=db.HashGet("myhash","field3");Console.WriteLine(res7);// >>> WorldHashEntry[]res8=db.HashGetAll("myhash");Console.WriteLine($"{string.Join(",", res8.Select(h => $"{h.Name}:{h.Value}"))}");// >>> field1: Hello, field2: Hi, field3: Worlddb.HashSet("myhash",newHashEntry[]{newHashEntry("field1","Hello"),newHashEntry("field2","World")});HashEntry[]hGetAllResult=db.HashGetAll("myhash");Array.Sort(hGetAllResult,(a1,a2)=>a1.Name.CompareTo(a2.Name));Console.WriteLine(string.Join(", ",hGetAllResult.Select(e=>$"{e.Name}: {e.Value}")));// >>> field1: Hello, field2: Worlddb.HashSet("myhash",newHashEntry[]{newHashEntry("field1","Hello"),newHashEntry("field2","World")});RedisValue[]hValsResult=db.HashValues("myhash");Array.Sort(hValsResult);Console.WriteLine(string.Join(", ",hValsResult));// >>> Hello, World}}

Give these commands a try in the interactive console:

HSET myhash field1 "Hello"HSET myhash field2 "World"HVALS myhash

RESP2 Reply

Array reply: a list of values in the hash, or an empty list when the key does not exist

RESP3 Reply

Array reply: a list of values in the hash, or an empty list when the key does not exist.
RATE THIS PAGE
Back to top ↑
See also
HDEL
HEXISTS
HEXPIRE
HEXPIREAT
HEXPIRETIME
HGET
HGETALL
HGETDEL
HGETEX
HINCRBY
HINCRBYFLOAT
HKEYS
HLEN
HMGET
HMSET
HPERSIST
HPEXPIRE
HPEXPIREAT
HPEXPIRETIME
HPTTL
HRANDFIELD
HSCAN
HSET
HSETEX
HSETNX
HSTRLEN
HTTL
HVALS

[8]ページ先頭

©2009-2025 Movatter.jp