As a future graduate, I am on my first big data mission and I am facing a problem:
Code
//Loading my csv file here
val df = spark.read
.format("csv")
.option("header", "true")
.option("delimiter",";")
.load("/user/sfrtech/dilan/yesterdaycsv.csv")
.toDF()
//Select required columns
val formatedDf = df.select("`TcRun.ID`", "`Td.Name`", "`TcRun.Startdate`", "`TcRun.EndDate`", "`O.Sim.MsisdnVoice`", "`T.Sim.MsisdnVoice`", "`ErrorCause`")
//Sql on DF in order to get useful data
formatedDf.createOrReplaceTempView("yesterday")
val sqlDF = spark.sql("" +
" SELECT TcRun.Id, Td.Name, TcRun.Startdate, TcRun.EndDate, SUBSTR(O.Sim.MsisdnVoice,7,14) as MsisdnO, SUBSTR(T.Sim.MsisdnVoice,7,14) as MsisdnT", ErrorCause +
" FROM yesterday" +
" WHERE Td.Name like '%RING'" +
" AND MsisdnO is not null" +
" AND MsisdnT is not null" +
" AND ErrorCause = 'NoError'")
Getting error
Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve '
Td.Name
' given input columns: [TcRun.EndDate, TcRun.Startdate, O.Sim.MsisdnVoice, TcRun.ID, Td.Name, T.Sim.MsisdnVoice, ErrorCause]; line 1 pos 177;
I guess the problem come from my columns name that contains "." but I don't know how to solve this, even if i'm using backticks
Solution
val newColumns = Seq("id", "name", "startDate", "endDate", "msisdnO", "msisdnT", "error")
val dfRenamed = df.toDF(newColumns: _*)
dfRenamed.printSchema
// root
// |-- id: string (nullable = false)
// |-- name: string (nullable = false)
// |-- startDate: string (nullable = false)
// |-- endDate: string(nullable = false)
// |-- msisdnO: string (nullable = false)
// |-- msisdnT: string (nullable = false)
// |-- error: string (nullable = false)
This worked,
val sqlDF = spark.sql("" +
" SELECT 'TcRun.Id', 'Td.Name', 'TcRun.Startdate', 'TcRun.EndDate'", ErrorCause +
" FROM yesterday" +
" WHERE 'Td.Name' like '%RING'" +
" AND MsisdnO is not null" +
" AND MsisdnT is not null" +
" AND ErrorCause = 'NoError'")
When you have a .
character in your field name, use quotes in the select clause.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With