How to escape column names with hyphen in Spark SQL

前端 未结 3 771
暗喜
暗喜 2020-12-20 11:50

I have imported a json file in Spark and convertd it into a table as

myDF.registerTempTable(\"myDF\")

I then want to run SQL queries on th

3条回答
  •  青春惊慌失措
    2020-12-20 12:39

    I cannot comment as I have less than 50 reps

    When you are referencing a json structure with struct.struct.field and there is a namespace present like:

    ns2:struct.struct.field the backticks(`) does not work.

    jsonDF = sqlc.read.load('jsonMsgs', format="json")
    jsonDF.registerTempTable("masterTable")
    sqlc.select("select `sn2:AnyAddRq.AnyInfo.noInfo.someRef.myInfo.someData.Name` AS sn2_AnyAddRq_AnyInfo_noInfo_someRef_myInfo_someData_Name from masterTable").show()
    

    pyspark.sql.utils.AnalysisException: u"cannot resolve 'sn2:AnyAddRq.AnyInfo.noInfo.someRef.myInfo.someData.Name'

    If I remove the sn2: fields, the query executes.

    I have also tried with single quote ('), backslash (\) and double quotes("")

    The only way it works if if I register another temp table on the sn2: strucutre, I am able access the fields within it like so

    anotherDF = jsonDF.select("sn2:AnyAddRq.AnyInfo.noInfo.someRef.myInfo.someData")
    anotherDF.registerTempTable("anotherDF")
    sqlc.select("select Name from anotherDF").show()
    

提交回复
热议问题