python - 如何创建嵌套字典,以便通过Python为Elasticsearch创建映射?

标签 python python-3.x elasticsearch

我正在尝试使用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}}

因此,所提供数据的输出为:
{
    "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"}
            }
        }
    }
}

关于python - 如何创建嵌套字典,以便通过Python为Elasticsearch创建映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58104864/

相关文章:

python - 使用 SQLAlchemy 动态获取列名

python - pyenv uninstall 是否会删除通过 pip 安装的所有软件包?

python - 为什么 cv2.rectangle 有时返回 np.ndarray,有时返回 cv2.UMat

elasticsearch - 引用不同索引中的对象-Elasticsearch

javascript - Elasticsearch - 如何使 JavaScript 客户端响应超过 10 项

python - 如何显示 Elasticsearch 中的所有记录。我能够从数据库Elastic Search中准确检索3条记录

python - 对于每个字符串,获得 3 个字符的唯一组合(一起)

Python:在功能上合并两个迭代器,其中一个迭代器是递归的

python - 使用 pip 安装 google-cloud 失败

Python C-API : Populate a Py_buffer from a C function