使用Scala使用Json Play解析S3上的Json文件

时间:2019-01-25 05:34:11

标签: scala apache-spark amazon-s3 playframework play-json

我想使用json play fromework从S3访问json文件

val creds:DefaultAWSCredentialsProviderChain = new DefaultAWSCredentialsProviderChain
val s3Client = new AmazonS3Client(creds)
val uri: AmazonS3URI = new AmazonS3URI(conf_file)
val s3Object: S3Object = s3Client.getObject(uri.getBucket, uri.getKey)

val json = Json.parse(s3Object.getObjectContent)

val mylist = (json \ "mydata").get.as[List[JsValue]]


But this line gives an error no such element "mydata"

Can anyone tell how to access a json file and read its contents using json play in scala.

I am able to access same file from local machine, as well as fetch contents of "mydata" from within json

1 个答案:

答案 0 :(得分:0)

您是否尝试过先打印对象,并检查其格式是否正确设置为JSON,因为一切对我来说都很好。

val json: JsValue = Json.parse("""{
  "mydata": [
    {"first": "aa"},
    {"second": "bb"},
    {"third": "cc"}
  ]
}""")
(json \ "mydata").asOpt[Seq[JsValue]].getOrElse(None)

尝试实现类似这样的方法。