Advertisement
Guest User

banco de dados em grafo

a guest
Apr 12th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. from neo4jrestclient.client import GraphDatabase
  2. import psycopg2
  3. import csv  
  4. db = GraphDatabase("http://127.0.0.1:7474",username="neo4j", password="1234")
  5. conn = psycopg2.connect("\
  6.   dbname='bdTrmmTest'\
  7.   user='postgres'\
  8.   host='127.0.0.1'\
  9.   password='1234'\
  10.   ");
  11. c = conn.cursor()
  12. c.execute("SELECT latitude, longitude, gid FROM pontos")
  13.  
  14. with open("arquivo_destino.csv","wt") as file_:
  15.     writer = csv.writer(file_)
  16.     writer.writerow(("latitude","longitude"))
  17.     records = True
  18.    
  19. e = conn.cursor()
  20. e.execute("SELECT precipitacaoh, gidgeo_fk FROM historico")
  21.  
  22. with open("arquivo_destino2.csv","wt") as file2_:
  23.     writer = csv.writer(file2_)
  24.     writer.writerow(("precipitacaoh","gidgeo_fk"))
  25.     records2 = True
  26.  
  27. sensorlatlong = db.labels.create("Latitude/Longitude")
  28. sensorprecip = db.labels.create("Precipitacao")
  29.  
  30. while records and records2:
  31.     records = c.fetchmany(100) #pega todos os resultados do select e armazena em uma tupla
  32.     writer.writerows(records)
  33.     records2 = e.fetchmany(100)
  34.     writer.writerows(records)
  35.     #cria todos os sensores os latitude e longitute
  36.     for j in records2:
  37.         s2 = db.nodes.create(precipitacao=j[0])
  38.         sensorprecip.add(s2)
  39.         for i in records:
  40.             if(i[2]==j[1]):
  41.                 q = 'MATCH (s:Latitude/Longitude) RETURN s'
  42.                 results = db.query(q, returns=(client.Node))
  43.                 if (results!=0):
  44.                     for r in results:
  45.                         if (r[0]["latitude"]==i[0]) and (r[0]["longitude"]==i[1]):
  46.                             r[0].relationships.create("MARCOU",s2)
  47.                         else:
  48.                             s = db.nodes.create(latitude=i[0],longitutde=i[1])
  49.                             s.relationships.create("MARCOU",s2)
  50.                 else:            
  51.                     s = db.nodes.create(latitude=i[0],longitutde=i[1])
  52.                     sensorlatlong.add(s)
  53.                     s.relationships.create("MARCOU",s2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement