_corrupt_record 将 JSON 文件读入 Spark 时出错
_corrupt_record error when reading a JSON file into Spark
我有这个 JSON 文件
{
"a": 1,
"b": 2
}
用Pythonjson.dump方法得到的。
现在,我想使用 pyspark 将此文件读入 Spark 中的 DataFrame。根据文档,我正在这样做
sc = SparkContext()
sqlc = SQLContext(sc)
df = sqlc.read.json('my_file.json')
print df.show()
打印语句吐出了这个:
+---------------+
|_corrupt_record|
+---------------+
| {|
| "a": 1, |
| "b": 2|
| }|
+---------------+
有谁知道发生了什么以及为什么它不能正确解释文件?
您需要在输入文件中每行有一个 json 个对象,请参阅 http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.DataFrameReader.json
如果您的 json 文件看起来像这样,它将为您提供预期的数据帧:
{ "a": 1, "b": 2 }
{ "a": 3, "b": 4 }
....
df.show()
+---+---+
| a| b|
+---+---+
| 1| 2|
| 3| 4|
+---+---+
加入@Bernhard 的精彩回答
# original file was written with pretty-print inside a list
with open("pretty-printed.json") as jsonfile:
js = json.load(jsonfile)
# write a new file with one object per line
with open("flattened.json", 'a') as outfile:
for d in js:
json.dump(d, outfile)
outfile.write('\n')
如果您想保留 JSON 文件原样(不删除新行字符 \n
),请包含 multiLine=True
关键字参数
sc = SparkContext()
sqlc = SQLContext(sc)
df = sqlc.read.json('my_file.json', multiLine=True)
print df.show()
在 Spark 2.2+ 中,您可以使用以下命令读取 json 多行文件。
val dataframe = spark.read.option("multiline",true).json( " filePath ")
如果每行有 json 个对象,那么,
val dataframe = spark.read.json(filepath)
我想分享我的经验,我有一个 JSON 列字符串,但带有 Python 符号,这意味着我有 None
而不是 null
,False
而不是 false
和 True
而不是 true
.
解析此列时,给我 returns 一个名为 _corrupt_record
的列。所以在解析 JSON 字符串之前我必须做的是用标准 JSON 表示法替换 Python 表示法:
df.withColumn("json_notation",
F.regexp_replace(F.regexp_replace(F.regexp_replace("_corrupt_record", "None", "null"), "False", "false") ,"True", "true")
在这个转换之后,我可以在 json_notation
列上使用函数 F.from_json()
并且在这里 Pyspark 能够正确解析 JSON 对象。
我有这个 JSON 文件
{
"a": 1,
"b": 2
}
用Pythonjson.dump方法得到的。 现在,我想使用 pyspark 将此文件读入 Spark 中的 DataFrame。根据文档,我正在这样做
sc = SparkContext()
sqlc = SQLContext(sc)
df = sqlc.read.json('my_file.json')
print df.show()
打印语句吐出了这个:
+---------------+
|_corrupt_record|
+---------------+
| {|
| "a": 1, |
| "b": 2|
| }|
+---------------+
有谁知道发生了什么以及为什么它不能正确解释文件?
您需要在输入文件中每行有一个 json 个对象,请参阅 http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.DataFrameReader.json
如果您的 json 文件看起来像这样,它将为您提供预期的数据帧:
{ "a": 1, "b": 2 }
{ "a": 3, "b": 4 }
....
df.show()
+---+---+
| a| b|
+---+---+
| 1| 2|
| 3| 4|
+---+---+
加入@Bernhard 的精彩回答
# original file was written with pretty-print inside a list
with open("pretty-printed.json") as jsonfile:
js = json.load(jsonfile)
# write a new file with one object per line
with open("flattened.json", 'a') as outfile:
for d in js:
json.dump(d, outfile)
outfile.write('\n')
如果您想保留 JSON 文件原样(不删除新行字符 \n
),请包含 multiLine=True
关键字参数
sc = SparkContext()
sqlc = SQLContext(sc)
df = sqlc.read.json('my_file.json', multiLine=True)
print df.show()
在 Spark 2.2+ 中,您可以使用以下命令读取 json 多行文件。
val dataframe = spark.read.option("multiline",true).json( " filePath ")
如果每行有 json 个对象,那么,
val dataframe = spark.read.json(filepath)
我想分享我的经验,我有一个 JSON 列字符串,但带有 Python 符号,这意味着我有 None
而不是 null
,False
而不是 false
和 True
而不是 true
.
解析此列时,给我 returns 一个名为 _corrupt_record
的列。所以在解析 JSON 字符串之前我必须做的是用标准 JSON 表示法替换 Python 表示法:
df.withColumn("json_notation",
F.regexp_replace(F.regexp_replace(F.regexp_replace("_corrupt_record", "None", "null"), "False", "false") ,"True", "true")
在这个转换之后,我可以在 json_notation
列上使用函数 F.from_json()
并且在这里 Pyspark 能够正确解析 JSON 对象。