上下文:Spring数据cassandra官方1.0.2.RELEASE来自Maven Central repo,CQL3,cassandra 2.0,datastax driver 2.0.4
背景:cassandra blob数据类型映射到Java ByteBuffer。
下面的示例代码演示了您不会使用等效插入旁边的select来检索正确的字节。实际检索的数据以许多垃圾字节为前缀,实际上看起来像是整行的序列化。 与Cassandra 1.2相关的这个older post表明我们可能必须从长度为ByteBuffer.remaining()的ByteBuffer.arrayOffset()开始,但是arrayOffset值实际为0.
我发现了一个spring-data-cassandra 2.0.0。 SNAPSHOT但CassandraOperations API有很大的不同,它的包名也是:org.springdata ...与org.springframework ...
帮助解决这个问题但非常欢迎。
与此同时,我似乎必须将Base64的二进制数据编码/解码到文本数据类型列中。
---这里是我使用的简单表CQL元数据-------------
CREATE TABLE person (
id text,
age int,
name text,
pict blob,
PRIMARY KEY (id)
) ;
---跟随映射到CQL表的简单数据对象---
package org.spring.cassandra.example;
import java.nio.ByteBuffer;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;
@Table
public class Person {
@PrimaryKey
private String id;
private int age;
private String name;
private ByteBuffer pict;
public Person(String id, int age, String name, ByteBuffer pict) {
this.id = id; this.name = name; this.age = age; this.pict = pict;
}
public String getId() { return id; }
public String getName() { return name; }
public int getAge() { return age; }
public ByteBuffer getPict() { return pict; }
}
}
---以及简单地插入和检索人物对象的普通java应用程序代码 -
package org.spring.cassandra.example;
import java.nio.ByteBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.cassandra.core.CassandraOperations;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
public class CassandraApp {
private static final Logger logger = LoggerFactory
.getLogger(CassandraApp.class);
public static String hexDump(ByteBuffer bb) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
bb.rewind();
char[] hexChars = new char[bb.limit() * 2];
for ( int j = 0; j < bb.limit(); j++ ) {
int v = bb.get() & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
bb.rewind();
return new String(hexChars);
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app-context.xml");
try {
CassandraOperations cassandraOps = applicationContext.getBean(
"cassandraTemplate", CassandraOperations.class);
cassandraOps.truncate("person");
// prepare data
byte[] ba = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11, 0x22, 0x33, 0x44, 0x55, (byte) 0xAA, (byte) 0xCC, (byte) 0xFF };
ByteBuffer myPict = ByteBuffer.wrap(ba);
String myId = "1234567890";
String myName = "mickey";
int myAge = 50;
logger.info("We try id=" + myId + ", name=" + myName + ", age=" + myAge +", pict=" + hexDump(myPict));
cassandraOps.insert(new Person(myId, myAge, myName, myPict ));
Select s = QueryBuilder.select("id","name","age","pict").from("person");
s.where(QueryBuilder.eq("id", myId));
ResultSet rs = cassandraOps.query(s);
Row r = rs.one();
logger.info("We got id=" + r.getString(0) + ", name=" + r.getString(1) + ", age=" + r.getInt(2) +", pict=" + hexDump(r.getBytes(3)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
---假设您为cassandra配置了一个简单的Spring项目 正如http://projects.spring.io/spring-data-cassandra/
所述实际执行产生(滑动这些记录器跟踪以查看结束):
[main] INFO org.spring.cassandra.example.CassandraApp - 我们尝试id = 1234567890,name = mickey,age = 50,pict = 0001020304051122334455AACCFF
[主要] INFO org.spring.cassandra.example.CassandraApp - 我们得到了ID = 1234567890,姓名=米奇,年龄= 50,PICT = 8200000800000073000000020000000100000004000A6D796B657973706163650006706572736F6E00026964000D00046E616D65000D000361676500090004706963740003000000010000000A31323334353637383930000000066D69636B657900000004000000320000000E的 0001020304051122334455AACCFF
虽然插件在数据库本身看起来是正确的,如cqlsh命令行所示:
cqlsh:mykeyspace> select * from person;
id | age | name | pict
------------+-----+--------+--------------------------------
1234567890 | 50 | mickey | 0x0001020304051122334455aaccff
(1 rows)
答案 0 :(得分:6)
我有完全相同的问题,但幸运的是找到了解决方案。 问题是ByteBuffer的使用令人困惑。尝试做类似的事情:
ByteBuffer bb = resultSet.one().getBytes("column_name");
byte[] data = new byte[bb.remaining()];
bb.get(data);
感谢Sylvain在此提出的建议:http://grokbase.com/t/cassandra/user/134brvqzd3/blobs-in-cql
答案 1 :(得分:4)