0
我想從數據框(scala語言)中選擇幾列。 問題是,我無法將所有列放入單個字符串並傳遞給DataFrame的選擇函數。我嘗試以下,但它不工作:Spark DataFrame AnalysisException
scala> val str1 = "sum(\"bal1\")/100,"
str1: String = sum("bal1")/100,
scala> val str2 = "sum(\"bal12\")/100,"
str1: String = sum("bal2")/100,
scala> val str3 = str1.concat(str2)
str3: String = sum("bal1")/100,sum("bal2")/100
peopleDataFrame.select(str3).show // Throws AnalysisException as mentioned below
scala> peopleDataFrame.select(str3).show
org.apache.spark.sql.AnalysisException: cannot resolve 'sum("bal1")/100,sum("bal2")/100' given input columns name, bal1, bal2;
完整的代碼片段:
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val people = sc.textFile("hdfs://quickstart.cloudera:8020/user/sekar/1.txt")
val schemaString = "name,bal1,bal2"
import org.apache.spark.sql.Row;
import org.apache.spark.sql.types.{StructType,StructField,IntegerType};
val schema =
StructType(
schemaString.split(",").map(fieldName => StructField(fieldName, IntegerType, true)))
val rowRDD = people.map(_.split(",")).map(p => Row(p(0).toString, p(1).toInt, p(2).toInt))
val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)
scala> val str1 = "sum(\"bal1\")/100,"
str1: String = sum("bal1")/100,
scala> val str2 = "sum(\"bal12\")/100,"
str1: String = sum("bal2")/100,
scala> val str3 = str1.concat(str2)
str3: String = sum("bal1")/100,sum("bal2")/100
peopleDataFrame.select(str3).show
STR3被正確解析爲「總和( 「BAL1」)/ 100,總和( 「BAL2」)/ 100 」。請讓我知道如何解決AnalysiseException。
如果需要更多信息,請讓我知道。提前致謝。
你可以給你的數據和期望的輸出的樣本?不清楚你在做什麼! – eliasah