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

发布于 2024-11-16 03:17:23 字数 2141 浏览 4 评论 0原文

我想从列族中获取所有行并显示所有列。我尝试了这个:

// 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());
        }
    }
}

问题是,例如,所有具有长值的列都是空的。如果我将最后一个“参数化器”更改为“对象”,则会收到“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)

I'd like to get all rows from a column family and display all columns as well. I tried this one:

// 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());
        }
    }
}

The problem is, that all the columns which have long-values for example are empty. If I change the last "parameterizer" to Object, I get a "CorruptedStreamException". How do I have to do this?

UPDATE: this is the output using 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-11-23 03:17:23

ML的答案:
http://groups.google.com/group/hector-users/browse_thread /thread/8246ad59c9d5cf9d

另外,请看一下新的模板包。它提供了更加明智的键入结果检索。这是使用此类的入门指南:
https://github.com/rantav/hector/wiki/入门-%285-分钟%29

The answer from the ML:
http://groups.google.com/group/hector-users/browse_thread/thread/8246ad59c9d5cf9d

Also, please take a look at the new template package. It provides significantly more sane retrieval of typed results. Here is a getting started guide utilizing such:
https://github.com/rantav/hector/wiki/Getting-started-%285-minutes%29

橘香 2024-11-23 03:17:23

您是否尝试过使用 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()));
}

并查看返回的类型(如果有)。

Have you tried using byte[] instead of Object? The underlying type of all Cassandra data items is a byte array. e.g.

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

This is based on guesswork from web examples, as I use a Python client rather than Java...

You could also try omitting the generics:

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

and see what types you get back, if any.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文