gpt4 book ai didi

java - Cassandra Java 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:16 27 4
gpt4 key购买 nike

我尝试使用 Cassandra 数据库在 Java 中执行一个简单的请求,但我不明白我的示例它不起作用:

public class Test {

public static void main( String[] args )
{
Cluster cluster = Cluster.builder()
.addContactPoints("127.0.0.1")
.build();
Session session = cluster.connect("test");

ResultSet results = session.execute("SELECT * FROM datatest");
for ( Row row : results ) {
System.out.println(row.getString("name"));
}
}

}

当我执行我的代码时出现这个错误:

[main] INFO com.datastax.driver.core.policies.DCAwareRoundRobinPolicy - Using data-center name 'datacenter1' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /127.0.0.1:9042 added
Exception in thread "main" java.lang.IllegalArgumentException: name is not a column defined in this metadata
at com.datastax.driver.core.ColumnDefinitions.getAllIdx(ColumnDefinitions.java:273)
at com.datastax.driver.core.ColumnDefinitions.getFirstIdx(ColumnDefinitions.java:279)
at com.datastax.driver.core.ArrayBackedRow.getIndexOf(ArrayBackedRow.java:69)
at com.datastax.driver.core.AbstractGettableData.getString(AbstractGettableData.java:137)
at fakemillions.Test.main(Test.java:23)

这是我的数据库:

My database screenshot

最佳答案

听起来您还没有在表 datatest 中定义名称为“name”的列。我以前没有使用过 Datastax 驱动程序,但作为 documentationIllegalArgumentException 被抛出如果

name is not part of the ResultSet this row is part of, i.e. if !this.columns().names().contains(name)

最好提供datatest表的创建方式。

关于java - Cassandra Java 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28925863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com