2016-02-25 27 views
5

ho i seguenti tasti Redis (primavera dati Redis),Primavera Redis chiavi di ordinamento

localhost>Keys * 
"1+ { \"_id":"1", \"Name\" : \"C5796\" , \"Site\" : \"DRG1\"}" 
"2+ { \"_id":"2", \"Name\" : \"CX1XE\" , \"Site\" : \"DG1\"}" 
"3+ { \"_id":"3", \"Name\" : \"C553\" , \"Site\" : \"DG1\"}" 

Se voglio ordinare in base alla id/name/Area, come posso farlo in Spring Redis?

List<Object> keys = redistemplate.sort(SortQueryBuilder.sort("Customer").build()); 

e,

SortQuery<String> sort = SortQueryBuilder.sort(key).noSort().get(field).build(); 
List<?> keys = redistemplate.boundHashOps(key).getOperations().sort(sort); 

non funzionano.

+0

Voglio solo parlare, la le chiavi sono operazioni di hash. localhost> HKeys Cliente "1+ {\" _ id ":" 1 ", \" Nome \ ": \" C5796 \ ", \" Sito \ ": \" DRG1 \ "}" "2+ { \ "_ id": "2", \ "Nome \": \ "CX1XE \", \ "Sito \": \ "DG1 \"} " " 3+ {\ "_ id": "3", \ " Nome \ ": \" C553 \ ", \" Sito \ ": \" DG1 \ "}" – ashK

risposta

2

Il codice è l'ultimo del post, se si ha familiarità con il principio di chiavi multi hset in rosso, saltare il seguente contenuto e leggere direttamente il codice.

Redis Sort è dedicato alle ordinare i campi all'interno List/Set/Zset, ma questo metodo può essere usato per ordinare più di base tasti specificato metrica che vogliamo. Possiamo usare "sort" per ordinare le chiavi multi-hset per il campo specificato, ma esiste una limitazione per il modello delle chiavi hset.
Ad esempio, se il modello delle chiavi hset è "hash {i}" (i è un numero intero), in questa condizione possiamo ordinarlo.

127.0.0.1:6379> keys hash* 
1) "hash3" 
2) "hash2" 
3) "hash1" 

Date un'occhiata al contenuto della hash1:

127.0.0.1:6379> hgetall hash1 
1) "id" 
2) "24" 
3) "name" 
4) "kobe" 

Ogni tasto cancelletto contiene due campi: "id", "nome". Se vogliamo ordinare queste chiavi hset con il suo id. Cosa dovremmo fare ?

Innanzitutto, aggiungere una chiave impostata denominata "myset". "myset" è una chiave impostata che contiene membri {"1", "2", "3"}.

127.0.0.1:6379> smembers myset 
1) "1" 
2) "2" 
3) "3" 

quindi eseguire il seguente comando:

127.0.0.1:6379> SORT myset BY hash*->id GET hash*->id GET hash*->name 
1) "3" 
2) "wade" 
3) "24" 
4) "kobe" 
5) "30" 
6) "curry" 

Eureka, sorta hash {1-3} dal suo id.
Ecco il codice di utilizzo di primavera Redis per fare il lavoro:

public static String getRandomStr() { 
    return String.valueOf(new Random().nextInt(100)); 
} 

public static void redisTemplateSort(RedisTemplate redisTemplate) { 
    String sortKey = "sortKey"; 

    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); 

    redisTemplate.setKeySerializer(stringRedisSerializer); 
    redisTemplate.setValueSerializer(stringRedisSerializer); 
    redisTemplate.setHashKeySerializer(stringRedisSerializer); 
    redisTemplate.setHashValueSerializer(stringRedisSerializer); 

    redisTemplate.delete(sortKey); 
    if (!redisTemplate.hasKey(sortKey)) { 
     for (int i = 0; i < 10; i++) { 
      redisTemplate.boundSetOps(sortKey).add(String.valueOf(i)); 
      String hashKey = "hash" + i, 
        strId = String.valueOf(i), 
        strName = getRandomStr(), 
        strSite = getRandomStr(); 
      redisTemplate.boundHashOps(hashKey).put("_id", strId); 
      redisTemplate.boundHashOps(hashKey).put("Name", strName); 
      redisTemplate.boundHashOps(hashKey).put("Site", strSite); 

      System.out.printf("%s : {\"_id\": %s, \"Name\": %s, \"Site\", %s}\n", 
        hashKey, strId, strName, strSite); 
     } 
    } 

    SortQuery<String> sortQuery = SortQueryBuilder.sort(sortKey).by("hash*->Name") 
      .get("hash*->_id").get("hash*->Name").get("hash*->Site").build(); 
    List<String> sortRslt = redisTemplate.sort(sortQuery); 

    for (int i = 0; i < sortRslt.size();) { 
     System.out.printf("{\"_id\": %s, \"Name\": %s, \"Site\", %s}\n", sortRslt.get(i+2), sortRslt.get(i+1), sortRslt.get(i)); 
     i += 3; 
    } 
} 

Risultato di esecuzione redisTemplateSort(redisTemplate) (come ordina per nome in codice):

hash0 : {"_id": 0, "Name": 59, "Site", 60} 
hash1 : {"_id": 1, "Name": 37, "Site", 57} 
hash2 : {"_id": 2, "Name": 6, "Site", 40} 
hash3 : {"_id": 3, "Name": 91, "Site", 58} 
hash4 : {"_id": 4, "Name": 39, "Site", 32} 
hash5 : {"_id": 5, "Name": 27, "Site", 82} 
hash6 : {"_id": 6, "Name": 43, "Site", 10} 
hash7 : {"_id": 7, "Name": 17, "Site", 55} 
hash8 : {"_id": 8, "Name": 14, "Site", 91} 
hash9 : {"_id": 9, "Name": 39, "Site", 91} 
{"_id": 40, "Name": 6, "Site", 2} 
{"_id": 91, "Name": 14, "Site", 8} 
{"_id": 55, "Name": 17, "Site", 7} 
{"_id": 82, "Name": 27, "Site", 5} 
{"_id": 57, "Name": 37, "Site", 1} 
{"_id": 32, "Name": 39, "Site", 4} 
{"_id": 91, "Name": 39, "Site", 9} 
{"_id": 10, "Name": 43, "Site", 6} 
{"_id": 60, "Name": 59, "Site", 0} 
{"_id": 58, "Name": 91, "Site", 3} 
+0

Grazie, al momento sto lavorando a una scintilla e una volta finita dovrò tornare qui. Controlla e accetta la risposta – ashK

+0

@ sel-fish c'è un modo per stampare/ottenere il nome della chiave (come hash1) in o/p di "SORT myset BY hash * -> ID GET hash * -> id GET hash * - > nome "? – Bharat

2

Non so di dati di primavera redis. Permettetemi di darvi un campione per ottenere questo in ingenuo Redis. Diciamo che hai hash, che ha id, nome e sito. e ho una lista che rappresenta le chiavi di quell'hash.

mia struttura sarà come:

lpush("Values",1); 


hset("hash_1","id","1"),hset("hash_1","Name","C5796"),hset("hash_1","Site","DRG1") 

for second hash 
lpush("Values",2); 
... 

Allo stesso modo per tutti i valori che si desidera impostare in hash. Ora per l'ordinamento fai così

SORT "Values" BY hash_*->id get hash_*->id get hash_*->name get hash_*->site 

questo ti restituirà risultati di hashmap ordinati in base all'id. allo stesso modo si può fare per nomi/sito. Per maggiori informazioni sull'ordinamento in redis: http://redis.io/commands/sort

Problemi correlati