Вы можете получить схему, оценив строку, полученную при чтении json:
import json
from pyspark.sql.types import StructField, StringType, IntegerType, StructType
with open('test.json') as f:
data = json.load(f)
df = sqlContext.createDataFrame([], schema = eval(data['tediasessionclose_schema']))
print(df.schema)
выходы:
StructType(List(StructField(@timestamp,StringType,true),StructField(message,StructType(List(StructField(componentAddress,StringType,true),StructField(values,StructType(List(StructField(confNum,StringType,true),StructField(day,IntegerType,true))),true))),true)))
, где test.json
:
{"tediasessionclose_schema" : "StructType([ StructField('@timestamp', StringType()), StructField('message' , StructType([ StructField('componentAddress', StringType()), StructField('values', StructType([ StructField('confNum', StringType()), StructField('day', IntegerType())]))]))])"}
Надеюсь, это поможет!