从pyspark中的多行文件读取json文件

qojgxg4l  于 2021-05-24  发布在  Spark
关注(0)|答案(1)|浏览(570)

我在学习python中的spark。我有一个json文件,如下所示:

{
   "_class":"fdsfsdfsdfds",
   "n":"test ccorona",
   "fn":"ccorona",
   "ln":"ccorona",
   "un":"ccorona",
   "p":"line",
   "g":"ccorona",
   "l":"ccorona",
   "e":"ccorona",     
   },

只有这样的代码:

df = spark.read.json("1.json")
df.show()

当我运行显示我得到这个味精

pyspark.sql.utils.AnalysisException: Since Spark 2.3, the queries from raw JSON/CSV files are disallowed when the
referenced columns only include the internal corrupt record column
(named _corrupt_record by default). For example:
spark.read.schema(schema).json(file).filter($"_corrupt_record".isNotNull).count()
and spark.read.schema(schema).json(file).select("_corrupt_record").show().
Instead, you can cache or save the parsed results and then send the same query.
For example, val df = spark.read.schema(schema).json(file).cache() and then
df.filter($"_corrupt_record".isNotNull).count().;

df.printSchema()
root
 |-- _corrupt_record: string (nullable = true)

还有这个

df.select("fn")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/spark/python/pyspark/sql/dataframe.py", line 1421, in select
    jdf = self._jdf.select(self._jcols(*cols))
  File "/opt/spark/python/lib/py4j-0.10.9-src.zip/py4j/java_gateway.py", line 1304, in __call__
  File "/opt/spark/python/pyspark/sql/utils.py", line 134, in deco
    raise_from(converted)
  File "<string>", line 3, in raise_from
pyspark.sql.utils.AnalysisException: cannot resolve '`fn`' given input columns: [_corrupt_record];;
'Project ['fn]
+- Relation[_corrupt_record#168] json

如何在spark中读取多行json sqlcontext?

lrpiutwd

lrpiutwd1#

以下是如何阅读多行json:

df = spark.read.option("multiLine", "true").option("mode", "PERMISSIVE").json("1.json")

相关问题