如何将cassandra行转换为我的java类? [英] How to convert the cassandra row to my java class?

查看:398
本文介绍了如何将cassandra行转换为我的java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Here is [a Link](http://stackoverflow.com/questions/32448987/how-to-retrieve-a-very-big-cassandra-table-and-delete-some-unuse-data-from-it#comment52844466_32464409) of my question before.
 After I get  the cassandra data row by row in my program, I'm confused by the convert between cassandra row to java class. In java class the table of cassandra is convert to a ResultSet class,when I iterator it and get the row data,it returns a NPE. In fact,I can see the Object (or the data) while  debuging the program. Here is My Iterator Code:


        ResultSet rs=CassandraTools.getInstance().execute(cql);
        Iterator<Row> iterator = rs.iterator();
        while (iterator.hasNext()) {
            Row row = iterator.next();
            row.getString()  ---->return NPE

CassandraTools类:

The CassandraTools class is:

public class CassandraTools {

private static CassandraTools instance;

private CassandraTools() {
}

public static synchronized CassandraTools getInstance() {
    if (instance == null) {
        instance = new CassandraTools();
        instance.init();
    }
    return instance;
}

Cluster cluster;
Session session;

public void init() {
    if (cluster == null) {
        cluster = new Cluster.Builder().addContactPoint("10.16.34.96")
                .build();
        if (session == null) {
            session = cluster.connect("uc_passport");
        }
    }
}

public ResultSet execute(String cql) {
    ResultSet rs = session.execute(cql);
    // rs.forEach(n -> {
    // System.out.println(n);
    // });
    return rs;

}

}

SO如何将行中的数据转换为一个java类?我已经读取的转换类在spring数据cassandra的API,但它是复杂的为我使用。谁能帮助?

SO how could I convert the data in the row to A java Class?I have read the convert class in the API of spring data cassandra,but it is complicated to use for me. Who can help?

推荐答案

IMHO,如果你想将Cassandra的行映射到一个java类,

IMHO, If you want to map the rows of Cassandra to a java class, you should try to use an Object-Datastore mapper which does these things for you.

如果您自己尝试这样做,您需要处理java-cassandra数据类型映射,验证等所有

If you try to do this by yourself, you need to handle the java-cassandra datatype mappings, validations etc all by yourself which is very hectic job.

很少有(Kundera,Hibernate OGM等)开放源对象 - 数据存储映射器可用,你可以使用它们。建议您尝试 Kundera 并检查针对 开始使用 与Cassandra。

There are few (Kundera, Hibernate OGM, etc) opensource object-datastore mappers available and you can use them. I suggest you to try Kundera and check this for getting started with Cassandra.

这篇关于如何将cassandra行转换为我的java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆