2016-08-30 108 views
0

我试图通过火花在Cassandra上运行查询。查询cassandra错误没有可行的替代输入'允许'

运行此命令:

val test = sc.cassandraTable[Person](keyspace,table) 
       .where("name=?","Jane").collect 

我得到的查询合适的输出。

当我尝试使用where语句作为整个字符串输入查询时,出现错误。

我收到的查询作为JSON:

{"clause": " name = 'Jane' "} 

然后把它变成一个字符串。

当运行

val query = (json \ "clause").get.as[String] 
//turns json value into a string 
val test = sc.cassandraTable[Person](keyspace,table) 
        .where(query).collect 

我收到以下错误:

java.io.IOException: Exception during preparation of SELECT "uuid", "person", "age" FROM "test"."users" WHERE token("uuid") > ? AND token("uuid") <= ? AND name = Jane ALLOW FILTERING: line 1:232 no viable alternative at input 'ALLOW' (...<= ? AND name = [Jane] ALLOW...) 
     at com.datastax.spark.connector.rdd.CassandraTableScanRDD.createStatement(CassandraTableScanRDD.scala:288) 
     at com.datastax.spark.connector.rdd.CassandraTableScanRDD.com$datastax$spark$connector$rdd$CassandraTableScanRDD$$fetchTokenRange(CassandraTableScanRDD.scala:302) 
     at com.datastax.spark.connector.rdd.CassandraTableScanRDD$$anonfun$18.apply(CassandraTableScanRDD.scala:328) 
     at com.datastax.spark.connector.rdd.CassandraTableScanRDD$$anonfun$18.apply(CassandraTableScanRDD.scala:328) 
     at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434) 
     at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440) 
     at com.datastax.spark.connector.util.CountingIterator.hasNext(CountingIterator.scala:12) 
     at scala.collection.Iterator$class.foreach(Iterator.scala:893) 
     at com.datastax.spark.connector.util.CountingIterator.foreach(CountingIterator.scala:4) 
     at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59) 

我怀疑,当我打开了JSON值“NAME ='简”成一个字符串,我失去了单因此我得到“name = Jane”,这当然会引发错误。我尝试用\单引号和第二对单引号围绕名称Jane {"clause": " name = ''Jane'' "}转义。它不能解决问题。

编辑:进一步测试后,它肯定是失去单引号的JSON,CQL需要它们执行查询。任何人都可以提出一种逃避/保存单引号的方式吗?我试着用\双单引号''转义。有没有办法使用JSON来提供适当的整个CQL语句?

+0

你已经确定了这个问题,如果你能通过你得到这些报价就没问题。我很确定他们被'.get.as [String]'删除了,所以我会仔细检查。我很确定'.where'不会修改你的字符串。 – RussS

+0

我实际上怀疑零件'WHERE token(“uuid”)>? AND token(“uuid”)<=? AND',因为它出现在where子句后面,我没有在我解析的字符串中包含它。但是,是的,在切换到文本以测试json是否是问题之后,事实证明是这样。谢谢! – Mnemosyne

+0

Np,“令牌”部分用于在Spark中进行工作分配。 :) – RussS

回答

相关问题