【问题标题】:How to use `select` within a jq --stream command?如何在 jq --stream 命令中使用 `select`?
【发布时间】:2022-12-09 23:52:32
【问题描述】:

我有一个非常大的 json 文档(~100 GB),我正在尝试使用 jq 来解析满足给定条件的特定对象。因为它太大了,我无法将它读入内存,需要使用 --stream 选项。

我了解如何运行 select 以在我不进行流式传输时提取我需要的内容,但可以使用一些帮助来弄清楚如何正确配置我的命令。

这是我名为example.json 的文档示例。

{
  "reporting_entity_name" : "INSURANCE COMPANY",
  "reporting_entity_type" : "INSURER",
  "last_updated_on" : "2022-12-01",
  "version" : "1.0.0",
  "in_network" : [ {
    "negotiation_arrangement" : "ffs",
    "name" : "ER VISIT",
    "billing_code_type" : "CPT",
    "billing_code_type_version" : "2022",
    "billing_code" : "99285",
    "description" : "HIGHEST LEVEL ER VISIT",
    "negotiated_rates" : [ {
      "provider_groups" : [ {
        "npi" : [ 111111111, 222222222],
        "tin" : {
          "type" : "ein",
          "value" : "99-9999999"
        }
      } ],
      "negotiated_prices" : [ {
        "negotiated_type" : "negotiated",
        "negotiated_rate" : 550.50,
        "expiration_date" : "9999-12-31",
        "service_code" : [ "23" ],
        "billing_class" : "institutional"
      } ]
    } ]
  }
]
}

我正在尝试获取 in_network 对象,其中 billing_code 等于 99285。

如果我能够在没有流式传输的情况下做到这一点,我将采用以下方法:

jq '.in_network[] | select(.billing_code == "99285")' example.json

非常感谢任何有关如何使用 --stream 选项配置它的帮助!

【问题讨论】:

  • 你想要整个in_network对象吗?请张贴精确的预期的输出,而不是猜测

标签: json select bigdata jq


【解决方案1】:

如果 .in_network 数组中的对象单独适合您的内存,请截断数组项(两层深):

jq --stream -n '
  fromstream(2|truncate_stream(inputs | select(.[0][0] == "in_network")))
  | select(.billing_code == "99285")
' example.json
{
  "negotiation_arrangement": "ffs",
  "name": "ER VISIT",
  "billing_code_type": "CPT",
  "billing_code_type_version": "2022",
  "billing_code": "99285",
  "description": "HIGHEST LEVEL ER VISIT",
  "negotiated_rates": [
    {
      "provider_groups": [
        {
          "npi": [
            111111111,
            222222222
          ],
          "tin": {
            "type": "ein",
            "value": "99-9999999"
          }
        }
      ],
      "negotiated_prices": [
        {
          "negotiated_type": "negotiated",
          "negotiated_rate": 550.5,
          "expiration_date": "9999-12-31",
          "service_code": [
            "23"
          ],
          "billing_class": "institutional"
        }
      ]
    }
  ]
}

【讨论】:

    【解决方案2】:

    你会发现 jq —stream 非常慢。由于 jq 旨在补充其他 shell 工具,我建议使用 jstream (https://github.com/bcicen/jstream),或我自己的 jm 或 jm.py (https://github.com/pkoppstein/jm),以“展开”数组,并将结果通过管道传递给 jq。

    例如。实现与 jq 过滤器相同的效果:

    jm —-pointer /in_network example.json | 
      jq 'select(.billing_code == "99285")' 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2022-12-11
      相关资源
      最近更新 更多