2017-10-09 77 views
0

我有一個數據集,它看起來像這樣如何用另一個數據幀頭更改數據幀的標題?

LineItem.organizationId|^|LineItem.lineItemId|^|StatementTypeCode|^|LineItemName|^|LocalLanguageLabel|^|FinancialConceptLocal|^|FinancialConceptGlobal|^|IsDimensional|^|InstrumentId|^|LineItemSequence|^|PhysicalMeasureId|^|FinancialConceptCodeGlobalSecondary|^|IsRangeAllowed|^|IsSegmentedByOrigin|^|SegmentGroupDescription|^|SegmentChildDescription|^|SegmentChildLocalLanguageLabel|^|LocalLanguageLabel.languageId|^|LineItemName.languageId|^|SegmentChildDescription.languageId|^|SegmentChildLocalLanguageLabel.languageId|^|SegmentGroupDescription.languageId|^|SegmentMultipleFundbDescription|^|SegmentMultipleFundbDescription.languageId|^|IsCredit|^|FinancialConceptLocalId|^|FinancialConceptGlobalId|^|FinancialConceptCodeGlobalSecondaryId|^|FFAction|!| 
Japan|^|1507101869432|^|4295876606|^|1|^|BAL|^|Cash And Deposits|^|null|^|null|^|ACAE|^|false|^|null|^|null|^|null|^|null|^|false|^|null|^|null|^|null|^|null|^|505126|^|505074|^|null|^|null|^|null|^|null|^|null|^|null|^|null|^|3018759|^|null|^|I|!| 

這也是我如何裝載有自動數據發現模式

val df1With_ = df.toDF(df.columns.map(_.replace(".", "_")): _*) 
val column_to_keep = df1With_.columns.filter(v => (!v.contains("^") && !v.contains("!") && !v.contains("_c"))).toSeq 
val df1result = df1With_.select(column_to_keep.head, column_to_keep.tail: _*) 

現在,我有我做連接操作和最後的另一個數據幀我創建了一個將輸出寫入csv文件的數據框。

最終數據幀看起來像這樣

val dfMainOutputFinal = dfMainOutput.select($"DataPartition", $"StatementTypeCode",concat_ws("|^|", dfMainOutput.schema.fieldNames.filter(_ != "DataPartition").map(c => col(c)): _*).as("concatenated")) 

val dfMainOutputFinalWithoutNull = dfMainOutputFinal.withColumn("concatenated", regexp_replace(col("concatenated"), "null", "")) 

dfMainOutputFinalWithoutNull.write.partitionBy("DataPartition","StatementTypeCode") 
    .format("csv") 
    .option("nullValue", "") 
    .option("header","true") 
    .option("codec", "gzip") 
    .save("output") 

現在在我的輸出文件我僅concatenated預計看到我的頭。現在

我的問題是反正是有改變我爲df1result數據幀

回答

1

的頭部最終輸出的頭,我相信要解決這將是重命名concatenated列的最簡單方法。由於列名已經在column_to_keep變量存在,你可以簡單地做:

val header = column_to_keep.mkString("|^|") 
val dfMainOutputFinalWithoutNull = dfMainOutputFinal 
    .withColumn("concatenated", regexp_replace(col("concatenated"), "null", "")) 
    .withColumnRenamed("concatenated", header) 

這將導致是一個非常長的列名,因此,我不會勸告它,如果它是比保存到別的東西一個csv。

+0

嗨Shaido如果我不得不在'最後一列'只爲標題? – SUDARSHAN

+0

所以我的頭部最後一列應該看起來像這個'FFAction |!|'但是現在我變得像'FFAction |^|!' – SUDARSHAN

+0

@SUDARSHAN改變'mkString(「|^|」)''爲'mkString(「」 ,「|^|」,「|」)'。 – Shaido

相關問題