Created
August 3, 2016 09:55
-
-
Save Deepwalker/91155345fd4558fa582b4cafed33adb9 to your computer and use it in GitHub Desktop.
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
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