0

여러 개의 json 파일이 있습니다. 아파치 스파크를 사용하여 구문 분석해야합니다. 그것은 중첩 된 키 초기화를 가지고있다. 중첩 된 키와 함께 모든 열을 인쇄해야합니다.Java에서 apache spark를 사용하여 json 파일에서 중첩 열을 얻는 방법

파일에도 중첩 키가 있습니다. 중첩 된 열 이름과 함께 모든 열 이름을 가져 오려고합니다. 어떻게 얻을 수 있니?

Age | Designation | Email   | Name  | City  | State 
------------------------------------------------------------ 
22 |Programmer |[email protected] | Vipin Suman| Ahmedabad |Gujarat 
: 나는 등의 데이터를 원하는

people.show(50, false); 

Age | Designation | Email   | Name  | Location 
------------------------------------------------------------ 
22 |Programmer |[email protected] | Vipin Suman|[Ahmedabad,Gujarat] 

: 나는이 결과를 얻고있다

{ 
    "Name":"Vipin Suman", 
    "Email":"[email protected]", 
    "Designation":"Programmer", 
    "Age":22 , 
    "location": 
      { 
      "City":"Ahmedabad", 
      "State":"Gujarat" 
      } 
} 

: 같은 파일에

String jsonFilePath = "/home/vipin/workspace/Smarten/jsonParsing/Employee/Employee-01.json,/home/vipin/workspace/Smarten/jsonParsing/Employee/Employee-02.json"; 

String[] jsonFiles = jsonFilePath.split(","); 

Dataset<Row> people = sparkSession.read().json(jsonFiles); 

JSON 구조 :

내가이 시도

또는 같은 : -

Age | Designation | Email   | Name  | Location 
--------------------------------------------------------------- 
22 |Programmer |[email protected] | Vipin Suman| Ahmedabad,Gujarat 

scema 내가 중첩 된 키를 찾을 수있는 방법을

{ 
    "Name":"Vipin Suman", 
    "Email":"[email protected]", 
"Designation":"Trainee Programmer", 
"Age":22 , 
"location": 
    {"City": 
      { 
      "Pin":324009, 
      "City Name":"Ahmedabad" 
      }, 
    "State":"Gujarat" 
    }, 
"Company": 
      { 
      "Company Name":"Elegant", 
      "Domain":"Java" 
      }, 
"Test":["Test1","Test2"] 

} 

다음이

root 
|-- Age: long (nullable = true) 
|-- Company: struct (nullable = true) 
| |-- Company Name: string (nullable = true) 
| |-- Domain: string (nullable = true) 
|-- Designation: string (nullable = true) 
|-- Email: string (nullable = true) 
|-- Name: string (nullable = true) 
|-- Test: array (nullable = true) 
| |-- element: string (containsNull = true) 
|-- location: struct (nullable = true) 
| |-- City: struct (nullable = true) 
| | |-- City Name: string (nullable = true) 
| | |-- Pin: long (nullable = true) 
| |-- State: string (nullable = true) 

및 JSON 구조처럼 보이게합니다. 및

+1

준비하십시오 : 그것은 다음과 같은 출력 줄 것이다

people.select("*", "location.*").drop("location").show 

입력 데이터 예를 들어, 당신은 무엇을했고 질문은 무엇입니까? –

답변

1

당신이 코드를 사용할 수 있습니다 위의 예상 형식으로 데이터를 표시하려면 적절한 formet 테이블을 보여

+---+-----------+-----------------+----------+---------+-------+ 
|Age|Designation|   Email|  Name|  City| State| 
+---+-----------+-----------------+----------+---------+-------+ 
| 22| Programmer|[email protected]|VipinSuman|Ahmedabad|Gujarat| 
+---+-----------+-----------------+----------+---------+-------+ 
+0

답장을 보내 주셔서 감사합니다. @himanshuIIITian. 한 가지 더 물어볼 수 있습니까? 내가 어떤 키가 중첩되어 있는지 어떻게 알 수없는 경우 어떻게 찾을 수 있습니까? 또는 여러 개의 중첩 된 열이있는 경우 어떻게 그 상황을 찾아 해결할 수 있습니다. –

+0

@Vpn_talent 데이터 프레임의 스키마를 모를 경우 중첩되었는지 여부를 알 수 없으므로 불가능합니다. – himanshuIIITian

+0

@Vpn_talent이 답변으로 문제가 해결 되었습니까? – himanshuIIITian