java 将 cassandra blob 类型转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31815778/
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-11-02 19:10:07  来源:igfitidea点击:

Converting cassandra blob type to string

javacassandrablobcqldatastax

提问by idoda

I have an old column family which has a column named "value" which was defined as a blob data type. This column usually holds two numbers separated with an underscore, like "421_2".

我有一个旧的列族,它有一个名为“value”的列,它被定义为 blob 数据类型。此列通常包含用下划线分隔的两个数字,例如“421_2”。

When im using the python datastax driver and execute the query, the results return with that field parsed as a string:

当我使用 python datastax 驱动程序并执行查询时,结果返回该字段解析为字符串:

In [21]: session.execute(q)
Out[21]: 
[Row(column1=4776015, value='145_0'),
 Row(column1=4891778, value='114_0'),
 Row(column1=4891780, value='195_0'),
 Row(column1=4893662, value='105_0'),
 Row(column1=4893664, value='115_0'),
 Row(column1=4898493, value='168_0'),
 Row(column1=4945162, value='148_0'),
 Row(column1=4945163, value='131_0'),
 Row(column1=4945168, value='125_0'),
 Row(column1=4945169, value='211_0'),
 Row(column1=4998426, value='463_0')]

When I use the java driver I get a com.datastax.driver.core.Row object back. When I try to read the value field by, for example, row.getString("value")I get the expected InvalidTypeException: Column value is of type blob. Seems like the only way to read the field is via row.getBytes("value")and then I get back an java.nio.HeapByteBufferobject.

当我使用 java 驱动程序时,我会返回一个 com.datastax.driver.core.Row 对象。例如,当我尝试读取 value 字段时,row.getString("value")我得到了预期的InvalidTypeException: Column value is of type blob. 似乎读取该字段的唯一方法是通过row.getBytes("value")然后我取回一个java.nio.HeapByteBuffer对象。

Problem is, I cant seem to convert this object to string in an easy fashion. Googling yielded two answers from 2012 that suggest the following:

问题是,我似乎无法以简单的方式将此对象转换为字符串。谷歌搜索从 2012 年得到了两个答案,建议如下:

String string_value = new String(result.getBytes("value"), "UTF-8");

But such a String constructor doesn't seems to exist anymore. So, my questions are:

但是这样的 String 构造函数似乎不再存在了。所以,我的问题是:

  1. How do I convert HeapByteBuffer into string?
  2. How come the python driver converted the blob easily and the java one did not?
  1. 如何将 HeapByteBuffer 转换为字符串?
  2. 为什么python驱动程序很容易转换blob而java驱动程序没有?

Side Note: I could debug the python driver, but currently that seems too much work for something that should be trivial. (and the fact that no one asked about it suggests Im missing something simple here..)

旁注:我可以调试 python 驱动程序,但目前对于一些应该是微不足道的事情来说似乎工作太多了。(而且没有人问过它这一事实表明我在这里遗漏了一些简单的东西..)

回答by popcorny

Another easier way is to change the CQL statement.

另一种更简单的方法是更改​​ CQL 语句。

select column1, blobastext(value) from YourTable where key = xxx

The second column would be type of String.

第二列将是字符串类型。

回答by Olivier Michallat

You can also get direct access to the Java driver's serializers. This way you don't have to deal with low-level details, and it also works for other types.

您还可以直接访问 Java 驱动程序的序列化程序。这样你就不必处理低级细节,它也适用于其他类型。

Driver 2.0.x:

驱动程序 2.0.x:

String s = (String)DataType.text().deserialize(byteBuffer);

Driver 2.1.x:

驱动程序 2.1.x:

ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
String s = (String)DataType.text().deserialize(byteBuffer, protocolVersion);

Driver 2.2.x:

驱动程序 2.2.x:

ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
String s = TypeCodec.VarcharCodec.instance.deserialize(byteBuffer, protocolVersion);

回答by Jenna

For version 3.1.4 of the datastax java driver the following will convert a blob to a string:

对于 3.1.4 版的 datastax java 驱动程序,以下内容会将 blob 转换为字符串:

ProtocolVersion proto = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();

String deserialize = TypeCodec.varchar().deserialize(row.getBytes(i), proto);

回答by Adam Holmberg

1.) Converting from byte buffer in Java is discussed in this answer.

1.) 在这个答案中讨论了从 Java 中的字节缓冲区转换。

2.) Assuming you're using Python 2, it's coming back as a string in Python because str is the binary type.

2.) 假设您使用的是 Python 2,它会作为 Python 中的字符串返回,因为 str 是二进制类型。