我如何让 jq 获取这样的 json:
{
"responseHeader":{
"status":0,
"QTime":1
},
"cluster":{
"collections":{
"collection1-stored":{
"pullReplicas":"0",
"configName":"collection1-stored-conf1",
"replicationFactor":1,
"router":{
"name":"compositeId"
},
"nrtReplicas":1,
"tlogReplicas":"0",
"shards":{
"shard1":{
"range":"80000000-7fffffff",
"state":"active",
"replicas":{
"core_node2":{
"core":"collection1-stored_shard1_replica_n1",
"node_name":"servername1:8983_solr",
"type":"NRT",
"state":"active",
"leader":"true",
"force_set_state":"false",
"base_url":"http://servername1:8983/solr"
}
},
"health":"GREEN"
}
},
"health":"GREEN",
"znodeVersion":3,
"creationTimeMillis":1727780823546
},
"collection2-multi":{
"pullReplicas":"0",
"configName":"collection2-multi-conf2",
"replicationFactor":2,
"router":{
"name":"compositeId"
},
"nrtReplicas":2,
"tlogReplicas":"0",
"shards":{
"shard1":{
"range":"80000000-7fffffff",
"state":"active",
"replicas":{
"core_node3":{
"core":"collection2-multi_shard1_replica_n1",
"node_name":"servername1:8983_solr",
"type":"NRT",
"state":"active",
"leader":"true",
"force_set_state":"false",
"base_url":"http://servername1:8983/solr"
},
"core_node4":{
"core":"collection2-multi_shard1_replica_n2",
"node_name":"servername2:8983_solr",
"type":"NRT",
"state":"active",
"force_set_state":"false",
"base_url":"http://servername2:8983/solr"
}
},
"health":"GREEN"
}
},
"health":"GREEN",
"znodeVersion":5,
"creationTimeMillis":1727780813821,
"aliases":["collection2-multi-query","collection2-multi-upd"]
},
"collection4-multi-multi1":{
"pullReplicas":"0",
"configName":"collection4-multi-multi-conf4",
"replicationFactor":2,
"router":{
"name":"compositeId"
},
"nrtReplicas":2,
"tlogReplicas":"0",
"shards":{
"shard1":{
"range":"80000000-ffffffff",
"state":"active",
"replicas":{
"core_node5":{
"core":"collection4-multi-multi1_shard1_replica_n2",
"node_name":"servername1:8983_solr",
"type":"NRT",
"state":"active",
"force_set_state":"false",
"base_url":"http://servername1:8983/solr"
},
"core_node8":{
"core":"collection4-multi-multi1_shard1_replica_n6",
"node_name":"servername2:8983_solr",
"type":"NRT",
"state":"active",
"leader":"true",
"force_set_state":"false",
"base_url":"http://servername2:8983/solr"
}
},
"health":"GREEN"
},
"shard2":{
"range":"0-7fffffff",
"state":"active",
"replicas":{
"core_node3":{
"core":"collection4-multi-multi1_shard2_replica_n1",
"node_name":"servername1:8983_solr",
"type":"NRT",
"state":"active",
"leader":"true",
"force_set_state":"false",
"base_url":"http://servername1:8983/solr"
},
"core_node7":{
"core":"collection4-multi-multi1_shard2_replica_n4",
"node_name":"servername2:8983_solr",
"type":"NRT",
"state":"active",
"force_set_state":"false",
"base_url":"http://servername2:8983/solr"
}
},
"health":"GREEN"
}
},
"health":"GREEN",
"znodeVersion":7,
"creationTimeMillis":1727780770838,
"aliases":["collection4-multi-multi"]
},
"collection3-multi":{
"pullReplicas":"0",
"configName":"collection3-multi-conf3",
"replicationFactor":2,
"router":{
"name":"compositeId"
},
"nrtReplicas":2,
"tlogReplicas":"0",
"shards":{
"shard1":{
"range":"80000000-7fffffff",
"state":"active",
"replicas":{
"core_node7":{
"core":"collection3-multi_shard1_replica_n1",
"node_name":"servername2:8983_solr",
"type":"NRT",
"state":"active",
"force_set_state":"false",
"base_url":"http://servername2:8983/solr"
},
"core_node5":{
"core":"collection3-multi_shard1_replica_n2",
"node_name":"servername1:8983_solr",
"type":"NRT",
"state":"active",
"leader":"true",
"force_set_state":"false",
"base_url":"http://servername1:8983/solr"
}
},
"health":"GREEN"
}
},
"health":"GREEN",
"znodeVersion":6,
"creationTimeMillis":1727780768553,
"aliases":["collection3-multi-index","collection3-multi-query"]
}
}
}
}
并生成以下输出:
collection1-stored, collection1-stored-conf1, shard1:core_node2, collection1-stored_shard1_replica_n1, servername1:8983_solr
collection2-multi, collection2-multi-conf2, shard1:core_node3, collection2-multi_shard1_replica_n1, servername1:8983_solr
collection2-multi, collection2-multi-conf2, shard1:core_node4, collection2-multi_shard1_replica_n2, servername2:8983_solr
collection4-multi-multi1, collection4-multi-multi-conf4, shard1:core_node5, collection4-multi-multi1_shard1_replica_n2, servername1:8983_solr
collection4-multi-multi1, collection4-multi-multi-conf4, shard1:core_node8, collection4-multi-multi1_shard1_replica_n6, servername2:8983_solr
collection4-multi-multi1, collection4-multi-multi-conf4, shard2:core_node3, collection4-multi-multi1_shard2_replica_n1, servername1:8983_solr
collection4-multi-multi1, collection4-multi-multi-conf4, shard2:core_node7, collection4-multi-multi1_shard2_replica_n4, servername2:8983_solr
collection3-multi, collection3-multi-conf3, shard1:core_node7, collection3-multi_shard1_replica_n1, servername2:8983_solr
collection3-multi, collection3-multi-conf3, shard1:core_node5, collection3-multi_shard1_replica_n2, servername1:8983_solr
使用 jq 格式化程序。
总而言之,我需要为每个集合名称、它的每个分片和每个副本获取一行,其中包含集合名称、configName 值、核心名称、核心值和 node_name 值。
我只能达到第一级过滤:
jq ".cluster.collections"
谢谢
假设您想要 CSV 输出:
... 在
script
哪里这本质上是一个三重循环,其中...
.cluster.collections
条目、设置$collection
和$config
集合的键以及.configName
集合的属性。$shard
分片的键。内部循环生成的数组被传递
@csv
以转换为 CSV 记录。如果您想使用逗号和空格作为输出字段的分隔符,请将其替换@csv
为join(", ")
。参见:
to_entries
。