6

上下文:Spring data cassandra official 1.0.2.RELEASE from Maven Central repo, CQL3, cassandra 2.0, datastax driver 2.0.4

背景:cassandra blob 数据类型映射到 Java ByteBuffer。

下面的示例代码演示了在等效插入旁边使用 select 不会检索正确的字节。实际检索到的数据以大量垃圾字节为前缀,实际上看起来像是整行的序列化。这篇与 Cassandra 1.2 相关的较早帖子建议我们可能必须从长度为 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

[main] INFO org.spring.cassandra.example.CassandraApp - We got id=1234567890, name=mickey, age=50, pict=8200000800000073000000020000000100000004000A6D796B657973706163650006706572736F6E00026964000D00046E616D65000D000361676500090004706963740003000000010000000A31323334353637383930000000066D69636B657900000004000000320000000E 0001020304051122334455AACCFF

尽管插入在数据库本身中看起来是正确的,但从 cqlsh 命令行可以看出:

cqlsh:mykeyspace> select * from person;

 id         | age | name   | pict
------------+-----+--------+--------------------------------
 1234567890 |  50 | mickey | 0x0001020304051122334455aaccff

(1 rows)
4

2 回答 2

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

于 2014-09-10T16:07:31.010 回答
4

看一下 Datastax Java 驱动程序的Bytes类,它提供了对数据进行编码/解码所需的内容。

这篇文章中,我写了一个使用示例。

HTH,卡罗

于 2014-08-18T10:36:48.663 回答