本文整理汇总了Python中py2neo.Graph.begin方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.begin方法的具体用法?Python Graph.begin怎么用?Python Graph.begin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py2neo.Graph
的用法示例。
在下文中一共展示了Graph.begin方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sync_meetup_data
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
def sync_meetup_data(group):
graph = Graph(host=config['neo4j']['host'], user=config['neo4j']['user'],
password=config['neo4j']['password'])
location = get_group_location(group)
tx = graph.begin()
location_node = Node('Location', city=location['city'], state=location['state'], country=location['country'])
tx.create(location_node)
tx.commit()
meetup_groups = get_groups_in_location(location, category=34)
logger.info('Finding upcoming meetup events at {} meetup groups'.format(len(meetup_groups)))
for group in meetup_groups:
time.sleep(2)
group, events = get_group_events(group)
tx = graph.begin()
group_node = Node("Group", name=group)
tx.create(group_node)
location_relation = Relationship(location_node, 'HAS MEETUP', group_node)
tx.create(location_relation)
for event in events:
event_node = Node('Event', name=event['name'], time=event['time'])
tx.create(event_node)
rel = Relationship(group_node, "HAS EVENT", event_node)
tx.create(rel)
tx.commit()
logger.info('Transaction ({}) status: {}'.format(group, str(tx.finished())))
示例2: handle
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
def handle(self, *args, **options): # pylint: disable=unused-argument
"""
Iterates through each course, serializes them into graphs, and saves
those graphs to neo4j.
"""
# first, make sure that there's a valid neo4j configuration
if settings.NEO4J_CONFIG is None:
raise CommandError(
"No neo4j configuration (NEO4J_CONFIG) defined in lms.auth.json."
)
auth_params = ["{host}:{https_port}", "{user}", "{password}"]
authenticate(*[param.format(**settings.NEO4J_CONFIG) for param in auth_params])
graph = Graph(**settings.NEO4J_CONFIG)
mss = ModuleStoreSerializer()
total_number_of_courses = len(mss.all_courses)
for index, course in enumerate(mss.all_courses):
# first, clear the request cache to prevent memory leaks
RequestCache.clear_request_cache()
log.info(
"Now exporting %s to neo4j: course %d of %d total courses",
course.id,
index + 1,
total_number_of_courses
)
nodes, relationships = mss.serialize_course(course.id)
log.info(
"%d nodes and %d relationships in %s",
len(nodes),
len(relationships),
course.id
)
transaction = graph.begin()
try:
# first, delete existing course
transaction.run(
"MATCH (n:item) WHERE n.course_key='{}' DETACH DELETE n".format(
six.text_type(course.id)
)
)
# now, re-add it
self.add_to_transaction(nodes, transaction)
self.add_to_transaction(relationships, transaction)
transaction.commit()
except Exception: # pylint: disable=broad-except
log.exception(
"Error trying to dump course %s to neo4j, rolling back",
six.text_type(course.id)
)
transaction.rollback()
示例3: expot_data
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
def expot_data(cid, data):
"""
将数据导入到neo4j,给每个导入的实体添加一个标签cid.
:param cid:
:param data:
:return:
"""
title = data[0]
host, http_port, bolt_port, user, password = 'localhost', 7474, 7687, 'neo4j', 'gswewf'
graph = Graph(host=host, http_port=http_port, bolt_port=bolt_port, user=user, password=password)
# title = ["_id", "_labels", "tagline", "title", "released", "name", "born", "_start", "_end", "_type", "roles"]
_start_index = title.index('_start')
node_property = title[2:_start_index]
relation_property = title[_start_index + 3:]
nodes = {}
relationships = []
tx = graph.begin()
for line in data[1:]:
_id, _labels = line[:2]
node_property_value = line[2:_start_index]
_start, _end, _type = line[_start_index:_start_index + 3]
relation_property_value = line[_start_index + 3:]
_labels = [label for label in _labels.strip().split(':') if label]
_labels.append(cid.capitalize())
# print(line)
# nodes = {"a": Node("person", name="weiyudang", age=13), "b": Node("person", name="wangjiaqi")}
if _id and not _start and not _end:
property_dict = {k: v for k, v in zip(node_property, node_property_value) if v}
_cid = "{}_{}".format(cid.lower(), _id)
updatetime = int(time.time() * 1000) # 与 neo4j的timestamp()一致
node = Node(*_labels, _cid=_cid, updatetime=updatetime, **property_dict)
# graph.merge(node)
nodes.setdefault(_cid, node)
tx.create(node)
elif not _id and _start and _end:
property_dict = {k: v for k, v in zip(relation_property, relation_property_value) if v}
start_cid = "{}_{}".format(cid.lower(), _start)
end_cid = "{}_{}".format(cid.lower(), _end)
# a = Node(_cid=start_cid)
# b = Node(_cid=end_cid)
a = nodes.get(start_cid)
b = nodes.get(end_cid)
a_knows_b = Relationship(a, _type, b, **property_dict)
# graph.merge(a_knows_b)
relationships.append(a_knows_b)
tx.create(a_knows_b)
else:
raise ValueError("数据有误: {}".format(line))
print(len(nodes), len(relationships))
# sub_graph = Subgraph(nodes=nodes, relationships=relationships)
# graph.create(sub_graph)
tx.commit()
示例4: BaseUploader
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
class BaseUploader(object):
__metaclass__ = abc.ABCMeta
def __init__(self, graph_url, file_to_process):
#watch("httpstream")
self.graph = Graph(graph_url)
self.setup(self.graph)
dir = os.path.dirname(os.path.dirname(__file__))
self.input_file = os.path.join(dir, file_to_process)
self.idx = 0
print('connected to graph db at : ' + str(self.graph))
@abc.abstractmethod
def setup(self, graph):
"""Process the file.
:rtype : None
"""
@abc.abstractmethod
def add_query(self, record, tx):
"""Process the file."""
return
def process(self):
"""Process the file."""
print('start processing')
with open(self.input_file, 'rt', encoding='utf-8') as infile:
reader = csv.DictReader(infile, quoting=csv.QUOTE_NONE)
tx = self.graph.begin()
for row in reader:
if self.idx % 1000 == 0 and self.idx != 0:
tx.commit()
tx = self.graph.begin()
print('commited 1000 rows till row:' + str(self.idx))
self.add_query(row, tx)
self.idx += 1
tx.commit()
示例5: Neo4j
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
class Neo4j():
def __init__(self, host='localhost:7474', username='neo4j', password='neo4j'):
if not has_py2neo:
raise Exception('py2neo is required, please install: pip install py2neo')
authenticate(host, username, password)
self.graph = Graph("http://{}/db/data/".format(host))
def load_events_directory(self, directory):
self.events = []
for path in glob.glob(os.path.join(directory, '*.json')):
e = MISPEvent()
e.load(path)
self.import_event(e)
def del_all(self):
self.graph.delete_all()
def import_event(self, event):
tx = self.graph.begin()
event_node = Node('Event', uuid=event.uuid, name=event.info)
# event_node['distribution'] = event.distribution
# event_node['threat_level_id'] = event.threat_level_id
# event_node['analysis'] = event.analysis
# event_node['published'] = event.published
# event_node['date'] = event.date.isoformat()
tx.create(event_node)
for a in event.attributes:
attr_node = Node('Attribute', a.type, uuid=a.uuid)
attr_node['category'] = a.category
attr_node['name'] = a.value
# attr_node['to_ids'] = a.to_ids
# attr_node['comment'] = a.comment
# attr_node['distribution'] = a.distribution
tx.create(attr_node)
member_rel = Relationship(event_node, "is member", attr_node)
tx.create(member_rel)
val = Node('Value', name=a.value)
ev = Relationship(event_node, "has", val)
av = Relationship(attr_node, "is", val)
s = val | ev | av
tx.merge(s)
#tx.graph.push(s)
tx.commit()
示例6: Graph
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
from py2neo import Graph, Node, Relationship
g = Graph()
tx = g.begin()
a = Node("Person", name="Alice")
tx.create(a)
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
tx.create(ab)
tx.commit()
g.exists(ab)
"""
Sample Query
>>> from py2neo import Graph
>>> g = Graph()
>>> g.run("MATCH (a) WHERE a.name={x} RETURN a.name", x="Bob").evaluate()
u'Bob'
>>>
"""
示例7: MERGE
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
UNWIND {categories} AS category
MERGE (c:Category {name: category})
MERGE (b)-[:IS_IN]->(c)
'''
merge_category_query = '''
MATCH (b:Business {id: {business_id}})
MERGE (c:Category {name: {category}})
CREATE UNIQUE (c)<-[:IS_IN]-(b)
'''
print("Beginning business batch")
with open('data/yelp_academic_dataset_business.json', 'r') as f:
tx = db.begin()
count = 0
for b in (json.loads(l) for l in f):
tx.run(create_business_query, b)
count += 1
if count >= 10000:
tx.commit()
tx = db.begin()
print("Committing transaction")
count = 0
if count > 0:
tx.commit()
print("Committing transaction")
## Create spatial layer:
示例8: Relationship
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
# html_rel = Relationship(new_node,"HTML text",html_text)
# gp.create(html_rel)
gp.commit()
def get_the_available_crawlers():
crawlers = ["CRAWLER-2", "CRAWLER-3", "CRAWLER-4"]
return crawlers
graph_database_location = "http://"+database+":7474/db/data/"
graph = Graph(graph_database_location, user='neo4j', password='cns2202') # connect to the local graph database
if delete_graph_history == "yes":
graph.delete_all() # Delete all the previous made nodes and relationship
print("DATABASE DELETED !")
gp = graph.begin()
coordinates = [] # create the list for coordinates
coordinates = generate_coordinates(width, height, coordinates) # generates coordinates based on the diff and the resolution
coordinates = generate_random_coordinates(coordinates) # already generated coordinates are shuffled randomly
chrome_options = Options()
chrome_options.add_extension(".\process_monitor.crx") # Adding the extension to chrome
# chrome_options.add_extension("C:\\Users\crawler\Desktop\Crawler\process_monitor.crx")
chromium_path = ".\chrome-win32\chrome.exe" # Use the portable chromium browser
# If chromium browser is not required then by removing the above chromium path, it will start using the default one
# The default will be developer google chrome.
# ONly Dev channel google chrome can support the extension used here. This extension used a particular API.
# The API used is "chrome.processes" and it is available only in the chrome dev-channel and chromium browser
chrome_options.binary_location = chromium_path
示例9: __init__
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
class Neo4j:
labels = None
relationship_types = None
property_keys = None
constraints = None
indexes = None
parameters = {}
def __init__(self, host, port, username=None, password=None, ssl=False, timeout=None, bolt=None):
if timeout is not None:
http.socket_timeout = timeout
host_port = "{host}:{port}".format(host=host, port=port)
uri = "{scheme}://{host_port}/db/data/".format(scheme="https" if ssl else "http", host_port=host_port)
self.graph = Graph(uri, user=username, password=password, bolt=bolt, secure=ssl)
try:
self.neo4j_version = self.graph.dbms.kernel_version
except Unauthorized:
raise AuthError(uri)
except SocketError:
raise ConnectionError(uri)
def cypher(self, statement):
error = False
headers = []
rows = []
start = datetime.now()
tx = self.graph.begin()
try:
result = tx.run(statement, self.parameters)
headers = list(result.keys())
rows = [[x[header] for header in headers] for x in result]
tx.commit()
except KeyboardInterrupt:
tx.rollback()
error = ""
except Exception as e:
error = e
end = datetime.now()
return {
"headers": headers,
"rows": rows,
"duration": duration_in_ms(start, end),
"error": error
}
def get_labels(self):
if not self.labels:
self.labels = sorted(self.graph.node_labels)
return self.labels
def get_relationship_types(self):
if not self.relationship_types:
self.relationship_types = sorted(self.graph.relationship_types)
return self.relationship_types
def get_property_keys(self):
if not self.property_keys:
self.property_keys = sorted(remote(self.graph).resolve("propertykeys").get().content)
return self.property_keys
def get_constraints(self):
if not self.constraints:
data = remote(self.graph).resolve("schema/constraint").get().content
self.constraints = sort_dict_by_key(data, "label")
return self.constraints
def get_indexes(self):
if not self.indexes:
data = remote(self.graph).resolve("schema/index").get().content
self.indexes = sort_dict_by_key(data, "label")
return self.indexes
def update_parameters(self, key, value):
self.parameters[key] = value
def refresh(self):
self.labels = None
self.relationship_types = None
self.property_keys = None
self.indexes = None
self.constraints = None
self.get_labels()
self.get_relationship_types()
self.get_property_keys()
self.get_indexes()
self.get_constraints()
def print_labels(self):
headers = ["Labels"]
rows = [[x] for x in self.get_labels()]
print(pretty_table(headers, rows))
#.........这里部分代码省略.........
示例10: handle
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
def handle(self, *args, **options): # pylint: disable=unused-argument
"""
Iterates through each course, serializes them into graphs, and saves
those graphs to neo4j.
"""
host = options['host']
port = options['port']
neo4j_user = options['user']
neo4j_password = options['password']
authenticate(
"{host}:{port}".format(host=host, port=port),
neo4j_user,
neo4j_password,
)
graph = Graph(
bolt=True,
password=neo4j_password,
user=neo4j_user,
https_port=port,
host=host,
secure=True
)
mss = ModuleStoreSerializer()
total_number_of_courses = len(mss.all_courses)
for index, course in enumerate(mss.all_courses):
# first, clear the request cache to prevent memory leaks
RequestCache.clear_request_cache()
log.info(
"Now exporting %s to neo4j: course %d of %d total courses",
course.id,
index + 1,
total_number_of_courses
)
nodes, relationships = mss.serialize_course(course.id)
log.info(
"%d nodes and %d relationships in %s",
len(nodes),
len(relationships),
course.id
)
transaction = graph.begin()
try:
# first, delete existing course
transaction.run(
"MATCH (n:item) WHERE n.course_key='{}' DETACH DELETE n".format(
six.text_type(course.id)
)
)
# now, re-add it
self.add_to_transaction(nodes, transaction)
self.add_to_transaction(relationships, transaction)
transaction.commit()
except Exception: # pylint: disable=broad-except
log.exception(
"Error trying to dump course %s to neo4j, rolling back",
six.text_type(course.id)
)
transaction.rollback()
示例11: Graph
# 需要导入模块: from py2neo import Graph [as 别名]
# 或者: from py2neo.Graph import begin [as 别名]
from py2neo import Graph, Node, Relationship
import sys
from time import sleep
graph_database_location = "http://192.168.100.53:7474/db/data/"
graph = Graph(graph_database_location, user='neo4j', password='cns2202') # connect to the local graph database
tx=graph.begin()
statement = 'Match (a:Main_Tab)-[c:Crawling_Complete]->(b:Completed) WHERE ((a.Crawler="CRAWLER-1")) RETURN c'
count=[]
flag_detected = 0
if (sys.argv[1] == "CRAWLER-1"):
flag_detected = 1
else:
while True:
print("Okay I am gonna sleep for 30 seconds and Check again")
sleep(30)
cursor=tx.run(statement).data()
print(cursor)
if(len(cursor) != 0):
for each in cursor:
x=list(each.values())
count.append(x[0])
if (len(count) != 0):
flag_detected = 1
break
if( flag_detected == 1):
print("Detected Completion")