Reputation: 3609
I am trying to concat two columns with double quotes gets prefix and suffix at both these two columns . The code works ,but it gives me extra double quotes .
Input :
campaign_file_name_1, campaign_name_1, shagdhsjagdhjsagdhrSqpaKa5saoaus89, 1
campaign_file_name_1, campaign_name_1, sagdhsagdhasjkjkasihdklas872hjsdjk, 2
Expected Output :
campaign_file_name_1, shagdhsjagdhjsagdhrSqpaKa5saoaus89, "campaign_name_1"="1", 2017-06-06 17:09:31
campaign_file_name_1, sagdhsagdhasjkjkasihdklas872hjsdjk, "campaign_name_1"="2", 2017-06-06 17:09:31
Actual Output as Per code :
campaign_file_name_1, shagdhsjagdhjsagdhrSqpaKa5saoaus89, """campaign_name_1""=""1""", 2017-06-06 17:09:31
campaign_file_name_1, sagdhsagdhasjkjkasihdklas872hjsdjk, """campaign_name_1""=""2""", 2017-06-06 17:09:31
Spark Code :
object campaignResultsMergerETL extends BaseETL {
val now = ApplicationUtil.getCurrentTimeStamp()
val conf = new Configuration()
val fs = FileSystem.get(conf)
val log = LoggerFactory.getLogger(this.getClass.getName)
def main(args: Array[String]): Unit = {
//---------------------
code for sqlContext Initialization
//---------------------
val campaignResultsDF = sqlContext.read.format("com.databricks.spark.avro").load(campaignResultsLoc)
campaignResultsDF.registerTempTable("campaign_results")
val campaignGroupedDF = sqlContext.sql(
"""
|SELECT campaign_file_name,
|campaign_name,
|tracker_id,
|SUM(campaign_measure) AS campaign_measure
|FROM campaign_results
|GROUP BY campaign_file_name,campaign_name,tracker_id
""".stripMargin)
campaignGroupedDF.registerTempTable("campaign_results_full")
val campaignMergedDF = sqlContext.sql(
s"""
|SELECT campaign_file_name,
|tracker_id,
|CONCAT('\"',campaign_name, '\"','=','\"',campaign_measure,'\"'),
|"$now" AS audit_timestamp
|FROM campaign_results_full
""".stripMargin)
saveAsCSVFiles(campaignMergedDF,campaignResultsExportLoc,numPartitions)
}
def saveAsCSVFiles(campaignMeasureDF:DataFrame,hdfs_output_loc:String,numPartitions:Int): Unit =
{
log.info("saveAsCSVFile method started")
if (fs.exists(new Path(hdfs_output_loc))){
fs.delete(new Path(hdfs_output_loc), true)
}
campaignMeasureDF.repartition(numPartitions).write.format("com.databricks.spark.csv").save(hdfs_output_loc)
log.info("saveAsCSVFile method ended")
}
}
Could someone help me to fix this issue?
Upvotes: 1
Views: 11774
Reputation: 22449
It looks like you enclosed =
incorrectly in your CONCAT
arguments. Try:
|CONCAT('\"',campaign_name, '\"','=','\"',campaign_measure,'\"'),
[UPDATE]
Maybe your Spark version is different from mine, it seems to work as expected for me:
val df = Seq(("x", "y")).toDF("a", "b")
df.createOrReplaceTempView("df")
val df2 = spark.sqlContext.sql("""SELECT a, b, CONCAT('"', a, '"="', b, '"') as a_eq_b FROM df""")
df2.show
+---+---+-------+
| a| b| a_eq_b|
+---+---+-------+
| x| y|"x"="y"|
+---+---+-------+
df2.coalesce(1).write.option("header", "true").csv("/path/to/df2.csv")
/path/to/df2.csv content:
a,b,a_eq_b
x,y,"\"x\"=\"y\""
Now, you could optionally make quote null like in the following:
df2.coalesce(1).write.option("header", "true").option("quote", "\u0000").csv("/path/to/df2null.csv")
/path/to/df2null.csv content:
a,b,a_eq_b
x,y,"x"="y"
Note that, though, if you need to read the CSV back on Spark, make sure you include the same quote
option.
Upvotes: 2