Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import csv
  2. import elasticsearch
  3. import random
  4.  
  5.  
  6. def get_data(filename):
  7. # Load and parse the CSV as a dict
  8. data = []
  9.  
  10. with open(filename) as f:
  11. reader = csv.reader(f, delimiter="\t")
  12.  
  13. rows = list(reader)
  14.  
  15. for x in range(0, 100):
  16. for row in rows:
  17. try:
  18. data.append({
  19. "name": row[0],
  20. "iterations": random.randrange(0, 1000),
  21. "speed": random.randrange(0, 10)
  22. })
  23. except IndexError:
  24. continue
  25.  
  26. return data
  27.  
  28.  
  29. def get_json_write_data(row):
  30. json = {
  31. "measurement": row["name"],
  32. "tags": {},
  33. "fields": {
  34. "iterations": row["iterations"],
  35. "speed": row["speed"]
  36. }
  37. }
  38.  
  39. return json
  40.  
  41.  
  42. def main():
  43. filename = "data.csv" # TODO: Read this from a command line argument
  44.  
  45. data = get_data(filename)
  46.  
  47. client = elasticsearch.Elasticsearch()
  48.  
  49. n = 0
  50. for row in data:
  51. print row
  52.  
  53. client.index(index='data', doc_type='blog', id=n, body=row)
  54. n += 1
  55.  
  56.  
  57. if __name__ == "__main__":
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement