gpt4 book ai didi

java - 找不到 CassandraSQLContext 键

转载 作者:行者123 更新时间:2023-11-30 11:01:40 26 4
gpt4 key购买 nike

谁能帮我解决这个错误

这是我在 cassandra 中的 table

CREATE TABLE zz("timestamp" timestamp, "sessionId" text, "userId" text, PRIMARY KEY ("userId", "sessionId", "timestamp"));

一些数据

INSERT INTO rr ("userId", "sessionId", "timestamp") VALUES ('1', '1', 1);

我将 Spark 与 Java 结合使用,通过 SQL 查询我的数据

SparkConf conf = new SparkConf(true).setMaster("local").setAppName("DatastaxTests").set("spark.cassandra.connection.host", "localhost");
SparkContext ctx = new SparkContext(conf);
SparkContextJavaFunctions functions = CassandraJavaUtil.javaFunctions(ctx);

CassandraSQLContext cassandraContext = new CassandraSQLContext(ctx);
cassandraContext.setKeyspace("crm360retail");

DataFrame userIdRDD = cassandraContext.sql("SELECT * FROM rr");
System.out.println("Data fetched: \n" + StringUtils.join(userIdRDD.collect(), "\n"));

但是当我执行代码时出现这个错误

java.util.NoSuchElementException: key not found: userId
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:58)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:58)
at org.apache.spark.sql.cassandra.CassandraTableScan$$anonfun$1$$anonfun$apply$1.apply(CassandraTableScan.scala:29)
at org.apache.spark.sql.cassandra.CassandraTableScan$$anonfun$1$$anonfun$apply$1.apply(CassandraTableScan.scala:29)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
at scala.collection.AbstractTraversable.map(Traversable.scala:105)
at org.apache.spark.sql.cassandra.CassandraTableScan$$anonfun$1.apply(CassandraTableScan.scala:29)
at org.apache.spark.sql.cassandra.CassandraTableScan$$anonfun$1.apply(CassandraTableScan.scala:28)
at scala.Function1$$anonfun$andThen$1.apply(Function1.scala:55)
at org.apache.spark.sql.cassandra.CassandraTableScan.inputRdd(CassandraTableScan.scala:48)
at org.apache.spark.sql.cassandra.CassandraTableScan.execute(CassandraTableScan.scala:99)
at org.apache.spark.sql.execution.SparkPlan.executeCollect(SparkPlan.scala:84)
at org.apache.spark.sql.DataFrame.collect(DataFrame.scala:887)
at com.worldline.ms.pcop.crm360.retail.tracking.service.dao.jpa.TrackingDaoImpl.getLastVisitedProduct(TrackingDaoImpl.java:37)
at com.worldline.ms.pcop.crm360.retail.tracking.service.functional.DivolteTrackingFunctionnalServiceImpl.getLastVisitedProduct(DivolteTrackingFunctionnalServiceImpl.java:35)
at com.worldline.ms.pcop.crm360.retail.tracking.service.business.DivolteTrackingBusinessServiceImpl.getLastVisitedProduct(DivolteTrackingBusinessServiceImpl.java:29)
at com.worldline.ms.pcop.crm360.retail.tracking.service.test.DivolteTrackingBusinessServiceTest.testGetLastVisitedProduct(DivolteTrackingBusinessServiceTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我知道问题是我的列名是区分大小写的,如果我使用这个查询 cassandraContext.sql("SELECT 'userId', timestamp FROM rr") 结果是

userIdRDD.collectAsList().get(0) = [userId,1970-01-01 01:00:00.001]

如何从区分大小写的表列中获取数据?

最佳答案

CQL3 将列名称视为不区分大小写。名称始终转换为小写。为了避免这种情况,您必须在 Create 查询中用双引号引用名称。如果您使用 Java 执行它,您的查询应如下所示:

CREATE TABLE zz("timestamp" timestamp, \"sessionId\" text, \"userId\" text, PRIMARY KEY (\"userId\", \"sessionId\", "timestamp"));

请注意引号被转义了。在这种情况下,userId 和 sessionId 将不会以小写形式保存。

希望对您有所帮助,

卓然

关于java - 找不到 CassandraSQLContext 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31068639/

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