Scala-如何调用RESTAPI并在HDFS中另存为json文件?
环境:Scala和spark 1.6 我正在尝试-Scala-如何调用RESTAPI并在HDFS中另存为json文件?,json,scala,rest,apache-spark,hdfs,Json,Scala,Rest,Apache Spark,Hdfs,环境:Scala和spark 1.6 我正在尝试- 1.通过RESTAPI调用获取json数据 2.以json文件的形式写入HDFS 3.将json文件转换为数据帧 val rawdata = "curl http://services.groupkt.com/state/get/USA/all"!! println(rawdata) // can see json output, but can't save as file in HDFS 我可以在屏幕上看到输出,但如何将原始数据的内容写
1.通过RESTAPI调用获取json数据
2.以json文件的形式写入HDFS 3.将json文件转换为数据帧
val rawdata = "curl http://services.groupkt.com/state/get/USA/all"!!
println(rawdata) // can see json output, but can't save as file in HDFS
我可以在屏幕上看到输出,但如何将原始数据的内容写入HDFSURL(hdfs://quickstart.cloudera:8020/user/hive/warehouse/test/)? 或者有没有办法不保存为文件而获取原始数据的内容?我还需要将json转换为dataframe
提前感谢侯赛因
val rawdata = "curl http://services.groupkt.com/state/get/USA/all"!!
println(rawdata)
一旦您有了数据
,您就可以使用此数据中的代码将其保存在Hadoop
中
创建数据帧:
假设您的json字符串如下所示:
{"time":"sometext1","host":"somehost1","event": {"category":"sometext2","computerName":"somecomputer1"}}
您可以从以下代码将json转换为数据帧
:
// Creating Rdd
val vals = sc.parallelize(
"""{"time":"sometext1","host":"somehost1","event": {"category":"sometext2","computerName":"somecomputer1"}}""" ::
Nil)
// Creating Schema
val schema = (new StructType)
.add("time", StringType)
.add("host", StringType)
.add("event", (new StructType)
.add("category", StringType)
.add("computerName", StringType))
import sqlContext.implicits._
val jsonDF = sqlContext.read.schema(schema).json(vals)
创建dataframe
后,您仍然可以选择使用lib或使用RDD上的saveAsTextFile
方法将其保存在hadoop
中。