Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis/java - writing and reading binary data

I'm trying to write and read a gzip to/from Redis. The problem is that I tried saving the read bytes to a file and opening it with gzip - it's invalid. The strings are also different when looking at them in the Eclipse console.

Here's my code:

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);
  }

}
like image 876
Buffalo Avatar asked Dec 20 '16 15:12

Buffalo


People also ask

Does Redis support binary data?

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file. The empty string is also a valid key.

Which classes allow to read and write binary data?

The preferred stream classes for processing binary files are ObjectInputStream and ObjectOutputStream. Each has methods to read/write data one byte at a time and can automatically convert numbers and characters to bytes.

Does Redis store everything as string?

In Redis, we have the advantage of storing both strings and collections of strings as values. A string value cannot exceed 512 MB of text or binary data. However, it can store any type of data, like text, integers, floats, videos, images, and audio files.

Why use Redisson?

It allows you to use all of the familiar Java collections and data structures on top of Redis - such as List, Map, Queue, Lock, Semaphore and many more. Redisson has practically zero learning curve for Java developers who already know these standard interfaces and want to use Redis.


1 Answers

You are using Jedis.get(String) to read which includes an inner UTF-8 conversion. But using Jedis.set(byte[], byte[]) to write does not include such conversion. The mismatch could be because of this reason. If so, you can try Jedis.get(byte[]) to read from redis to skip UTF-8 conversion. E.g.

byte[] readJsonContent = jedis.get(jsonKey.getBytes());
like image 86
sazzad Avatar answered Sep 24 '22 00:09

sazzad