如何将 Java Long 转换为 Cassandra 的 byte[]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2641150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:10:33  来源:igfitidea点击:

How to convert a Java Long to byte[] for Cassandra?

javacassandra

提问by dfrankow

Lazy programmer alert. :)

懒惰的程序员警报。:)

Cassandra stores column values as bytes (Java example). Specifying a LongType comparatorcompares those bytes as a long. I want the value of a long into a Cassandra-friendly byte[]. How? I poked around for awhile. I think you people can help me faster.

Cassandra 将列值存储为字节(Java 示例)。指定LongType 比较器会将这些字节作为 long 进行比较。我希望将 long 的值转换为对 Cassandra 友好的 byte[]。如何?我摸索了一会儿。我认为你们可以更快地帮助我。

EDIT:

编辑:

Both Alexander and Eli's answers agreed with this reverse transformation. Thanks!

Alexander 和 Eli 的回答都同意这种逆向转换。谢谢!

采纳答案by Alexander Pogrebnyak

Here is cut and paste from java 6 DataOutputStream.writeLong

这是从 java 6 剪切和粘贴 DataOutputStream.writeLong

public final void writeLong(long v) throws IOException {
    writeBuffer[0] = (byte)(v >>> 56);
    writeBuffer[1] = (byte)(v >>> 48);
    writeBuffer[2] = (byte)(v >>> 40);
    writeBuffer[3] = (byte)(v >>> 32);
    writeBuffer[4] = (byte)(v >>> 24);
    writeBuffer[5] = (byte)(v >>> 16);
    writeBuffer[6] = (byte)(v >>>  8);
    writeBuffer[7] = (byte)(v >>>  0);
    out.write(writeBuffer, 0, 8);
incCount(8);
}

Here are modifications for your case

以下是针对您的案例的修改

public final byte[] longToBytes(long v) {
    byte[] writeBuffer = new byte[ 8 ];

    writeBuffer[0] = (byte)(v >>> 56);
    writeBuffer[1] = (byte)(v >>> 48);
    writeBuffer[2] = (byte)(v >>> 40);
    writeBuffer[3] = (byte)(v >>> 32);
    writeBuffer[4] = (byte)(v >>> 24);
    writeBuffer[5] = (byte)(v >>> 16);
    writeBuffer[6] = (byte)(v >>>  8);
    writeBuffer[7] = (byte)(v >>>  0);

    return writeBuffer;
}

回答by Eli Courtwright

I would write the long to a ByteArrayOutputStreamwrapped in a DataOutputStreamand then retrieve the raw bytes, although this will always give you your data in big endian byte order (most significant byte first):

我会将 long 写入包装在DataOutputStream 中ByteArrayOutputStream,然后检索原始字节,尽管这将始终以大端字节顺序(最重要的字节在前)为您提供数据:

public static byte[] getBytes(Long val)
    throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeLong(val);
    return baos.toByteArray();
}

If you want to be able to specify the endianness, you can use the ByteBufferclass:

如果您希望能够指定字节顺序,可以使用ByteBuffer类:

public static byte[] getBytes(Long val)
{
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(val);
    return buf.array();
}

回答by Michal

You can use Cassandra's utility class: ByteBufferUtil.bytes(long n)

您可以使用 Cassandra 的实用程序类: ByteBufferUtil.bytes(long n)

回答by President James K. Polk

You can crack the bytes apart by using shifts and mask, or a bit easier is ByteBuffer.wrapto wrap an 8 long byte array and using the putLongmethod. You must set the ByteOrder first using the ByteBuffer.ordermethod.

您可以通过使用移位和掩码来分解字节,或者更容易一点的是ByteBuffer.wrap包装一个 8 长字节数组并使用该putLong方法。您必须首先使用该ByteBuffer.order方法设置 ByteOrder 。