Skip to content

Instantly share code, notes, and snippets.

@Deepwalker
Created August 3, 2016 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deepwalker/91155345fd4558fa582b4cafed33adb9 to your computer and use it in GitHub Desktop.
Save Deepwalker/91155345fd4558fa582b4cafed33adb9 to your computer and use it in GitHub Desktop.
import os
import csv
import types
import asyncio
import json
import aiohttp
async def feed_elastic():
exhausted = False
@asyncio.coroutine
def produce_elastic_commands(reader, num):
nonlocal exhausted
for index, row in enumerate(reader):
print(index)
yield b'{"create":{}}\n'
yield json.dumps(row).encode('utf-8')
yield b'\n'
if index > num:
return
exhausted = True
async with aiohttp.ClientSession() as session:
with open('llll_data.csv') as inp:
size = os.stat(inp.fileno()).st_size
reader = csv.DictReader(inp)
while True:
streamer = produce_elastic_commands(reader, 100000)
response = await session.post('http://localhost:9200/llll/sheet/_bulk', data=streamer)
if exhausted:
break
print(response)
await reponse
asyncio.get_event_loop().run_until_complete(feed_elastic())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment