elasticsearch 如何在linux bash中使用jq从其他对象中拾取一个对象?

chhqkbe1  于 2023-01-01  发布在  ElasticSearch
关注(0)|答案(1)|浏览(117)

如何在Linux bash中使用jq从其他对象拾取一个对象?我有下面的数据使用下面的curl请求

kubectl exec -it elasticsearch-es-default-0 -- curl -s -X GET 'http://localhost:9200/_nodes/stats' | jq -r '.[]'

我需要在每个节点对象中获取jvm数据。每个单独的节点对象名称与其他两个不同。

{
    "SdMlVX-2RhOwI1JidEGD6Q": { ...},
    "2OA1JwIdR7WBUcQpp4lRbw": { ...},
    "IyuafpOTRsyVY_3Yvfv7CA": { ...}

实际Json数据输出。

{
  "total": 3,
  "successful": 3,
  "failed": 0
}
elasticsearch

{
    "SdMlVX-2RhOwI1JidEGD6Q": {
      "timestamp": 1672293619508,
      "name": "elasticsearch-es-default-0",
      "transport_address": "10.92.2.23:9300",
      "host": "10.92.2.23",
      "ip": "10.92.2.23:9300",
      "roles": [
        "master",
        "data",
        "ingest"
      ],
      "attributes": {
        "k8s_node_name": "gke-apisec-k8s-cluster-default-pool-84518d0d-p7bs",
        "ml.machine_memory": "12884901888",
        "xpack.installed": "true",
        "ml.max_open_jobs": "20",
        "ml.enabled": "true"
      },
      "indices": {
        "docs": {
          "count": 226556493,
          "deleted": 9692243
        },
        "store": {
          "size_in_bytes": 690487839009
        },
        "indexing": {
          "index_total": 11912389,
          "index_time_in_millis": 62183875,
          "index_current": 0,
          "index_failed": 0,
          "delete_total": 5388907,
          "delete_time_in_millis": 5729959,
          "delete_current": 0,
          "noop_update_total": 0,
          "is_throttled": false,
          "throttle_time_in_millis": 0
        },
        "get": {
          "total": 77959,
          "time_in_millis": 69555,
          "exists_total": 77959,
          "exists_time_in_millis": 69555,
          "missing_total": 0,
          "missing_time_in_millis": 0,
          "current": 0
        },
        "search": {
          "open_contexts": 7,
          "query_total": 27518458,
          "query_time_in_millis": 40763935,
          "query_current": 0,
          "fetch_total": 3853841,
          "fetch_time_in_millis": 4335315,
          "fetch_current": 0,
          "scroll_total": 352087,
          "scroll_time_in_millis": 21255042046,
          "scroll_current": 7,
          "suggest_total": 0,
          "suggest_time_in_millis": 0,
          "suggest_current": 0
        },
        "merges": {
          "current": 0,
          "current_docs": 0,
          "current_size_in_bytes": 0,
          "total": 750200,
          "total_time_in_millis": 121866303,
          "total_docs": 1369353645,
          "total_size_in_bytes": 1179583031536,
          "total_stopped_time_in_millis": 0,
          "total_throttled_time_in_millis": 2270735,
          "total_auto_throttle_in_bytes": 2733390502
        },
        "refresh": {
          "total": 49029221,
          "total_time_in_millis": 141861752,
          "listeners": 0
        },
        "flush": {
          "total": 36779,
          "periodic": 0,
          "total_time_in_millis": 2057816
        },
        "warmer": {
          "current": 0,
          "total": 8875256,
          "total_time_in_millis": 386604
        },
        "query_cache": {
          "memory_size_in_bytes": 92411542,
          "total_count": 73044526,
          "hit_count": 1845099,
          "miss_count": 71199427,
          "cache_size": 177923,
          "cache_count": 197051,
          "evictions": 19128
        },
        "fielddata": {
          "memory_size_in_bytes": 0,
          "evictions": 0
        },
        "completion": {
          "size_in_bytes": 0
        },
        "segments": {
          "count": 1268,
          "memory_in_bytes": 638683438,
          "terms_memory_in_bytes": 543675317,
          "stored_fields_memory_in_bytes": 85786816,
          "term_vectors_memory_in_bytes": 0,
          "norms_memory_in_bytes": 1419072,
          "points_memory_in_bytes": 6295565,
          "doc_values_memory_in_bytes": 1506668,
          "index_writer_memory_in_bytes": 0,
          "version_map_memory_in_bytes": 0,
          "fixed_bit_set_memory_in_bytes": 0,
          "max_unsafe_auto_id_timestamp": 1670925692307,
          "file_sizes": {}
        },
        "translog": {
          "operations": 531653,
          "size_in_bytes": 700342694,
          "uncommitted_operations": 13709,
          "uncommitted_size_in_bytes": 10536912,
          "earliest_last_modified_age": 0
        },
        "request_cache": {
          "memory_size_in_bytes": 87854,
          "evictions": 858614,
          "hit_count": 1350107,
          "miss_count": 3927915
        },
        "recovery": {
          "current_as_source": 0,
          "current_as_target": 0,
          "throttle_time_in_millis": 0
        }
      },
      "os": {
        "timestamp": 1672293620427,
        "cpu": {
          "percent": 37,
          "load_average": {
            "1m": 0.66,
            "5m": 1,
            "15m": 0.95
          }
        },
        "mem": {
          "total_in_bytes": 12884901888,
          "free_in_bytes": 745472,
          "used_in_bytes": 12884156416,
          "free_percent": 0,
          "used_percent": 100
        },
        "swap": {
          "total_in_bytes": 0,
          "free_in_bytes": 0,
          "used_in_bytes": 0
        },
        "cgroup": {
          "cpuacct": {
            "control_group": "/",
            "usage_nanos": 416863006426950
          },
          "cpu": {
            "control_group": "/",
            "cfs_period_micros": 100000,
            "cfs_quota_micros": 300000,
            "stat": {
              "number_of_elapsed_periods": 10990705,
              "number_of_times_throttled": 380054,
              "time_throttled_nanos": 40128444265111
            }
          },
          "memory": {
            "control_group": "/",
            "limit_in_bytes": "12884901888",
            "usage_in_bytes": "12884164608"
          }
        }
      },
      "process": {
        "timestamp": 1672293620427,
        "open_file_descriptors": 11471,
        "max_file_descriptors": 1048576,
        "cpu": {
          "percent": 3,
          "total_in_millis": 409005290
        },
        "mem": {
          "total_virtual_in_bytes": 17301680128
        }
      },
      "jvm": {
        "timestamp": 1672293620460,
        "uptime_in_millis": 1367981670,
        "mem": {
          "heap_used_in_bytes": 4476768256,
          "heap_used_percent": 41,
          "heap_committed_in_bytes": 10737418240,
          "heap_max_in_bytes": 10737418240,
          "non_heap_used_in_bytes": 148619104,
          "non_heap_committed_in_bytes": 183808000,
          "pools": {
            "young": {
              "used_in_bytes": 2516582400,
              "max_in_bytes": 0,
              "peak_used_in_bytes": 6438256640,
              "peak_max_in_bytes": 0
            },
            "old": {
              "used_in_bytes": 1926631424,
              "max_in_bytes": 10737418240,
              "peak_used_in_bytes": 4342874624,
              "peak_max_in_bytes": 10737418240
            },
            "survivor": {
              "used_in_bytes": 33554432,
              "max_in_bytes": 0,
              "peak_used_in_bytes": 208666720,
              "peak_max_in_bytes": 0
            }
          }
        },
        "threads": {
          "count": 92,
          "peak_count": 102
        },
        "gc": {
          "collectors": {
            "young": {
              "collection_count": 68766,
              "collection_time_in_millis": 2031266
            },
            "old": {
              "collection_count": 0,
              "collection_time_in_millis": 0
            }
          }
        },
        "buffer_pools": {
          "mapped": {
            "count": 0,
            "used_in_bytes": 0,
            "total_capacity_in_bytes": 0
          },
          "direct": {
            "count": 85,
            "used_in_bytes": 102368655,
            "total_capacity_in_bytes": 102368654
          },
          "mapped - 'non-volatile memory'": {
            "count": 0,
            "used_in_bytes": 0,
            "total_capacity_in_bytes": 0
          }
        },
        "classes": {
          "current_loaded_count": 17887,
          "total_loaded_count": 18101,
          "total_unloaded_count": 214
        }
      },
      "thread_pool": {
        "analyze": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "ccr": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "fetch_shard_started": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "fetch_shard_store": {
          "threads": 1,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 6,
          "completed": 276
        },
        "flush": {
          "threads": 2,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 2,
          "completed": 73518
        },
        "force_merge": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "generic": {
          "threads": 42,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 42,
          "completed": 4862767
        },
        "get": {
          "threads": 3,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 3,
          "completed": 77959
        },
        "index": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "listener": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "management": {
          "threads": 5,
          "queue": 0,
          "active": 1,
          "rejected": 0,
          "largest": 5,
          "completed": 10261366
        },
        "ml_autodetect": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "ml_datafeed": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "ml_utility": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "refresh": {
          "threads": 2,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 2,
          "completed": 102674430
        },
        "rollup_indexing": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "search": {
          "threads": 5,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 5,
          "completed": 49941262
        },
        "search_throttled": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "snapshot": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "warmer": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "watcher": {
          "threads": 0,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 0,
          "completed": 0
        },
        "write": {
          "threads": 3,
          "queue": 0,
          "active": 0,
          "rejected": 0,
          "largest": 3,
          "completed": 10325158
        }
      },
      "fs": {
        "timestamp": 1672293620461,
        "total": {
          "total_in_bytes": 2163350618112,
          "free_in_bytes": 1472063938560,
          "available_in_bytes": 1472047161344
        },
        "data": [
          {
            "path": "/usr/share/elasticsearch/data/nodes/0",
            "mount": "/usr/share/elasticsearch/data (/dev/sdc)",
            "type": "ext4",
            "total_in_bytes": 2163350618112,
            "free_in_bytes": 1472063938560,
            "available_in_bytes": 1472047161344
          }
        ],
        "io_stats": {
          "devices": [
            {
              "device_name": "sdc",
              "operations": 196909283,
              "read_operations": 105597899,
              "write_operations": 91311384,
              "read_kilobytes": 953149728,
              "write_kilobytes": 1831656916
            }
          ],
          "total": {
            "operations": 196909283,
            "read_operations": 105597899,
            "write_operations": 91311384,
            "read_kilobytes": 953149728,
            "write_kilobytes": 1831656916
          }
        }
      },
      "transport": {
        "server_open": 100,
        "rx_count": 140790208,
        "rx_size_in_bytes": 102209547554,
        "tx_count": 161364251,
        "tx_size_in_bytes": 130320986733
      },
      "http": {
        "current_open": 1,
        "total_opened": 288703
      },
      "breakers": {
        "request": {
          "limit_size_in_bytes": 6442450944,
          "limit_size": "6gb",
          "estimated_size_in_bytes": 0,
          "estimated_size": "0b",
          "overhead": 1,
          "tripped": 0
        },
        "fielddata": {
          "limit_size_in_bytes": 6442450944,
          "limit_size": "6gb",
          "estimated_size_in_bytes": 0,
          "estimated_size": "0b",
          "overhead": 1.03,
          "tripped": 0
        },
        "in_flight_requests": {
          "limit_size_in_bytes": 10737418240,
          "limit_size": "10gb",
          "estimated_size_in_bytes": 0,
          "estimated_size": "0b",
          "overhead": 1,
          "tripped": 0
        },
        "accounting": {
          "limit_size_in_bytes": 10737418240,
          "limit_size": "10gb",
          "estimated_size_in_bytes": 638697887,
          "estimated_size": "609.1mb",
          "overhead": 1,
          "tripped": 0
        },
        "parent": {
          "limit_size_in_bytes": 7516192768,
          "limit_size": "7gb",
          "estimated_size_in_bytes": 638697887,
          "estimated_size": "609.1mb",
          "overhead": 1,
          "tripped": 0
        }
      },
      "script": {
        "compilations": 2,
        "cache_evictions": 0
      },
      "discovery": {
        "cluster_state_queue": {
          "total": 0,
          "pending": 0,
          "committed": 0
        },
        "published_cluster_states": {
          "full_states": 1,
          "incompatible_diffs": 0,
          "compatible_diffs": 321
        }
      },
      "ingest": {
        "total": {
          "count": 0,
          "time_in_millis": 0,
          "current": 0,
          "failed": 0
        },
        "pipelines": {
          "xpack_monitoring_2": {
            "count": 0,
            "time_in_millis": 0,
            "current": 0,
            "failed": 0,
            "processors": [
              {
                "script": {
                  "count": 0,
                  "time_in_millis": 0,
                  "current": 0,
                  "failed": 0
                }
              },
              {
                "rename": {
                  "count": 0,
                  "time_in_millis": 0,
                  "current": 0,
                  "failed": 0
                }
              },
              {
                "set": {
                  "count": 0,
                  "time_in_millis": 0,
                  "current": 0,
                  "failed": 0
                }
              },
              {
                "gsub": {
                  "count": 0,
                  "time_in_millis": 0,
                  "current": 0,
                  "failed": 0
                }
              }
            ]
          },
          "xpack_monitoring_6": {
            "count": 0,
            "time_in_millis": 0,
            "current": 0,
            "failed": 0,
            "processors": []
          }
        }
      },
      "adaptive_selection": {
        "SdMlVX-2RhOwI1JidEGD6Q": {
          "outgoing_searches": 0,
          "avg_queue_size": 0,
          "avg_service_time_ns": 808900,
          "avg_response_time_ns": 924854,
          "rank": "0.9"
        },
        "IyuafpOTRsyVY_3Yvfv7CA": {
          "outgoing_searches": 0,
          "avg_queue_size": 0,
          "avg_service_time_ns": 677910,
          "avg_response_time_ns": 2779377,
          "rank": "2.8"
        },
        "2OA1JwIdR7WBUcQpp4lRbw": {
          "outgoing_searches": 0,
          "avg_queue_size": 0,
          "avg_service_time_ns": 934504,
          "avg_response_time_ns": 4083448,
          "rank": "4.1"
        }
      },
    "2OA1JwIdR7WBUcQpp4lRbw": { ...},
    "IyuafpOTRsyVY_3Yvfv7CA": { ...}  

}

我尝试下面的代码,但它抛出错误信息.

kubectl exec -it elasticsearch-es-default-0 -- curl -s -X GET 'http://localhost:9200/_nodes/stats' | jq -r '.[]|.jvm'

Error message:
null
jq: error (at <stdin>:0): Cannot index string with string "jvm"

那么,如何在linux bash中使用jq从一个对象中拾取另一个对象呢?

wixjitnu

wixjitnu1#

我需要在每个节点对象中获取jvm数据
因此,在访问jvm之前,您需要遍历这些对象:

.[].jvm

Jqplay Demo

相关问题