编程语言
首页 > 编程语言> > java – 读取cassandra中的行和反序列化问题

java – 读取cassandra中的行和反序列化问题

作者:互联网

我想从列族中获取所有行并显示所有列.我试过这个:

// Static import of HFactory!

// First, insert the data
Mutator<String> mutator = HFactory.createMutator(fKeyspace, fStringS);
mutator.insert("fahrer1", "Fahrer", createStringColumn("first", "John"));
mutator.insert("fahrer2", "Fahrer", createStringColumn("first", "Vorname"));
mutator.insert("fahrer2", "Fahrer", createStringColumn("second", "Nachname"));
mutator.
    addInsertion("fahrer3", "Fahrer",
            createColumn("first", "Firstname", fStringS,
                    fStringS)).
    addInsertion("fahrer3", "Fahrer",
            createColumn("second", "Lastname", fStringS,
                    fStringS)).
    addInsertion("fahrer3", "Fahrer",
            createColumn("age", 29L, fStringS, fLongS))
    .execute();

// Now select..
CqlQuery<String, String, String> cqlQuery =
   new CqlQuery<String, String, String>(fKeyspace,fStringS,fStringS,fStringS);
cqlQuery.setQuery("SELECT * FROM ColumnFamily");

QueryResult<CqlRows<String, String, String>> result = cqlQuery.execute();

CqlRows<String, String, String> rows = result.get();
for (Row<String, String, String> row : rows.getList()) {
    System.out.println(row.getKey() + ":");
    for(HColumn<String, Object>col : row.getColumnSlice().getColumns()) {
        System.out.println(col.getName() + " : " + col.getValue());
        }
    }
}

问题是,例如具有长值的所有列都是空的.如果我将最后一个“参数化器”更改为Object,则会出现“CorruptedStreamException”.我该怎么做?

更新:这是使用CLI的输出

[default@Autorennen] list Fahrer;
Using default limit of 100
-------------------
RowKey: fahrer1
=> (column=first, value=John, timestamp=1308392358211000)
-------------------
RowKey: fahrer2
=> (column=first, value=SecondUpdated, timestamp=1308392358350000)
=> (column=second, value=584e6163686e616d65, timestamp=1308392358284000)
-------------------
RowKey: fahrer3
=> (column=age, value=000000000000001d, timestamp=1308392358286002)
=> (column=first, value=Firstname, timestamp=1308392358286000)
=> (column=second, value=4c6173746e616d65, timestamp=1308392358286001)

解决方法:

你尝试过使用byte []而不是Object吗?所有Cassandra数据项的基础类型是字节数组.例如

QueryResult<CqlRows<String, String, byte[]>>

这是基于Web示例的猜测,因为我使用的是Python客户端而不是Java …

您也可以尝试省略泛型:

CqlResult result = executeQuery("select * from ColumnFamily");
for (CqlRow row : result.getRows()) {
    System.out.println(new String (row.getKey()));
}

并查看您获得的类型,如果有的话.

标签:hector,java,cassandra
来源: https://codeday.me/bug/20190903/1796502.html