2017-10-07 1 views
1

나는 json 행으로 구성된 Dataset<String> ds이 있습니다.Json String 배열을 Spark 2.2.0의 특정 열의 데이터 집합으로 변환하는 방법은 무엇입니까?

샘플 JSON 행

[ 
    "{"name": "foo", "address": {"state": "CA", "country": "USA"}, "docs":[{"subject": "english", "year": 2016}]}", 
    "{"name": "bar", "address": {"state": "OH", "country": "USA"}, "docs":[{"subject": "math", "year": 2017}]}" 

] 

ds.printSchema() (이 데이터 세트에서 한 행의 단지 예입니다)

root 
|-- value: string (nullable = true) 

지금 나는 다음과 같은 데이터 세트로 변환 할 Spark 2.2.0을 사용하여

name |    address    | docs 
---------------------------------------------------------------------------------- 
"foo" | {"state": "CA", "country": "USA"} | [{"subject": "english", "year": 2016}] 
"bar" | {"state": "OH", "country": "USA"} | [{"subject": "math", "year": 2017}] 

Jav 하지만 스칼라 잘 한 자바 API에서 사용할 수있는 기능이 있기 때문에 또한

다음

지금까지

val df = Seq("""["{"name": "foo", "address": {"state": "CA", "country": "USA"}, "docs":[{"subject": "english", "year": 2016}]}", "{"name": "bar", "address": {"state": "OH", "country": "USA"}, "docs":[{"subject": "math", "year": 2017}]}" ]""").toDF 

df.show (false)를

|value                                                      | 
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
|["{"name": "foo", "address": {"state": "CA", "country": "USA"}, "docs":[{"subject": "english", "year": 2016}]}", "{"name": "bar", "address": {"state": "OH", "country": "USA"}, "docs":[{"subject": "math", "year": 2017}]}" ]| 

답변

1

내가 찾은 뭘하려 Java의 해결 방법. 이게 도움이 되길 바란다.

수입 아래로 (내 경우 TempBean)

import java.util.List; 
import java.util.Map; 

public class TempBean 
    { 
     String name; 
     Map<String, String> address; 
     List<Map<String, String>> docs; 
     public String getName() 
      { 
       return name; 
      } 
     public void setName(String name) 
      { 
       this.name = name; 
      } 
     public Map<String, String> getAddress() 
      { 
       return address; 
      } 
     public void setAddress(Map<String, String> address) 
      { 
       this.address = address; 
      } 
     public List<Map<String, String>> getDocs() 
      { 
       return docs; 
      } 
     public void setDocs(List<Map<String, String>> docs) 
      { 
       this.docs = docs; 
      } 

    } 

사용하여 다음 코드를 콩 클래스를 만듭니다

//import com.fasterxml.jackson.core.JsonGenerator; 
//import com.fasterxml.jackson.core.JsonParseException; 
//import com.fasterxml.jackson.core.JsonProcessingException; 
//import com.fasterxml.jackson.core.type.TypeReference; 
//import com.fasterxml.jackson.databind.JsonMappingException; 
//import com.fasterxml.jackson.databind.ObjectMapper; 

ObjectMapper mapper = new ObjectMapper(); 
List<String> dfList = ds.collectAsList(); //using your Dataset<String> 
List<TempBean> tempList = new ArrayList<TempBean>(); 
try 
    { 
     for (String json : dfList) 
      { 
      List<Map<String, Object>> mapList = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){}); 
      for(Map<String,Object> map : mapList) 
      { 
       TempBean temp = new TempBean(); 
       temp.setName(map.get("name").toString()); 
      temp.setAddress((Map<String,String>)map.get("address")); 
      temp.setDocs((List<Map<String,String>>)map.get("docs")); 
      tempList.add(temp); 
      } 
      } 
    } 
catch (JsonParseException e) 
    { 
     e.printStackTrace(); 
    } 
catch (JsonMappingException e) 
    { 
     e.printStackTrace(); 
    } 
catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

만들기 dataframe :

Dataset<Row> dff = spark.createDataFrame(tempList, TempBean.class); 

표시 데이터베이스

dff.show(false); 
+--------------------------------+---------------------------------------+----+ 
|address       |docs         |name| 
+--------------------------------+---------------------------------------+----+ 
|Map(state -> CA, country -> USA)|[Map(subject -> english, year -> 2016)]|foo | 
|Map(state -> OH, country -> USA)|[Map(subject -> math, year -> 2017)] |bar | 
+--------------------------------+---------------------------------------+----+ 

인쇄 스키마 :

dff.printSchema(); 
root 
|-- address: map (nullable = true) 
| |-- key: string 
| |-- value: string (valueContainsNull = true) 
|-- docs: array (nullable = true) 
| |-- element: map (containsNull = true) 
| | |-- key: string 
| | |-- value: string (valueContainsNull = true) 
|-- name: string (nullable = true) 
관련 문제