Advertisement
surik00

Add smth to InfluxDB

Feb 2nd, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import json
  2. import logging
  3. import asyncio
  4. from datetime import datetime
  5. from aioinflux import AsyncInfluxDBClient
  6.  
  7.  
  8. logging.basicConfig(format=u'%(levelname)-8s [%(asctime)s] %(message)s',
  9.                     level=logging.INFO)
  10.  
  11. DB_NAME = 'testname'
  12.  
  13. TEST_TEXT = '{"user": 12345123, "message": "тестовый текст сообщения"}'
  14.  
  15.  
  16. def get_json_or_false(text):
  17.     try:
  18.         jsn = json.loads(text)
  19.     except ValueError as e:
  20.         logging.error(f'Error parsing json!:\n{e}')
  21.         return False
  22.     else:
  23.         return jsn
  24.  
  25.  
  26. async def process_new_data(text_line):
  27.     my_json = get_json_or_false(text_line)
  28.     if not my_json:
  29.         return
  30.     point = dict(time=datetime.now(),
  31.                  measurement='some_measurement_lol',
  32.                  fields=my_json)
  33.     client = AsyncInfluxDBClient(db=DB_NAME)
  34.     return await client.write(point)
  35.  
  36.  
  37. loop = asyncio.get_event_loop()
  38. loop.run_until_complete(process_new_data(TEST_TEXT))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement