2

我想将 stackexchange 原始数据处理成 BigQuery,但首先数据使用 7z 压缩格式,所以我解压缩数据以将其移植为 gz 格式,但内部文件是 xml。所以我需要将文件从xml转换为json。有任何想法吗?我使用 p7zip 解压缩并使用 xml2json 尝试移植 xml 文件但不起作用。

<?xml version="1.0" encoding="utf-8"?> <comments> <row Id="1" PostId="1" Score="3" Text="We need to all post more questions. Last time, we kinda &quot;rushed&quot; to get a w hole bunch of people to sign up at the last minute (and pulled some funny stuff" CreationDate="2014-02-12T01:01:14.257" UserId="52" />..

我用 xml2json xml2json -t json2xml -o xxx.xml yyy.json

使用 xml-json 的其他测试 **David 推荐

通过以下命令使用来自 stackoverflow.com-Users.7z 的此文件 Users.xml(大小 895M):xml-json Users.xml row > Users.json

xml-json Users.xml row > Users.json /usr/local/lib/node_modules/xml-json/node_modules/xml-nodes/index.js:19 this.soFar += String(chunk)
RangeError: Invalid string length
at XmlNodes._transform (/usr/local/lib/node_modules/xml-json/node_modules/xml-nodes/index.js:19:15)
at XmlNodes.Transform._read (_stream_transform.js:183:22)
at XmlNodes.Transform._write (_stream_transform.js:167:12)
at doWrite (_stream_writable.js:265:12)
at writeOrBuffer (_stream_writable.js:252:5)
at XmlNodes.Writable.write (_stream_writable.js:197:11)
at Duplexify._write (/usr/local/lib/node_modules/xml-json/node_modules/pumpify/node_modules/duplexify/index.js:197:22)
at doWrite (/usr/local/lib/node_modules/xml-json/node_modules/pumpify/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:237:10)
at writeOrBuffer (/usr/local/lib/node_modules/xml-json/node_modules/pumpify/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:227:5)
at Writable.write (/usr/local/lib/node_modules/xml-json/node_modules/pumpify/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:194:11)
at ReadStream.ondata (_stream_readable.js:539:20)
at ReadStream.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:162:16)
at ReadStream.Readable.push (_stream_readable.js:125:10)
at onread (fs.js:1581:12)
at Object.wrapper [as oncomplete] (fs.js:482:17)
4

2 回答 2

2

David M Smith 的回答是正确的,转换为 CSV 也可以。

经过几次尝试(并编辑了我的答案,因为我在回答之前没有彻底测试),我设法用一个像这样的小 Python 脚本创建了一个正确的 json 文件:

#!python
from __future__ import print_function
import sys
import fileinput
import xml
from xml.dom import minidom
import json

for line in fileinput.input():
        try:
                xmlDoc = minidom.parseString(line)
                print(json.dumps(dict(xmlDoc.childNodes[0].attributes.items())))
        except xml.parsers.expat.ExpatError:
                print("Unable to process line : ", line, file=sys.stderr)
        except KeyboardInterrupt:
                sys.exit(0)

然后您可能需要重新启动 shell 以更新路径(或任何其他方法)。

对于最大的文件,我之前需要拆分它们,因为 BigQuery 接受最大 4GB 的文件。这是完整的流程:

7z x -so ../orig/stackoverflow.com-Posts.7z 2> /dev/null | ./xmltojson.py > PostHistory3.json
split -e -d -C3G --additional-suffix=.json Posts.json Postssplit
ls Postssplit*.json | xargs -ifile gzip file 
gsutil cp Postssplit*.json.gz gs://YOURBUCKET
bq --project_id=YOURPROJECT load --source_format=NEWLINE_DELIMITED_JSON YOURDATASET.YOURTABLE gs://YOURBUCKET/Postssplit01.json,gs://YOURBUCKET/Postssplit03.json,gs://YOURBUCKET/Postssplit04.json,#ETCETERA 'Id:INTEGER,PostTypeId:INTEGER,AcceptedAnswerId:INTEGER,ParentId:INTEGER,CreationDate:TIMESTAMP,Score:INTEGER,ViewCount:INTEGER,Body:STRING,OwnerUserId:INTEGER,OwnerDisplayName:STRING,LastEditorUserId:INTEGER,LastEditorDisplayName:STRING,LastEditDate:TIMESTAMP,LastActivityDate:TIMESTAMP,Title:STRING,Tags:STRING,AnswerCount:INTEGER,CommentCount:INTEGER,FavoriteCount:INTEGER,ClosedDate:TIMESTAMP,CommunityOwnedDate:TIMESTAMP'

gsutil部分不是强制性的,但我更愿意将我的文件上传到 Cloud Storage 然后导入。这样,如果导入失败,我可以重试。

如果 Google 团队的某个人正在阅读,将其作为公共数据集将是非常棒的 :-)

请注意,这不适用于任何 XML,仅适用于与当前 Stack Exchange 导出格式类似的格式。

于 2014-10-02T05:37:50.630 回答
1

你真的需要 JSON 吗?除非您的数据在结构中是分层的,否则 CSV 可能会更快/更容易。

使用xml2csv-conv 之类的东西将数据转换为 CSV,然后使用 bq 命令行工具上传:

bq load mydataset.mytable mydata.csv "column1:string,column2:string ..."
于 2014-10-01T04:59:04.350 回答