【发布时间】:2017-08-08 08:05:28
【问题描述】:
我正在阅读此 StackOverFlow discussion 将 JSON 转换为 CSV 并且看起来很棒,但我无法让基本的 jq 工作.. 我不确定我做错了什么。我已经尝试了基本的东西,但我无法破解出什么问题。这是我在 Shell 脚本中的 ES 查询
curl -XGET 'http://es-1:9200/data_latest/customer/_search?pretty' -H 'Content-Type: application/json' -d'
{
"_source": ["customer_app_version", "customer_num_apps", "customer_name","app_disk_size_bytes","app_memory_capacity_bytes"],
"query": {
"bool": {
"must": [{
"term": {
"is_app_customer": {
"value": "true"
}
}
}]
}
},
"aggs": {
"Customer_UUID": {
"terms": {
"field": "customer_uuid",
"size": 100
}
}
}
}
' Shell 脚本输出
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 6171,
"max_score": 1.8510876,
"hits": [
{
"_index": "data_latest_v1",
"_type": "customer",
"_id": "0003245-4844-9015-1z2e-d4ae5234rd56",
"_score": 1.8510876,
"_source": {
"customer_app_version": "el7.20150513",
"customer_num_apps": 3,
"app_memory_capacity_bytes": 405248409600,
"customer_name": "Timbuktu Inc",
"app_disk_size_bytes": 25117047875604
}
},
{
"_index": "data_latest_v1",
"_type": "customer",
"_id": "0003245-4844-9015-1z2e-d4ae5234rd56",
"_score": 1.8510876,
"_source": {
"customer_app_version": "el4.20150513",
"customer_num_apps": 34,
"app_memory_capacity_bytes": 58923439600,
"customer_name": "Bunnies Inc",
"app_disk_size_bytes": 36517984275604
}
}
]
}
}
(被截断,但上面的子集在语法上是有效的)
- 如何在 shell 脚本中使用 jq 将 _source 字段(仅此而已)中的键和值作为 CSV 输出?我知道我在问其他讨论中描述的问题,但我尝试过但无法得到它
例如,我在我添加的 ' (上面脚本的结尾)之后添加 | jq -r '."customer_name"'
也试过了
| jq -r '.customer_name'
对于两者,我都会得到这样的输出。
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
103 13566 100 13566 0 346 507k 13248 --:--:-- --:--:-- --:--:-- 537k
null
我做错了什么?我需要做什么?如果有人可以在这里指导我,那将非常有帮助。
【问题讨论】:
-
你可能想抑制 curl 的状态输出,这样它就不会在这里复杂化。
-
另外,您提供的输出实际上并不是格式正确的 JSON——它缺少一些紧密的元素。请确保您提供的数据足够正确,以便人们测试他们的答案。
-
谢谢!我会确保它是有效的 Json。我确实指出输出被截断了。我仍在学习如何抑制 ES 的一些输出(例如:take、shards 等)