創建節點和建立關系
# -*- coding:utf-8 -*-
from py2neo import Node,Relationship,Graph,NodeMatcher,RelationshipMatcher
# 數據庫
graph = Graph('http://192.168.1.101:7474', auth=("neo4j", "123456"))
# 查詢節點是否存在
def MatchNode(m_graph,m_label,m_attrs):
m_n = "_.name=" + "\'" + m_attrs['name'] + "\'"
matcher = NodeMatcher(m_graph)
re_value = matcher.match(m_label).where(m_n).first()
return re_value
# 創建節點
def CreateNode(m_graph,m_label,m_attrs):
re_value = MatchNode(m_graph,m_label,m_attrs)
# print(re_value)
if re_value is None:
m_node = Node(m_label,**m_attrs)
# 創建節點
n = graph.create(m_node)
return n
return None
label1 = 'Stock'
attrs1 = {"name":'招商銀行',"code":'600036'}
label2 = 'SecuritiesExchange'
attrs2 = {"name":'上海證券交易所'}
CreateNode(graph,label1,attrs1)
CreateNode(graph,label2,attrs2)
# 建立兩個節點的關系,如果節點不存在,不進行創建
def CreateRelationship(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
re_value1 = MatchNode(m_graph,m_label1,m_attrs1)
re_value2 = MatchNode(m_graph,m_label2,m_attrs2)
if re_value1 is None or re_value2 is None:
return False
m_r = Relationship(re_value1,m_r_name,re_value2)
n = graph.create(m_r)
return n
m_r_name = "證券交易所"
re_value = CreateRelationship(graph,label1,attrs1,label2,attrs2,m_r_name)
批量創建節點和建立關系
# -*- coding:utf-8 -*-
import xlrd
import uuid
from py2neo import Node,Relationship,Graph,NodeMatcher,RelationshipMatcher
# 數據庫
graph = Graph('http://192.168.1.101:7474', auth=("neo4j", "123456"))
# 查詢節點是否存在
def MatchNode(graph,label,attrs):
matchName = "_.name=" + "\'" + attrs['name'] + "\'"
matcher = NodeMatcher(graph)
res = matcher.match(label).where(matchName).first()
return res
# 創建節點
def CreateNode(graph,label,attrs):
res = MatchNode(graph,label,attrs)
# print(res)
if res is None:
node = Node(label,**attrs)
# 創建節點
n = graph.create(node)
return n
return None
# 建立兩個節點的關系
def CreateRelationship(graph,label1,attrs1,label2,attrs2,relationship):
node1 = MatchNode(graph,label1,attrs1)
node2 = MatchNode(graph,label2,attrs2)
if node1 is None or node2 is None:
return False
node1_r_node2 = Relationship(node1,relationship,node2)
n = graph.create(node1_r_node2)
return n
def Excel2Neo4j(graph):
label1 = 'Stock'
label2 = 'Province'
relationship = 'Area'
readbook = xlrd.open_workbook('A股列表.xls',encoding_override="utf-8")
sheet = readbook.sheet_by_name('A股列表')
nrows = sheet.nrows
for i in range(1,nrows):
code = sheet.cell(i,4).value
name = sheet.cell(i,5).value
id = uuid.uuid3(uuid.NAMESPACE_DNS,sheet.cell(i,4).value).hex
fullname = sheet.cell(i,1).value
englishname = sheet.cell(i,2).value
address = sheet.cell(i,3).value
fdate = sheet.cell(i,6).value
province = sheet.cell(i,15).value
city = sheet.cell(i,16).value
industry = sheet.cell(i,17).value
weburl = sheet.cell(i,18).value
attrs1 = {'code':code,'name':name,'id':id,'fullname':fullname,'englishname':englishname,'address':address,\
'fdate':fdate,'province':province,'city':city,'industry':industry,'weburl':weburl}
print(attrs1)
CreateNode(graph,label1,attrs1)
attrs2 = {'name':province}
CreateNode(graph, label2, attrs2)
CreateRelationship(graph, label1, attrs1, label2, attrs2, relationship)
Excel2Neo4j(graph)
print("End")
# 通過cypher命令查詢某個標簽的節點個數
def GetNodeCountByLabel(graph,label):
cypher = "match(n:"+label+") return count(n)"
return graph.run(cypher).data()
print(GetNodeCountByLabel(graph,"Stock"))
print(GetNodeCountByLabel(graph,"Province"))
查詢操作
# -*- coding:utf-8 -*-
from py2neo import Node,Relationship,Graph,NodeMatcher,RelationshipMatcher
# 數據庫
graph = Graph('http://192.168.1.101:7474', auth=("neo4j", "123456"))
# 按id查詢節點
def MatchNodeByID(m_graph,m_id):
matcher = NodeMatcher(m_graph)
re_value = matcher.get(m_id)
return re_value
# 按attr查詢節點
def MatchNodeByAttr(m_graph,m_label,m_attrs):
m_n = "_.name=" + "\'" + m_attrs['name'] + "\'"
matcher = NodeMatcher(m_graph)
re_value = matcher.match(m_label).where(m_n).first()
return re_value
# 按label查詢節點
def MatchNodeByLabel(m_graph,m_label):
matcher = NodeMatcher(m_graph)
re_value = matcher.match(m_label)
return re_value
print(MatchNodeByID(graph,0))
print(MatchNodeByAttr(graph,'Stock',{"name":'招商銀行',"code":'600036'}))
print(MatchNodeByLabel(graph,'Stock'))
通過Cypher命令批量創建節點
- 下載文件
板塊,公司全稱,英文名稱,注冊地址,A股代碼,A股簡稱,A股上市日期,A股總股本,A股流通股本,B股代碼,B股 簡 稱,B股上市日期,B股總股本,B股流通股本,地 區,省 份,城 市,所屬行業,公司網址
主板,平安銀行股份有限公司,"Ping An Bank Co., Ltd.",廣東省深圳市羅湖區深南東路5047號,000001,平安銀行,1991-04-03,"19,405,918,198","19,405,546,950",,,,0,0,華南,廣東,深圳市,J 金融業,bank.pingan.com
主板,萬科企業股份有限公司,"CHINA VANKE CO., LTD.",廣東省深圳市鹽田區大梅沙環梅路33號萬科中心,000002,萬 科A,1991-01-29,"9,724,196,533","9,717,553,265",,,,0,0,華南,廣東,深圳市,K 房地產,www.vanke.com
主板,深圳國華網安科技股份有限公司,"Shenzhen GuoHua Network Security Technology Co., Ltd.",廣東省深圳市福田區梅林街道梅都社區中康路126號卓越梅林中心廣場(南區)卓悅匯B2206A,000004,國華網安,1990-12-01,"156,003,026","116,330,763",,,,0,0,華南,廣東,深圳市,I 信息技術,www.sz000004.cn
......
# -*- coding:utf-8 -*-
import urllib.request
def DownloadSZExchangeExcel():
url = 'http://www.szse.cn/api/report/ShowReport?SHOWTYPE=xlsx&CATALOGID=1110&TABKEY=tab1&random=0.4744196476238187'
downPath = 'A股列表.xlsx'
urllib.request.urlretrieve(url,downPath)
DownloadSZExchangeExcel()
# -*- coding:utf-8 -*-
import xlrd
import uuid
from py2neo import Graph
# 數據庫
graph = Graph('http://192.168.1.101:7474', auth=("neo4j", "123456"))
# 創建節點
def CreateNode(m_graph,m_cypher):
m_graph.run(m_cypher)
def Excel2Neo4j(m_graph):
label = 'Stock'
# filename = 'A股列表.xls'.encode('utf-8').decode('utf-8')
readbook = xlrd.open_workbook('A股列表.xls',encoding_override="utf-8")
sheet = readbook.sheet_by_name('A股列表')
nrows = sheet.nrows
ncols = sheet.ncols
for i in range(1,nrows):
c_cmd0 = 'create(n:{0}{{code:\'{1}\','.format(label,sheet.cell(i,4).value)
c_cmd1 = 'name:\'{0}\','.format(sheet.cell(i,5).value)#.replace('A','A')
c_cmd2 = 'id:\'{0}\','.format(uuid.uuid3(uuid.NAMESPACE_DNS,sheet.cell(i,4).value).hex)
c_cmd3 = 'fullname:\'{0}\','.format(sheet.cell(i,1).value)
c_cmd4 = 'englishname:\'{0}\','.format(sheet.cell(i,2).value)
c_cmd5 = 'address:\'{0}\','.format(sheet.cell(i,3).value)
c_cmd6 = 'fdate:\'{0}\','.format(sheet.cell(i,6).value)
c_cmd7 = 'province:\'{0}\','.format(sheet.cell(i,15).value)
c_cmd8 = 'city:\'{0}\','.format(sheet.cell(i,16).value)
c_cmd9 = 'industry:\'{0}\','.format(sheet.cell(i,17).value)
c_cmd10 = 'weburl:\'{0}\'}})'.format(sheet.cell(i,18).value)
c_cmd = c_cmd0+c_cmd1+c_cmd2+c_cmd3+c_cmd4+c_cmd5+c_cmd6+c_cmd7+c_cmd8+c_cmd9+c_cmd10
print(c_cmd)
CreateNode(graph,c_cmd)
Excel2Neo4j(graph)
print("End")
# 查詢某個標簽的節點個數
def GetNodeCountByLabel(m_graph,m_label):
m_n = "match(n:"+m_label+") return count(n)"
return m_graph.run(m_n).data()
print(GetNodeCountByLabel(graph,"Stock"))
