簡體   English   中英

如何創建嵌套字典以便通過 Python 為 Elasticsearch 創建映射?

[英]How to create nested dicts in order to create mapping for Elasticsearch by Python?

我正在嘗試使用 Loop 創建映射來處理嵌套字典。

我的映射表應該看起來像下面的值。

 { 
    "mapping": {
    "properties": {
        "clusterName": {
            "properties": {
                "infoAddr": { "type": "string" },
                "usedSpace": { "type": "string" },
                "capacity": { "type": "int" },
                "version": { "type": "string"},
                "used": { "type": "int"},
                "remaining": { "type" : "int"},
                "volfails": { "type": "int"}
            }

          }
      }
  }
}

這是我從 REST API 獲得的數據

{
    "test.mydomain_1.xyz:1019": {
                                "infoAddr":"x.x.x.x:1022",
                                "usedSpace":384635032546,
                                "capacity":30697676811776,
                                "version":"2.7.3.2.6.5.23-1",
                                "used":384635032546,
                                "remaining":30311575148182,
                                "volfails":0 },
    "test.mydomain_2.xyz:1019": {
                                "infoAddr":"x.x.x.x:1022",
                                "usedSpace":384635032546,
                                "capacity":30697676811776,
                                "version":"2.7.3.2.6.5.23-1",
                                "used":384635032546,
                                "remaining":30311575148182,
                                "volfails":0 }
}

現在我有清單

1. clusterName = ("test.mydomain_1.xyz:1019", "test.mydomain_2.xyz:1019",..."test.mydomain_n.xyz:1019")
2. Properties under properties field = ("infoAddr", "usedSpace",..."volfails")
3. Type of values from properties = ("str","str",..."int")

請建議我如何使用循環從這些數據創建映射,以便自動創建此映射。

謝謝

result = {}
for cluster_name, data in a.items():
    type_data = {key: {'type': type(value).__name__} for key, value in data.items()}
    result[cluster_name] = type_data

mapping = {"mapping": {"properties": result}}

因此,所提供數據的 output 將是:

{
    "mapping":{
        "properties": {
        "test.mydomain_1.xyz:1019": {
            "infoAddr": {"type": "str"}, 
            "usedSpace": {"type": "int"},
            "capacity": {"type": "int"}, 
            "version": {"type": "str"},
            "used": {"type": "int"}, 
            "remaining": {"type": "int"},
            "volfails": {"type": "int"}},
        "test.mydomain_2.xyz:1019": {
            "infoAddr": {"type": "str"}, 
            "usedSpace": {"type": "int"},
            "capacity": {"type": "int"},
            "version": {"type": "str"},
            "used": {"type": "int"}, 
            "remaining": {"type": "int"},
            "volfails": {"type": "int"}
            }
        }
    }
}

如果您想將 map 從 elasticsearch 日志中獲取到特定形式,我們打算在您在帖子中顯示的字典映射中,通常,您需要創建某種形式的遞歸訪問者,以便能夠將其用於具有任意嵌套的字典和如果可以應用映射,則有關字段類型的信息。

例如,您可以創建某種 function,如下所示:

def visit(data):
    data_mapping = {}
    for data_key, data_value in data.items():
        if isinstance(data_value,dict):
            if data_key not in data_mapping:
                data_mapping[data_key] = {'properties': {}}
            data_mapping[data_key]['properties'] = visit(data_value)
        else:
            data_mapping[data_key] = {
                'type': data_value.__class__.__name__
            }
    return data_mapping

接下來,調用 elasticsearch 日志字典上的 function:

mapping = visit(data)
mapping = {'mapping': { 'properties': mapping}}

因此,output 將是:

{'mapping': { 'properties': {
  'test.mydomain_1.xyz:1019': {
       'properties': {
             'infoAddr': {'type': 'str'},
             'usedSpace': {'type': 'int'},
             'capacity': {'type': 'int'},
             'version': {'type': 'str'},
             'used': {'type': 'int'},
             'remaining': {'type': 'int'},
             'volfails': {'type': 'int'}
        }
  },
 'test.mydomain_2.xyz:1019': {
         'properties': {
               'infoAddr': {'type': 'str'},
               'usedSpace': {'type': 'int'},
               'capacity': {'type': 'int'},
               'version': {'type': 'str'},
               'used': {'type': 'int'},
               'remaining': {'type': 'int'},
               'volfails': {'type': 'int'}
          }
  }}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM