Elasticsearch(Elasticsearch Curator)Python API。获取ES索引及其大小的字典

时间:2016-09-06 12:16:28

标签: python api dictionary elasticsearch elasticsearch-curator

我可以通过以下代码获取列表中的所有ES索引:

from elasticsearch import Elasticsearch
es = Elasticsearch()

indices=es.indices.get_aliases().keys()
sorted(indices)

但是有可能获得像{'index1': '100gb', 'index2': '10gb', 'index3': '15gb'}这样的字典。所以我指的是带有索引名称和大小的dic。

2 个答案:

答案 0 :(得分:2)

我的变体:

import elasticsearch

client = elasticsearch.Elasticsearch()
all_indices = client.indices.stats(metric='store', human=True)['indices'].keys()
dic_indices = {}

for index in all_indices:
    size = client.indices.stats(metric='store', human=True)['indices'][index]['total']['store']['size']
    dic_indices[index] = size

print dic_indices

结果有下一个视图:

{u'haproxy-2016.09.02': u'1.6gb', u'haproxy-2016.09.03': u'827.3mb', u'marathon-2016.09.03': u'296.1mb', u'docker-2016-09-06': u'187.2mb', u'haproxy-2016.09.06': u'339.7mb', u'haproxy-2016.09.04': u'647.5mb', u'haproxy-2016.09.05': u'595.5mb'}

答案 1 :(得分:2)

Curator 4在IndexList初始化时提取了大部分索引元数据。如果重要的话,它是以字节为单位,而不是以人类可读的大小。

位于 IndexList.index_info [index_name] ['size_in_bytes']

http://curator.readthedocs.io/en/latest/objectclasses.html#indexlist

了解有关IndexList方法的更多信息
import elasticsearch
import curator
client = elasticsearch.Elasticsearch(host='127.0.0.1')

il = curator.IndexList(client)
print('{0}'.format(il.indices))
[u'topbeat-2016.09.01', ...]

print('{0}'.format(il.index_info['topbeat-2016.09.01'])
{'number_of_replicas': u'1', 'size_in_bytes': 706503044, 'number_of_shards': u'5', 'docs': 1629986, 'age': {'creation_date': 1472688002}, 'state': u'open', 'segments': 0}