[英]Accessing Elasticsearch using python
我目前正在嘗試編寫腳本來豐富一些數據。 我已經編碼了一些可以與demodata txt文件配合使用的東西,但是現在我想嘗試直接從腳本中的服務器請求最新數據。
我正在使用的數據存儲在Elasticsearch上。 我已經收到一個URL,包括端口號。 我也有一個集群ID,一個用戶名和一個密碼。
我可以使用Kibana直接訪問數據,在控制台中(在“開發工具”下)在其中輸入以下內容:
GET /*projectname*/appevents/_search?pretty=true&size=10000
我可以將輸出復制到TXT文件中(實際上,它實際上是JSON數據),該文件目前已由我的腳本解析。 我希望直接收集數據而無需此中間步驟。 另外,我目前僅限於10000條記錄/事件,但我想全部獲得。
這有效:
res = requests.get('*url*:*port*',
auth=HTTPBasicAuth('*username*','*password*'))
print(res.content)
我正在為elasticsearch軟件包苦苦掙扎。 如何模仿腳本中上面列出的“ get”命令,以JSON格式收集所有內容?
已修復,從程序員那里得到了一些幫助。 存儲在列表中,因此我可以從那里開始使用它。 下面的代碼,標識信息已刪除。
es = Elasticsearch(
hosts=[{'host': '***', 'port': ***}],
http_auth=('***', '***'),
use_ssl=True
)
count = es.count(index="***", doc_type="***")
print(count) # {u'count': 244532, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}
# Use scroll to ease strain on cluster (don't pull in all results at once)
results = es.search(index="***", doc_type="***", size=1000,
scroll="30s")
scroll_id = results['_scroll_id']
total_size = results['hits']['total']
print(total_size)
# Save all results in list
dump = []
ct = 1
while total_size > 0:
results = es.scroll(scroll_id=scroll_id, scroll='30s')
dump += results['hits']['hits']
scroll_id = results['_scroll_id']
total_size = len(results['hits']['hits']) # As long as there are results, keep going ...
print("Chunk #", ct, ": ", total_size, "\tList size: ", len(dump))
ct += 1
es.clear_scroll(body={'scroll_id': [scroll_id]}) # Cleanup (otherwise Scroll id remains in ES memory)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.