0
我正在讀取具有標頭的輸入文件。 '點作爲DataFrame。我正試圖修剪該文件中的值。使用標頭中的點修剪數據幀值
樣品testCode:
val sqc = new SQLContext(getLocalSparkContext())
import sqc.implicits.localSeqToDataFrameHolder
import org.apache.spark.sql.functions._
val testDataFrame = Seq((" test data ", " dummy data ", " a multi word string ")).toDF("col1", "col. 2", "col3")
testDataFrame.withColumn("col. 2", trim(testDataFrame("`col. 2`"))).show()
testDataFrame.withColumn("col1", trim(testDataFrame("col1"))).show()
如果我嘗試執行上面的代碼,我得到
解決不了「關口。 2'給出了輸入列col1,col。 2,col3; org.apache.spark.sql.AnalysisException:無法解析'col。 2'給出 輸入列col1,col。 2,col3;
在這條線:testDataFrame.withColumn("col1", trim(testDataFrame("col1"))).show()
令人驚訝地,
testDataFrame.withColumn("col. 2", trim(testDataFrame("`col. 2`"))).show()
通行證。 我甚至試圖更換列如:
testDataFrame.withColumnRenamed("col. 2", "`col. 2`").show()
仍然收到相同的異常。對此有何幫助?
感謝Udi的評論。根據您提供的內容,我使用了 val testDataFrame = Seq((「test data」,「dummy data」,「multi word string」))。toDF(「col1」,「col。2」,「col3」) var transformedDF = testDataFrame.withColumn(「tmp」,trim(testDataFrame(「'col。2」)))。drop(「col。2」)。withColumnRenamed(「tmp」,「col。2」) transformedDF .column(「col1」,trim(testDataFrame(「col1」)))。show() 列的順序已更改,並且「col1」上的修剪失敗 – sriram
withColumn函數任何其他列失敗時,任何一個頭正在'。 」。因此,必須爲其他列執行相同的「tmp」列。希望這得到修復。 – sriram