简体   繁体   English

如何在特定大小的Java中生成二进制Blob对象?

[英]How to generate a binary blob object in java of a specific size?

I am trying to build an opaque Binary Blob 64-bit encoded with any garbage/random content, also trying to make it in such a way so that I can specify the size in Kb for the blob of data to generate into it. 我正在尝试构建一个不透明的二进制Blob,它使用任何垃圾内容/随机内容进行编码,并且尝试以某种方式进行编码,以便我可以指定要生成的数据Blob的大小,以Kb为单位。

The purpose of it is to bench-mark the write throughput of a DB application which support the blob datatype (eg C*). 它的目的是对支持blob数据类型(例如C *)的DB应用程序的写吞吐量进行基准测试。 The blob does not need to be unique, hence it can be constructed once and just reuse it to avoid latency client side due to GC and object creation. Blob不必是唯一的,因此它可以被构造一次,并且只需重新使用它即可避免由于GC和对象创建而导致客户端延迟。

I have naively tried using a ByteBuffer.allocateDirect(bSize) thinking I could just use whatever garbage it will pickup from memory, however I am doubtful that this approach works. 我天真地尝试使用ByteBuffer.allocateDirect(bSize)以为我可以使用它将从内存中提取的任何垃圾,但是我怀疑这种方法是否有效。

Any suggestions as to how to achieve that? 关于如何实现这一目标的任何建议?

If you are using Linux, this little command line trick creates dummy files quickly: 如果您使用的是Linux,此命令行小技巧将快速创建虚拟文件:

 dd if=/dev/zero of=FileName bs=1024 count=1000

count * bs = filesize count * bs =文件大小

Then: 然后:

String sql = "INSERT INTO table (blob) VALUES (?)";
PreparedStatement stmt = conn.prepareStatement(sql);
File blob = new File("path_to_FileName");
FileInputStream   fis = new FileInputStream(blob);
stmt.setBinaryStream(1, fis, (int) blob.length());
stmt.execute();

If you need the data to be random you can use Random.nextBytes(byte[]); 如果需要随机数据,可以使用Random.nextBytes(byte []);。 however it is rare for such systems to care about the contents so you can just use all zeros. 但是,此类系统很少关心内容,因此您只能使用全零。

byte[] blob = new byte[byteCount];

// optionally
// new Random().nextBytes(blob);

stmt.setBinaryStream(1, new ByteArrayInputStream(blob), blob.length);

Make a custom implementation of InputStream to produce random data: 进行InputStream的自定义实现以生成随机数据:

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

public class RandomInputStream extends InputStream
{
    private int available;
    private Random random = new Random();

    public RandomInputStream(final int totalSize)
    {
        this.available = totalSize;
    }

    @Override
    public int read() throws IOException
    {
        if(available == 0)
            return -1;
        --available;
        return random.nextInt(256);
    }


    @Override
    public int available() throws IOException
    {
        return available;
    }
}

And then just use it as usual: 然后像往常一样使用它:

InputStream is = new RandomInputStream(1024*1024);
stmt.setBinaryStream(1, is, is.available());

This would be the fastest solution possible. 这将是最快的解决方案。

byte[] blob = new byte[byteCount]; byte [] blob =新的byte [byteCount];

// optionally // new Random().nextBytes(blob); //可选// new Random()。nextBytes(blob);

stmt.setBinaryStream(1, new ByteArrayInputStream(blob), blob.length); stmt.setBinaryStream(1,新的ByteArrayInputStream(blob),blob.length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM