2016-07-21 20 views
-1

ich Hivecontext bin mit einer JSON-Datei mit folgendem Code zu lesen:lesen Json Datei mit Apache Spark-

df = hive_context.read.json("/Users/duttaam/Downloads/test.json") 
df.registerTempTable("df"); 

standardmäßig Funken bestimmt das folgende Schema

root 
|-- id: string (nullable = true) 
|-- profiles: array (nullable = true) 
| |-- element: struct (containsNull = true) 
| | |-- app_id: string (nullable = true) 
| | |-- localytics: struct (nullable = true) 
| | | |-- attributes: struct (nullable = true) 
| | | | |-- ap: long (nullable = true) 
| | | | |-- app_version: string (nullable = true) 
| | | | |-- birthdate: string (nullable = true) 
| | | | |-- country: string (nullable = true) 
| | | | |-- device_timezone: string (nullable = true) 
| | | | |-- language: string (nullable = true) 
| | | | |-- last_session_date: string (nullable = true) 
| | | | |-- library_version: string (nullable = true) 
| | | | |-- os_version: string (nullable = true) 
| | | | |-- push_enabled: long (nullable = true) 
| | | | |-- total_sessions: long (nullable = true) 
| | | | |-- user_type: string (nullable = true) 

Meine Json wie sieht wie folgt aus

{ 
    "id": "dsdasdasdsd", 
    "profiles": [ 
    { 
     "attributes": { 
     "MDN": "eoe/W/5Ru1KAPDMQQ/wq\n/pu/tGRWpA==" 
     }, 
     "localytics": { 
     "attributes": { 
      "last_session_date": "2016-07-17", 
      "device_timezone": "-04:00", 
      "country": "us", 
      "language": "en", 
      "user_type": "known", 
      "city_name": "Indianapolis" 
     } 
     } 
    }, 
    { 
     "app_id": "sdas-c824fcf6-bbae-11e5-adasda-asasqwvz", 
     "attributes": { 
     "Automatic Backup User": "No" 
     }, 
     "localytics": { 
     "attributes": { 
      "last_session_date": "2016-07-17", 
      "os_version": "6.2.1", 
      "app_version": "16.2.19.1", 
      "library_version": "androida_3.7.0", 
      "ap": 1, 
      "custom_1": "Unknown (Not Logged In)", 
      "total_sessions": 4, 
      "birthdate": "2016-07-09", 
      "push_enabled": 1, 
      "user_type": "known", 
      "custom_0": "Unknown (Not Logged In)", 
      "seconds_since_last_session": 1457 
     } 
     } 
    } 
    ] 
} 

So nimmt Spark standardmäßig keine Attributfelder in beiden Profilen auf. Gibt es eine Möglichkeit, die Anzeigenstruktur zu ändern, um die Schema-Struktur zu ändern?

Vielen Dank im Voraus.

Grüße, Amit

+0

why -ive vote? Ist das keine gültige Frage? – Dutta

Antwort

1

Sie können versuchen, hivecontxt.jsonFile (infile) mit:

from pyspark import SparkContext 
from pyspark.sql import HiveContext 
import json 

sc = SparkContext() 
hive_contxt = HiveContext(sc) 

your_schema = hive_contxt.jsonFile(INFILE) 
your_schema.registerTempTable('YOUR TITLE') 

Sie auch hive_context.sql(YOUR QUERY).collect()

Sie auch Ihre json in den Speicher versuchen können mit Abfrage kann Dumping dann mit hive_context.jsonRDD(json_dumped_object)

def make_json_single_row(row, field_names): 
    row_lst = row.split(';') 
    return json.dumps(dict(zip(field_names, row_lst))) 

def make_json(rdd, field_names): 
    return rdd.map(lambda row: make_json_single_row(row, field_names) 

field_names = ['column1','column2','column3'] 
rdd = sc.textfile(infile) 
split_rdd = make_json(rdd, field_names) 
your_new_schema = hive_contxt.jsonRDD(split_rdd) 
0

Wenn Sie nur wollen, um Ihre profiles Spalte
in Ihrem Fall, können Sie dies tun (aber ich bin sicher nicht der beste Weg):

Java:

import org.apache.spark.sql.functions; 

DataFrame prof = df.select(functions.explode(df.col("profiles")).as("prof")); 
prof.select("prof.app_id", "prof.attributes.*", "prof.localytics.attributes.*"); 

, dass Sie gut zu wissen, erfordert Ihr JSON-Schema als Bedingung