Created
February 28, 2019 23:29
-
-
Save erfelipe/3d8e35899f1866ecfa8a81ac509a6c0e to your computer and use it in GitHub Desktop.
Elasticsearch library Connecting to Bonsai
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://docs.bonsai.io/article/102-python | |
import os, base64, re, logging | |
from elasticsearch import Elasticsearch | |
# Log transport details (optional): | |
logging.basicConfig(level=logging.INFO) | |
# Parse the auth and host from env: | |
bonsai = os.environ['BONSAI_URL'] | |
auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':') | |
host = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '') | |
# Connect to cluster over SSL using auth for best security: | |
es_header = [{ | |
'host': host, | |
'port': 443, | |
'use_ssl': True, | |
'http_auth': (auth[0],auth[1]) | |
}] | |
# Instantiate the new Elasticsearch connection: | |
es = Elasticsearch(es_header) | |
# Verify that Python can talk to Bonsai (optional): | |
es.ping() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment