2

I am trying to implement this very same scenario in Python: https://neo4j.com/docs/developer-manual/current/cypher/clauses/create/#create-create-multiple-nodes-with-a-parameter-for-their-properties

What I am trying to do is to insert in a graph all links found in a wikipedia page as nodes but I am stuck at the creation step.

import wikipedia
from py2neo import Graph

first_page = "United Kingdom"

page = wikipedia.page(first_page)

page_name = page.title
page_id = page.pageid
links = page.links

graph = Graph(bolt=True, password="mypassword")

nodes = {}
nodes['Page'] = list({"title" : c} for c in page.links)
node = "UNWIND {json} as data CREATE (n) SET n = data"
graph.run(node, json=nodes)

If I print the nodes dictionary, it is in the very same format shown in the link above from the Neo4j documentation, below for reference.

{
  "props" : [ {
    "name" : "Andres",
    "position" : "Developer"
  }, {
    "name" : "Michael",
    "position" : "Developer"
  } ]
}

however in my case I receive the following error message:

py2neo.status.CypherTypeError: Property values can only be of primitive types or arrays thereof.

I am trying to find an way to create nodes with a single statement, is this even possible using Python?

bidi
  • 150
  • 1
  • 14
  • The issue is related to the fact that you are sending an array of json objects to Neo4j, which is not allowed. – Bruno Peres Oct 03 '17 at 12:49
  • Hi Bruno, I can see that, still I don't understand how could I implement the example on their documentation page using Python. – bidi Oct 03 '17 at 12:54

1 Answers1

0

The error is caused due to the json/object you are passing. UNWIND uses list only. Try passing "nodes['Page']" but not "nodes" like below.

import wikipedia
from py2neo import Graph

first_page = "United Kingdom"

page = wikipedia.page(first_page)

page_name = page.title
page_id = page.pageid
links = page.links

graph = Graph(bolt=True, password="mypassword")

nodes = {}
nodes['Page'] = list({"title" : c} for c in page.links)
node = "UNWIND {json} as data CREATE (n) SET n = data"
graph.run(node, json=nodes['Page'])

OR you can use python's neo4j-driver which is easier.

import wikipedia
from neo4j.v1 import GraphDatabase, basic_auth

driver = GraphDatabase.driver("bolt://localhost:5687",auth=basic_auth("neo4j","neo"))
session = driver.session()

first_page = "United Kingdom"

page = wikipedia.page(first_page)

page_name = page.title
page_id = page.pageid
links = page.links

nodes = list({"title" : c} for c in page.links)

query = "UNWIND {nodes} as data CREATE (n:Test) SET n = data;"

result = session.run(query,nodes=nodes)
print result 

Hope this helps!

techie95
  • 515
  • 3
  • 16