我正在尝试填充一个字典,以便稍后在另一个循环中的系统表的SQL查询生成的循环中使用。当我在嵌套字典中只有2个维度时,我没有遇到任何问题,但我需要再添加2个维度,而且我不确定如何从中填充字典。
我有4个维度:工作室,节点(此数字可能因工作室而异),音量(这是"路径"在查询中也可能有所不同)和存储(这可能会有所不同) 。我希望输出类似于以下内容:
{'studio1': {u'v_studio1_node0001': {u'/vertica/data': {u'CATALOG': n,
u'DATA,TEMP': n},
u'/vertica/data2': {u'DATA,TEMP': n}},
u'v_studio1_node0002': {u'/vertica/data': {u'CATALOG': n,
u'DATA,TEMP': n},
u'/vertica/data2': {u'DATA,TEMP': n}},
u'v_studio1_node0003': {u'/vertica/data': {u'CATALOG': n,
u'DATA,TEMP': n},
u'/vertica/data2': {u'DATA,TEMP': n}}},
'studio2': {u'v_studio2_node0001': {u'/vertica/data': {u'CATALOG': n,
u'DATA,TEMP': n}},
u'v_studio2_node0002': {u'/vertica/data': {u'CATALOG': n,
u'DATA,TEMP': n}}}}
以下只有studio和node_name,它是我在字典studio_node_names
中使用的2个维度。我添加了studio_node_volume
我希望填充所有4个维度,然后将数字作为值。
#Create dictionary to hold studio names and node names for each studio to be used for creating alarms
studio_node_names = {}
# Create dictionary to hold studio names, node names, volume, and storage for each studio to be used for creating alarms
#studio_node_volume = {}
for connection_blob in STUDIO_CONNECTION_INFO:
studio = connection_blob['studio']
connection_info = connection_blob['connection_info']
try:
conn = vertica_connect(connection_info)
# Query the v_monitor tables to get metrics for alarms
try:
disk_usage_result = conn.query("select su.node_name, su.path, du.storage_usage, su.usage_percent from v_monitor.storage_usage su join v_monitor.disk_storage du on INSTR(CONCAT(du.storage_path, '/'), CONCAT(su.path, '/'), 1, 1) = 1 and du.node_name = su.node_name where su.path not in('', '/') and (du.storage_usage = 'CATALOG' or du.storage_usage like '%DATA%')")
except (BotoClientError, BotoServerError, Exception) as e:
print "ERROR: Vertica Query exception message"
# Update the metrics derived from the row data returned from the query "disk_usage_result".
for row in disk_usage_result.rows:
# Update the cluster Disk Usage metric.
disk_usage = row['usage_percent']
node_name = row['node_name']
#volume = row['path']
#storage = row['storage_usage']
if studio_node_names.get(studio): # For looping over to create DiskUsage alarms later
studio_node_names[studio].append(node_name)
else:
studio_node_names[studio] = [node_name]
except (BotoClientError, BotoServerError, Exception) as e:
print "ERROR: Vertica Connection exception message"
finally:
if conn:
conn.close()
print "INFO: Done checks of Vertica instances."
for connection_blob in STUDIO_CONNECTION_INFO:
studio = connection_blob['studio']
# Create the DiskUsage alarm.
for node_name in studio_node_names[studio]:
try:
# Alarm code here
except (BotoClientError, BotoServerError, Exception) as e:
print "ERROR: DiskUsage create_alarm exception message"
"创建DiskUsage Alarms"的另一个connection_blob
循环我将在studio_node_volume
上循环,为每个工作室,节点,卷,存储组合创建警报。
答案 0 :(得分:0)
所以,我能够使用以下内容创建我期望的字典:
if studio not in studio_node_volume:
studio_node_volume[studio] = {}
if node_name not in studio_node_volume[studio]:
studio_node_volume[studio][node_name] = {}
if volume not in studio_node_volume[studio][node_name]:
studio_node_volume[studio][node_name][volume] = {}
if storage not in studio_node_volume[studio][node_name][volume]:
studio_node_volume[studio][node_name][volume][storage] = {}
studio_node_volume[studio][node_name][volume][storage] = disk_usage
答案 1 :(得分:0)
我实际上是用另一种方式创建了字典,所以我可以初始化键。我在for connection_blob in STUDIO_CONNECTION_INFO:
循环之前添加了以下内容:
class D(dict):
def __missing__(self, key):
result = self[key] = D()
return result
studio_node_volume = D()
然后在disk_usage_result.rows中的`for row下:'循环并在我拥有的行的四个变量之后:
studio_node_volume[studio]
studio_node_volume[studio][node_name][volume]
studio_node_volume[studio][node_name][volume][storage]
studio_node_volume[studio][node_name][volume][storage] = disk_usage