带有百分比问题的spark-sql查询



我无法获得带有%符号的spark-sql查询。假设我有以下数据帧

val df = sc.parallelize(Seq(("Peter",123,23.5),("John",45,45.5)))
           .toDF("Name","Age","score(%)")
df.show

所以表格看起来是这样的:

+-----+---+--------+
| Name|Age|score(%)|
+-----+---+--------+
|Peter|123|    23.5|
| John| 45|    45.5|
+-----+---+--------+

我可以做:

sqlContext.sql("SELECT Name FROM df")

显示:

+-----+
| Name|
+-----+
|Peter|
| John|
+-----+

但当我这样做时:

sqlContext.sql("SELECT score(%) FROM df")

它抛出以下内容:(看起来问题是由%引起的,我尝试使用%,但没有帮助)

java.lang.RuntimeException: [1.14] failure: ``distinct'' expected but `%' found
SELECT score(%) FROM df
             ^
  at scala.sys.package$.error(package.scala:27)
  at org.apache.spark.sql.catalyst.AbstractSparkSQLParser.parse(AbstractSparkSQLParser.scala:36)
  at org.apache.spark.sql.catalyst.DefaultParserDialect.parse(ParserDialect.scala:67)
  at org.apache.spark.sql.SQLContext$$anonfun$3.apply(SQLContext.scala:175)
  at org.apache.spark.sql.SQLContext$$anonfun$3.apply(SQLContext.scala:175)
  at org.apache.spark.sql.SparkSQLParser$$anonfun$org$apache$spark$sql$SparkSQLParser$$others$1.apply(SparkSQLParser.scala:115)
  at org.apache.spark.sql.SparkSQLParser$$anonfun$org$apache$spark$sql$SparkSQLParser$$others$1.apply(SparkSQLParser.scala:114)
  at scala.util.parsing.combinator.Parsers$Success.map(Parsers.scala:137)
  at scala.util.parsing.combinator.Parsers$Success.map(Parsers.scala:136)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:237)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:237)
  at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:217)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1$$anonfun$apply$2.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1$$anonfun$apply$2.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$Failure.append(Parsers.scala:197)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:217)
  at scala.util.parsing.combinator.Parsers$$anon$2$$anonfun$apply$14.apply(Parsers.scala:882)
  at scala.util.parsing.combinator.Parsers$$anon$2$$anonfun$apply$14.apply(Parsers.scala:882)
  at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
  at scala.util.parsing.combinator.Parsers$$anon$2.apply(Parsers.scala:881)
  at scala.util.parsing.combinator.PackratParsers$$anon$1.apply(PackratParsers.scala:110)
  at org.apache.spark.sql.catalyst.AbstractSparkSQLParser.parse(AbstractSparkSQLParser.scala:34)
  at org.apache.spark.sql.SQLContext$$anonfun$2.apply(SQLContext.scala:172)
  at org.apache.spark.sql.SQLContext$$anonfun$2.apply(SQLContext.scala:172)
  at org.apache.spark.sql.execution.datasources.DDLParser.parse(DDLParser.scala:42)
  at org.apache.spark.sql.SQLContext.parseSql(SQLContext.scala:195)
  at org.apache.spark.sql.SQLContext.sql(SQLContext.scala:725)
  ... 48 elided

(当我被编程为spark使用spark csv摄取一大组csv时,就会遇到这个问题。当我尝试执行sql SELECT时,我遇到了这个%问题。如果可能的话,我希望避免修改标题…)

尝试使用回溯标记来分隔列名。

sqlContext.sql("SELECT `score(%)` FROM df")

相关内容

  • 没有找到相关文章

最新更新