[英]How to apply flatmap on JSONArray and convert to JSONObject in Spark?
I am working on spark java module, where I make a geocode api (batch request of 100 records).我正在研究 spark java 模块,我在其中进行地理编码 api(100 条记录的批量请求)。 Now for every request I will get JSONArray of JSONObject in one row like as below.现在,对于每个请求,我都会在一行中获得 JSONObject 的 JSONArray,如下所示。
Usually there will be 100 Address, but i gave only 2 addresses通常会有 100 个地址,但我只给了 2 个地址
Locations | Batch_Address
------------------------------------------
Address1,Address2 | [ { name:Address1,lat:12.89,lng:23.56} , { name:Address2,lat:12.3,lng:23.4} ]
What I required is like result as below我需要的结果如下
Address
----------
{ name:Address1,lat:12.89,lng:23.56}
{ name:Address2,lat:12.3,lng:23.4}
If your code deals with java8, which has java.util.Spliterator<T>
, so you can use it to create list from jsonarray.如果您的代码处理具有java.util.Spliterator<T>
的 java8,那么您可以使用它从 jsonarray 创建列表。
String type字符串类型
JavaRDD<String> JsonObject = json_Batch.javaRDD().flatMap(f -> {
String res = f.getAs("Batch_Address").toString();
JSONArray jsonArr = new JSONArray(res);
List<String> list = StreamSupport.stream(jsonArr.spliterator(), false)
.map(val -> val.toString())
.collect(Collectors.toCollection( ArrayList::new ));
return list.iterator();
});
JsonObject.foreach(f -> System.out.println(f));
JSONObject type JSON对象类型
JavaRDD<JSONObject> JsonObject = json_Batch.javaRDD().flatMap(f -> {
String res = f.getAs("Batch_Address").toString();
JSONArray jsonArr = new JSONArray(res);
List<JSONObject> list = StreamSupport.stream(jsonArr.spliterator(), false)
.map(val -> (JSONObject) val)
.collect(Collectors.toCollection( ArrayList::new ));
return list.iterator();
});
JsonObject.foreach(f -> System.out.println(f));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.