Last active
April 23, 2019 19:11
-
-
Save Jammink2/92a4a3da7a506cc920f57e7986828042 to your computer and use it in GitHub Desktop.
This file contains 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
# loads json files into Elasticsearch | |
# borrowed heavily from http://carrefax.com/new-blog/2018/3/12/load-json-files-into-elasticsearch | |
import requests, json, os | |
from elasticsearch import Elasticsearch | |
# specify a directory | |
directory = str(os.getcwd()) | |
# connect to Elasticsearch; by default, ES listens on port 9200 | |
try: | |
res = requests.get('http://localhost:9200') | |
print(res.content) | |
es = Elasticsearch([{'host' : 'localhost', 'port' : '9200'}]) | |
except requests.exceptions.ConnectionError: | |
print("Can't connect to the port!") | |
pass | |
# create an index value object | |
i = 1 | |
#iterate over each JSON file and load it into Elasticsearch | |
for filename in os.listdir(directory): | |
if filename.endswith(".json"): | |
f = open(filename) | |
docket_content = f.read() | |
#send the data into es | |
es.index(index='myindex', ignore=400, doc_type='docket', id = i, body=json.loads(docket_content)) | |
i = i + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment