有人可以帮我解决此错误
这是我在cassandra中的表
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 with 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]
如何从表格列中区分大小写?
答案 0 :(得分:1)
CQL3将列名称视为不区分大小写。名称始终转换为小写。为了避免这种情况,您必须在“创建查询”中双引号。如果您在Java中执行它,您的查询应如下所示:
CREATE TABLE zz("timestamp" timestamp, \"sessionId\" text, \"userId\" text, PRIMARY KEY (\"userId\", \"sessionId\", "timestamp"));
请注意引号已转义。在这种情况下,userId和sessionId不会以小写形式保存。
希望这有帮助,
卓然