Accessing column names with periods - Spark SQL 1.3
我有一个DataFrame,其中的字段包含一个句点。当我尝试在其上使用select()时,Spark无法解析它们,可能是因为"。"用于访问嵌套字段。
这是错误:
enrichData.select("google.com")
org.apache.spark.sql.AnalysisException: cannot resolve 'google.com' given input columns google.com, yahoo.com, ....
是否可以访问这些列?或一种简单的方式来更改列名(由于无法选择列名,如何更改名称?)。
在列名中包含句点会使spark将其假定为嵌套字段,即字段中的字段。为了解决这个问题,您需要使用反引号"`"。这应该工作:
1 2 3 4 5 6 7 8 9 10 | scala> val df = Seq(("yr", 2000), ("pr", 12341234)).toDF("x.y","e") df: org.apache.spark.sql.DataFrame = [x.y: string, e: int] scala> df.select("`x.y`").show +---+ |x.y| +---+ | yr| | pr| +---+ |
您需要添加反引号(`)
您可以删除架构并重新创建它,而无需使用以下句点:
1 2 3 4 5 6 | val newEnrichData = sqlContext.createDataFrame( enrichData.rdd, StructType(enrichData.schema.fields.map(sf => StructField(sf.name.replace(".",""), sf.dataType, sf.nullable) )) ) |