栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

neo4j两种同步数据方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

neo4j两种同步数据方式

文章目录
  • apoc方式同步jdbc
    • 1. call apoc
    • 2. 步骤
  • neo4j-import方式全量同步
    • 1. 导出csv文件
    • 2. 执行import命令
    • 3. 切换数据库,修改配置文件conf/neo4j.conf
    • 4. 重启neo4j

apoc方式同步jdbc 1. call apoc
CALL apoc.load.jdbc('jdbc:mysql://172.16.4.83:3306/ibmp?user=ibmp_test&password=XZdfjXIEsrumGrfFTmfltbtAuQCECUdl&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC', 'select * from basic_community') YIELD row
2. 步骤
  • 创建索引
CREATE INDEX ON :BasicCommunity(community_id);
CREATE INDEX ON :BasicDevices(device_id);
CREATE INDEX ON :BasicDevices(community_id);
  • apoc查询jdbc数据,最近一小时
select * from basic_person where upd_time >= from_unixtime(${updateTime}-1800) or crt_time >= from_unixtime(${updateTime}-1800);
  • 创建/更新节点、关系
//apoc.load.jdbc在一个会话里不建议多次调用
//小区
with "jdbc:mysql://172.16.4.83:3306/ibmp?user=ibmp_test&password=111111&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" as url
//CALL apoc.load.jdbc(url, 'select * from basic_community where community_id="420100000009" or community_id="420100000012"') YIELD row 
CALL apoc.load.jdbc(url, 'select * from basic_community') YIELD row 
MERGE (c:BasicCommunity{id:row.id})
on create set c.community_id=row.community_id,c.community_name=row.community_name,c.crt_time=datetime(),c.del_flag=row.del_flag
on match set c.community_id=row.community_id,c.community_name=row.community_name,c.upd_time=datetime(),c.del_flag=row.del_flag
//with c
//match (m:BasicDevices) where m.del_flag='0' and m.community_id is not null and m.community_id=c.community_id
//match (j:BasicCommunity) where j.del_flag='0' and j.community_id=m.community_id
//merge (m)-[b:BELONG_TO]->(j)
//merge (j)-[o:HAS]->(m)
//with '' as x
//MATCH (d:BasicCommunity{del_flag:'1'}) 
//DETACH DELETe d
//设备
with "jdbc:mysql://172.16.4.83:3306/ibmp?user=ibmp_test&password=111111&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" as url
//CALL apoc.load.jdbc(url, 'select * from basic_devices where device_id="004A770124002EC5" or device_id="WHJK000201RL0001"') YIELD row
CALL apoc.load.jdbc(url, 'select * from basic_devices') YIELD row
MERGE (c:BasicDevices{id:row.id})
on create set c.device_id=row.device_id,c.community_id=row.community_id,c.device_name=row.device_name,c.crt_time=datetime(),c.del_flag=row.del_flag
on match set c.device_id=row.device_id,c.community_id=row.community_id,c.device_name=row.device_name,c.upd_time=datetime(),c.del_flag=row.del_flag
//with c
//match (m:BasicDevices) where m.del_flag='0' and m.community_id is not null and m.id=c.id
//match (j:BasicCommunity) where j.del_flag='0' and j.community_id=m.community_id
//merge (m)-[b:BELONG_TO]->(j)
//merge (j)-[o:HAS]->(m)
//with '' as x
//MATCH (d:BasicDevices{del_flag:'1'}) 
//DETACH DELETe d
//人员
with "jdbc:mysql://172.16.4.83:3306/ibmp?user=ibmp_test&password=111111&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" as url
CALL apoc.load.jdbc(url, 'select * from basic_person') YIELD row
MERGE (c:BasicPerson{id:row.id})
on create set c.person_id=row.person_id,c.name=row.name,c.crt_time=datetime(),c.del_flag=row.del_flag
on match set c.person_id=row.person_id,c.name=row.name,c.upd_time=datetime(),c.del_flag=row.del_flag
with '' as x
MATCH (d:BasicPerson{del_flag:'1'}) 
DETACH DELETe d
  • 删除节点、关系
//小区
with '' as x
MATCH (d:BasicCommunity{del_flag:'1'}) 
DETACH DELETE d
//设备
with '' as x
MATCH (d:BasicDevices{del_flag:'1'}) 
DETACH DELETE d
  • 建立关系
match (n:BasicCommunity)
match (m:BasicDevices) where m.del_flag='0' and m.community_id is not null and n.community_id=m.community_id
merge (m)-[b:BELONG_TO]->(n)
merge (n)-[o:HAS]->(m)
neo4j-import方式全量同步

  全量同步之前需要保证同步的数据库是新建的或者清空数据的

rm -rf /opt/neo4j-community-3.5.26/data/databases/testcz.db/ 
1. 导出csv文件
  • 导出节点
-- basic_car
select id as `id:ID`, license, person_id, 'TestBasicCar4' as `:LABEL` from basic_car where del_flag='0';
-- basic_person
select id as `id:ID`, `name`,person_id,'TestBasicPerson4' as `:LABEL` from basic_person where del_flag='0' and person_id in ('202cb962ac59075b964b07152d234b70', 'f703da8059a19b0bde6c4eb857de5f50', 'ffc2a2b9066cb5119d41b367d3027060', '35314df2291a7ba05851ec60beef5a35');
  • 导出关系
select a.id as `:START_ID`, b.id as `:END_ID`, '属于' as `:TYPE` from basic_car a INNER JOIN basic_person b on a.person_id=b.person_id where a.del_flag='0' and b.del_flag='0';
2. 执行import命令
bin/neo4j-admin import --mode csv --database=testcz.db --ignore-extra-columns=true  --ignore-missing-nodes=true --ignore-duplicate-nodes=true --nodes:TestBasicPerson4 '/opt/neo4j-community-3.5.26/import/TestPerson4.csv' --nodes:TestBasicCar4 '/opt/neo4j-community-3.5.26/import/TestBasicCar4.csv' --relationships:属于 '/opt/neo4j-community-3.5.26/import/TestPersonCarRel4.csv'
3. 切换数据库,修改配置文件conf/neo4j.conf
dbms.active_database=testcz.db
4. 重启neo4j
bin/neo4j restart
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号