【发布时间】:2018-08-31 04:20:57
【问题描述】:
使用 pyspark,我正在从一个文件夹 contentdata2 中读取多个包含一个 JSON 对象的文件,
df = spark.read\
.option("mode", "DROPMALFORMED")\
.json("./data/contentdata2/")
df.printSchema()
content = df.select('fields').collect()
df.printSchema() 产生的地方
root
|-- fields: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- field: string (nullable = true)
| | |-- type: string (nullable = true)
| | |-- value: string (nullable = true)
|-- id: string (nullable = true)
|-- score: double (nullable = true)
|-- siteId: string (nullable = true)
我希望访问fields.element.field,并存储每个等于body的字段,以及等于urlhash的字段(对于每个JSON对象)。
content的格式是一个Row(字段),包含其他Row,像这样:
[Row(fields=[Row(field=‘body’, type=None, value=’[“First line of text“,”Second line of text”]), Row(field='urlhash', type=None, value='0a0b774c21c68325aa02cae517821e78687b2780')]), Row(fields=[Row(field=‘body’, type=None, value=’[“First line of text“,”Second line of text”]), Row(field='urlhash', type=None, value='0a0b774c21c6caca977e7821e78687b2780')]), ...
“[Row(fields=[Row(field=....)”重新出现的原因是因为来自不同文件的 JSON 对象被合并到一个列表中。有还有很多我不感兴趣的其他 Row 元素,因此没有包含在示例中。
JSON 对象的结构如下所示:
{
"fields": [
{
"field": "body",
"value": [
"Some text",
"Another line of text",
"Third line of text."
]
},
{
"field": "urlhash",
"value": "0a0a341e189cf2c002cb83b2dc529fbc454f97cc"
}
],
"score": 0.87475455,
"siteId": "9222270286501375973",
"id": "0a0a341e189cf2c002cb83b2dc529fbc454f97cc"
}
我希望存储每个 url 正文中的所有单词,以便稍后删除停用词并将其提供给 K 最近邻算法。
如何解决为每个 url 存储正文中的单词的问题,最好是作为 tsv 或 csv 列 urlhash 和单词(这是正文中的单词列表)?
【问题讨论】:
标签: json dataframe pyspark row