【发布时间】:2019-10-20 12:41:33
【问题描述】:
我有一个无法更改的第三方工具生成的事件日志文件。因此,这个日志文件是一个巨大的 JSON 数组,其中赔率元素包含元数据,而对包含与元数据关联的正文消息。我希望能够根据元数据拆分文件,按主题将信息聚集在不同的文件中。
我正在 Windows 上处理这个项目,我正在尝试使用批处理文件和 JQ。
数组基本上是这样的:
[
{ "type": "abc123"},
{"name":"first component of type abc123"},
{ "type": "abc123"},
{"name":"second component of type abc123"},
{ "type": "def124"},
{"name":"first component of type def124"},
{ "type": "xyz999"},
{"name":"first component of type xyz999"},
{ "type": "abc123"},
{"name":"third component of type abc123"},
{ "type": "def124"},
{"name":"second component of type def124"},
{ "type": "abc123"},
{"name":"fifth component of type abc123"},
{ "type": "abc123"},
{"name":"sixth component of type abc123"},
{ "type": "def124"},
{"name":"third component of type def124"},
{ "type": "def124"},
{"name":"fourth component of type def124"},
{ "type": "abc123"},
{"name":"seventh component of type abc123"},
{ "type": "xyz999"},
{"name":"second component of type xyz999"}
...
]
我知道我只有 3 种类型,所以我想要归档的是为每种类型创建一个文件。类似:
第一个文件
{
"componentLog": {
"type": "abc123",
"information": [
"first component of type abc123",
"second component of type abc123",
"third component of type abc123",
...
]
}
}
第二个文件
{
"componentLog": {
"type": "def124",
"information": [
"first component of type def124",
"second component of type def124",
"third component of type def124",
...
]
}
}
第三个文件
{
"componentLog": {
"type": "xyz999",
"information": [
"first component of type xyz999",
"second component of type xyz999",
"third component of type xyz999",
...
]
}
}
我知道我可以用这个分隔元数据
jq.exe ".[] | select(.type==\"product\")" file.json
然后我尝试计算 index。但 index 只返回包含 select 语句的第一项的索引...所以我不知道如何解决这个问题...
【问题讨论】:
-
“巨大”有多大?能否将 JSON 文件读入内存(例如
jq empty file.json)? -
@peak 文件大小约为 5Gb,因此不能选择内存。我正在使用 JQ,因为 AFAIK 它确实为我传输了文件。
-
@shellter 谢谢你的反馈,但我不明白你的意思是减少样本数据...
-
如果日志文件对于内存来说太大了,那么很可能其中一个输出文件对于内存来说也太大了。如果是这种情况,那么使用单个 JSON 实体创建如此大的文件有什么意义?为什么不将日志文件分成三个,每个都有一个 JSON 实体流?
-
@peak 另一个组件将上传这些文件并将其转换为弹性搜索的输入。我无法控制整个工作流程,所以我必须提供其他团队需要的东西。
标签: json batch-file jq data-partitioning