Using python list as node properties in py2neo - neo4j

I have a list of urls:
urls = ['http://url1', 'http://url2', 'http://url3']
Mind you the list can have any number of entries including 0 (none). I want to create new node property for each url (list entry).
Example how the node will look like
(label{name='something', url1='http://url1', url2='http://url2'}, etc...)
It is possible to expand a dictionary with ** with the same effect I need but is there any way to do this with a list?

You can put your list in a dictionary and use this to create a node:
from py2neo import Node
urls = ['http://1', 'http://2']
props = {}
for i, url in enumerate(urls):
# get a key like 'url1'
prop_key = 'url' + str(i)
props[prop_key] = url
my_node = Node('Person', **props)
graph.create(my_node)

Related

How broadcast variables are used in dask parallelization

I have some code applying a map function on a dask bag. I need a lookup dictionary to apply that function and it doesn't work with client.scatter.
I don't know if I am doing the right things, because the workers starts, but they don't do anything. I have tried different configuration looking to different examples, but I can't get it to work. Any support will be appreciated.
I know from Spark, you define a broadcast variable and you access the content by variable.value inside the function you want to apply. I don't see the same with dask.
# Function to map
def transform_contacts_add_to_historic_sin(data,historic_dict):
raw_buffer = ''
line = json.loads(data)
if line['timestamp] > historic_dict['timestamp]:
raw_buffer = raw_buffer + line['vid']
return raw_buffer
# main program
# historic_dict is a dictionary previously filled, which is the lookup variable for map function
# file_records will be a list of json.dump getting from a S3 file
from distributed import Client
client = Client()
historic_dict_scattered = client.scatter(historic_dict, broadcast=True)
file_records = []
raw_data = s3_procedure.read_raw_file(... S3 file.......)
data = TextIOWrapper(raw_data)
for line in data:
file_records.append(line)
bag_chunk = db.from_sequence(file_records, npartitions=16)
bag_transform = bag_chunk.map(lambda x: transform_contacts_add_to_historic(x), args=[historic_dict_scattered])
bag_transform.compute()
If your dictionary is small you can just include it directly
def func(partition, d):
return ...
my_dict = {...}
b = b.map(func, d=my_dict)
If it's large then you might want to wrap it up in Dask delayed first
my_dict = dask.delayed(my_dict)
b = b.map(func, d=my_dict)
If it's very large then yes, you might want to scatter it first (though I would avoid this if things work out with either of the approaches above).
[my_dict] = client.scatter([my_dict])
b = b.map(func, d=my_dict)

Neo4j syntax for WITH statement

I am trying to auto generate a a text file that can be run to create the nodes and relationships for a Neoj4 Graph.
The text file is being created in two sections:
First the nodes are created in a For loop (6000 nodes) with a result like this:
create(SystemLogic_d6:FB {type:"SUB_DINT", instanceName:"d6", section:"SystemLogic"})
create(SystemLogic_d5:FB {type:"SUB_DINT", instanceName:"d5", section:"SystemLogic"})
create(SystemLogic_d7:FB {type:"ADD_DINT", instanceName:"d7", section:"SystemLogic"})
create(SystemLogic_d8:FB {type:"SEL", instanceName:"d8", section:"SystemLogic"})
Then in the next section of the text file relationships are created in another For loop wih a result like this:
MATCH (SystemLogic_d8:FB), (SystemLogic_d12:FB) WHERE SystemLogic_d8.instanceName = "d8" AND SystemLogic_d12.instanceName = "d12" CREATE (SystemLogic_d8)-[: c]->(SystemLogic_d12)
MATCH (SystemLogic_d17:FB), (SystemLogic_d18:FB) WHERE SystemLogic_d17.instanceName = "d17" AND SystemLogic_d18.instanceName = "d18" CREATE (SystemLogic_d17)-[: c]->(SystemLogic_d18)
MATCH (SystemLogic_d16:FB), (SystemLogic_d17:FB) WHERE SystemLogic_d16.instanceName = "d16" AND SystemLogic_d17.instanceName = "d17" CREATE (SystemLogic_d16)-[: c]->(SystemLogic_d17)
MATCH (SystemLogic_d11:FB), (SystemLogic_d5:FB) WHERE SystemLogic_d11.instanceName = "d11" AND SystemLogic_d5.instanceName = "d5" CREATE (SystemLogic_d11)-[: c]->(SystemLogic_d5)
This is giving the error WITH is required between CREATE and MATCH
I tried inserting a WITH in between as in this answer
Neo4j Cypher WITH is required between CREATE and MATCH:
Which gives a result like this:
MATCH (SystemLogic_d8:FB), (SystemLogic_d12:FB) WITH SystemLogic_d8,SystemLogic_d12 WHERE SystemLogic_d8.instanceName = "d8" AND SystemLogic_d12.instanceName = "d12" CREATE (SystemLogic_d8)-[: c]->(SystemLogic_d12)
MATCH (SystemLogic_d17:FB), (SystemLogic_d18:FB) WITH SystemLogic_d17,SystemLogic_d18 WHERE SystemLogic_d17.instanceName = "d17" AND SystemLogic_d18.instanceName = "d18" CREATE (SystemLogic_d17)-[: c]->(SystemLogic_d18)
MATCH (SystemLogic_d16:FB), (SystemLogic_d17:FB) WITH SystemLogic_d16,SystemLogic_d17 WHERE SystemLogic_d16.instanceName = "d16" AND SystemLogic_d17.instanceName = "d17" CREATE (SystemLogic_d16)-[: c]->(SystemLogic_d17)
MATCH (SystemLogic_d11:FB), (SystemLogic_d5:FB) WITH SystemLogic_d11,SystemLogic_d5 WHERE SystemLogic_d11.instanceName = "d11" AND SystemLogic_d5.instanceName = "d5" CREATE (SystemLogic_d11)-[: c]->(SystemLogic_d5)
MATCH (SystemLogic_FBI_1407:FB), (SystemLogic_FBI_1408:FB) WITH SystemLogic_FBI_1407,SystemLogic_FBI_1408 WHERE SystemLogic_FBI_1407.instanceName = "FBI_1407" AND SystemLogic_FBI_1408.instanceName = "FBI_1408" CREATE (SystemLogic_FBI_1407)-[: c]->(SystemLogic_FBI_1408)
But I still get the same error
I also tried putting the WITH statement after the create statement but that gives another error.
Are you able to import and run multiple node/relationships creation statements in this fashion?
It works fine for creating the nodes but I am new to using Neo4J / Cypher and I am not sure if it is my syntax that is incorrect or that you can't create multiple relatiionships in this fasion.
Thanks for your help
You need to separate the statements with a semicolon, Please refer following queries:
create(SystemLogic_d8:FB {type:"SEL", instanceName:"d8", section:"SystemLogic"});
create(SystemLogic_d9:FB {type:"SEL", instanceName:"d8", section:"SystemLogic"});
MATCH (SystemLogic_d2:FB), (SystemLogic_d21:FB) WHERE SystemLogic_d2.instanceName = "d8" AND SystemLogic_d21.instanceName = "d12" CREATE (SystemLogic_d2)-[: c]->(SystemLogic_d21);
MATCH (SystemLogic_d1:FB), (SystemLogic_d12:FB) WHERE SystemLogic_d1.instanceName = "d8" AND SystemLogic_d12.instanceName = "d12" CREATE (SystemLogic_d1)-[: c]->(SystemLogic_d12)
If you have only CREATE statements then there is no need to use semicolon it will work,
But if you are using MATCH and CREATE combined then you need to separate the statements with a semicolon.
#Raj answer is valid. However, as you are already capturing the nodes in your create statements, you do not need to perform a match on them to create relations.
Your file could then be :
create(SystemLogic_d6:FB {type:"SUB_DINT", instanceName:"d6", section:"SystemLogic"})
create(SystemLogic_d5:FB {type:"SUB_DINT", instanceName:"d5", section:"SystemLogic"})
create(SystemLogic_d7:FB {type:"ADD_DINT", instanceName:"d7", section:"SystemLogic"})
create(SystemLogic_d8:FB {type:"SEL", instanceName:"d8", section:"SystemLogic"})
CREATE (SystemLogic_d8)-[:c]->(SystemLogic_d6)
CREATE (SystemLogic_d7)-[:c]->(SystemLogic_d6)
CREATE (SystemLogic_d8)-[:c]->(SystemLogic_d5)

How can I get all posible paths between two nodes with Graphhopper

I'm trying to find all paths between 2 nodes by using graphhopper but there is something wrong with my code. When remove setAlgorith line, it returns only one path but when I add setAlgorithm(Parameters.Algorithms.ALT_ROUTE), it returns 0 path. Here is my code:
GraphHopper graphHopper = new GraphHopper().forServer();
graphHopper.load("E:\\graphhopper\\[asia_vietnam].osm-gh");
GHRequest req = new GHRequest(21.006399, 105.820359, 21.022409, 105.819163).
setAlgorithm(Parameters.Algorithms.ALT_ROUTE).
setLocale(Locale.US);
GHResponse rsp = graphHopper.route(req);
List<PathWrapper> paths = rsp.getAll();
System.out.println(paths.size());
According to this post, it should return all possible path but seems like it doesn't work.

How to convert py2neo.database.cursor class to a dictionary or list in python?

Using graph.run() py2neo v3 to connect to neo4j DB: How can I convert an instance of py2neo.database.Cursor class to a dictionary or list in python?
Was simple in py2neo v2 using py2neo.cypher.core.RecordList class which is what graph.cypher.execute() the equivalent to graph.run would have returned if using the previous version...
Looks like you could do with the data method:
http://py2neo.org/v3/database.html#py2neo.database.Cursor.data
This is designed for use with libraries like Pandas where you need to extract the entire result.
As pointed out by Nigel Small, you can use the .data() method to convert it into a list.
Here is how it works, let's take for example this line of code calling graph.run, whose result is saved into tags variable:
tags = graph.run(query)
the result is a py2neo.database.Cursor class (containing a dictionary):
tags
-------------------------------
['py2neo', 'python', 'neo4j']
And by applying the .data() method, it becomes:
tags = graph.run(query).data()
whose result is a list (containing a dictionary):
[{'tags': ['py2neo', 'python', 'neo4j']}]
You can just cast it to a list :
result = session.run("MATCH (n) RETURN n")
records = list(result)

InfluxDB templating query - referring to measuerments?

I'm trying to make templates for my dahboards, and I have problems when it comes to referring to measuerment names.
My variables:
$space = SHOW MEASUREMENTS
Then I would like a variable that contains only values from a specific $space, which is actually a MEASUREMENT:
$app = SHOW TAG VALUES WITH KEY = "Application" WHERE MEASUREMENT =~ /^$space$/
Here I get a message: Template variables could not be initialized: error parsing query: found MEASUREMENT, expected identifier, string, number, bool at line 1, char 48
In the official example it is like this, though it refers to another tag:
$datacenter = SHOW TAG VALUES WITH KEY = "datacenter"
$host = SHOW TAG VALUES WITH KEY = "hostname" WHERE "datacenter" =~ /^$datacenter$/
I cannot find any info how to refer to MEASUREMENTS which would work. WHERE, WITH, etc.. Maybe is it not possible at all?
I found only this in the official tutorial, but this is for keys, not values.
SHOW TAG KEYS [FROM <measurement_name>]
I actually figured it out:
SHOW TAG VALUES FROM /^$space$/ WITH KEY = "Application"

Resources