How to convert json array<String> to csv in spark sql

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have tried this query to get required experience from linkedin data.

 Dataset<Row> filteredData = spark                     .sql("select full_name ,experience from (select *, explode(experience['title']) exp from tempTable )"                             + "  a where lower(exp) like '%developer%'"); 

But I got this error:

and finally I tried but I got more rows with the same name .

Dataset<Row> filteredData = spark                     .sql("select full_name ,explode(experience) from (select *, explode(experience['title']) exp from tempTable )"                             + "  a where lower(exp) like '%developer%'"); 

Please give me hint, how to convert array of string to comma separated string in the same column.

回答1:

You can apply UDF for making a comma separate string

Create UDF like this

def mkString(value: WrappedArray[String]): String = value.mkString(",") 

Register UDF in sparkSQL context

sqlContext.udf.register("mkstring", mkString _) 

Apply it on SparkSQL query

sqlContext.sql(select mkstring(columnName) from tableName) 

it will return comma separate value of array



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!