gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:47:47 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com