Connecting ElasticSearch by python code on remote node

I am hosting the ES server on one computer at my home and wants to push data from other computer remotely. In this case the remote server may or may not be available all the time so my remote python code crashes when ever the server is not reachable. Then I have restart the code.

es = Elasticsearch('http://elastic:changeme@192.168.43.2:9200/')
es.index(index="test", doc_type="string", body={"topic" : msg.topic, "dataString" : msg.payload, "timestamp": datetime.utcnow()})

How can I make it continuously running?

Hi,

you can just wrap this in a try - catch:

es = Elasticsearch('http://elastic:changeme@192.168.43.2:9200/')
try:
    es.index(index="test", doc_type="string", body={"topic" : msg.topic, "dataString" : msg.payload, "timestamp": datetime.utcnow()})
catch elasticsearch.exceptions.ConnectionError:
    # TODO: Handle the error appropriately for your situation
    pass

This will not terminate your process but it just drops the document on the floor if it could not connect to Elasticsearch and this is probably not what you want. So you'd need some kind of local buffer. This could be as simple as just adding the document to a Python list (in memory). When the document has been indexed successfully you can remove it from the list.

But obviously this is not persistent, i.e. if your process crashes or you terminate it before it got a chance to drain the local buffer, the documents are lost. If that is not ok for you then you need to make this persistent somehow, e.g. by writing to a file.

Daniel

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.