RedisTemplate通過scan方法進行自定義操作:1、根據hashKey的名稱匹配相關hash鍵值對


需求:有一個hash如下,現在想查詢出stream前綴的鍵值對

 

 操作方法如下:

package com.example;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;
import java.util.TreeMap;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestHash {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testScan()
    {
        Cursor<Map.Entry<String, String>> cursor = redisTemplate.opsForHash().scan(
                "media.9f2ef88f-c7b2-4325-8d64-ba03a9278516",
                ScanOptions.scanOptions().match("stream*.ts").build()); //此處使用表達式和鍵名進行匹配

        TreeMap<String,byte[]> streaming = new TreeMap<>();

        while (cursor.hasNext())
        {
            Map.Entry<String, String> entry = cursor.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            System.out.printf("%s %s\n",key,value);
        }

    }   
}

 

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM