2016-12-20 2 views
0

Redis에서 /로 Red Hat로 gzip을 쓰고 읽으려고합니다. 문제는 내가 파일에 읽기 바이트를 저장하고 gzip으로 열어 보았습니다. 잘못된 것입니다. 이클립스 콘솔에서 문자열을 볼 때 문자열도 다르다. 당신은 내부 UTF-8 변환을 포함하는 읽기 Jedis.get(String)을 사용하는Redis/java - 이진 데이터 쓰기 및 읽기

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

import redis.clients.jedis.Jedis; 

public class TestRedis 
{ 

    public static void main(String[] args) throws IOException 
    { 
    String fileName = "D:/temp/test_write.gz"; 

    String jsonKey = fileName; 

    Jedis jedis = new Jedis("127.0.0.1"); 

    byte[] jsonContent = ReadFile(new File(fileName).getPath()); 

    // test-write data we're storing in redis 
    FileOutputStream fostream = new FileOutputStream("D:/temp/test_write_before_redis.gz"); // looks ok 
    fostream.write(jsonContent); 
    fostream.close(); 

    jedis.set(jsonKey.getBytes(), jsonContent); 

    System.out.println("writing, key: " + jsonKey + ",\nvalue: " + new String(jsonContent)); // looks ok 

    byte[] readJsonContent = jedis.get(jsonKey).getBytes(); 
    String readJsonContentString = new String(readJsonContent); 
    FileOutputStream fos = new FileOutputStream("D:/temp/test_read.gz"); // invalid gz file :( 
    fos.write(readJsonContent); 
    fos.close(); 

    System.out.println("n\nread json content from redis: " + readJsonContentString); 

    } 

    private static byte[] ReadFile(String aFilePath) throws IOException 
    { 
    Path path = Paths.get(aFilePath); 
    return Files.readAllBytes(path); 
    } 

} 

답변

3

:

여기 내 코드입니다. 그러나 쓰기 위해 Jedis.set(byte[], byte[])을 사용하는 것은 그러한 변환을 포함하지 않습니다. 불일치는 이러한 이유 때문일 수 있습니다. 그렇다면 Jedis.get(byte[])을 redis에서 읽으므로 UTF-8 변환을 건너 뛸 수 있습니다. 예 :

byte[] readJsonContent = jedis.get(jsonKey.getBytes()); 
+0

굉장, 고마워! 지금, 이것이 Predis로 어떻게 할 수 있는지보기 위해서 .. :) – Buffalo