1、創(chuàng)建實(shí)體
CREATE CONSTRAINT uniq_law_id ON (p:Law) ASSERT p.id IS UNIQUE;
CALL apoc.periodic.iterate(
'call apoc.load.jdbc("jdbc:clickhouse://192.xxx.x.xxx:8123/xxx?user=xxx&password=xxx", " select * from xxx.xxx", []) ',
'CALL apoc.merge.node([row.ent_label],
{id: row.id},
{name:row.name,level:row.level,office:row.office,publish:row.publish,expiry:row.expiry,law_type:row.law_type,status:row.status},
{name:row.name,level:row.level,office:row.office,publish:row.publish,expiry:row.expiry,law_type:row.law_type,status:row.status}
) yield node RETURN count(*)',
{batchSize:1000, parallel:false}
)
;
這段代碼的目的是從 ClickHouse 數(shù)據(jù)庫中加載數(shù)據(jù)到 Neo4j 圖數(shù)據(jù)庫,并在加載過程中使用 APOC(Awesome Procedures on Cypher)庫提供的 apoc.merge.node
過程來合并數(shù)據(jù),確保在圖數(shù)據(jù)庫中的節(jié)點(diǎn)具有唯一性。
逐行解釋這段代碼:
-
CREATE CONSTRAINT uniq_law_id ON (p:Law) ASSERT p.id IS UNIQUE;
: 這一行創(chuàng)建了一個(gè)唯一約束,確保 "Law" 類型的節(jié)點(diǎn)中的id
屬性是唯一的。這是為了防止在后續(xù)的數(shù)據(jù)加載過程中出現(xiàn)重復(fù)的節(jié)點(diǎn)。 -
CALL apoc.periodic.iterate('call apoc.load.jdbc("jdbc:clickhouse://192.xxx.x.xxx:8123/xxx?user=xxx&password=xxx", "select * from xxx.xxx", [])', ...
: 這一行使用 APOC 提供的apoc.periodic.iterate
過程,該過程允許對(duì)數(shù)據(jù)進(jìn)行迭代處理。 -
'call apoc.load.jdbc("jdbc:clickhouse://192.xxx.x.xxx:8123/xxx?user=xxx&password=xxx", " select * from xxx.xxx", [])'
: 在迭代中,首先調(diào)用apoc.load.jdbc
過程,從 ClickHouse 數(shù)據(jù)庫中加載數(shù)據(jù)。這里使用的 JDBC 連接字符串指向 ClickHouse 數(shù)據(jù)庫,提供用戶名和密碼用于連接。 -
'CALL apoc.merge.node([row.ent_label], ...': 在每次迭代中,對(duì)于從 ClickHouse 加載的每一行數(shù)據(jù),調(diào)用
apoc.merge.node` 過程。-
[row.ent_label]
: 這是一個(gè)用于標(biāo)記節(jié)點(diǎn)標(biāo)簽的列表。在這里,使用了row.ent_label
作為節(jié)點(diǎn)的標(biāo)簽,可能是從 ClickHouse 數(shù)據(jù)庫中的某個(gè)列獲取的。 -
{id: row.id}, {name:row.name,level:row.level,...}
: 這里定義了節(jié)點(diǎn)的屬性。{id: row.id}
表示節(jié)點(diǎn)的id
屬性,其值來自加載的行數(shù)據(jù)中的id
列。同樣的邏輯適用于其他屬性。 -
yield node RETURN count(*)
: 返回每次迭代中處理的節(jié)點(diǎn)數(shù)量。這可以幫助你了解迭代的進(jìn)展。 -
{batchSize:1000, parallel:false}
: 定義了迭代的參數(shù)。batchSize
表示每次迭代處理的行數(shù),parallel
表示是否并行處理。在這里,設(shè)置為串行(false
)。
-
總的來說,這段代碼的目的是從 ClickHouse 數(shù)據(jù)庫中加載數(shù)據(jù)到 Neo4j 圖數(shù)據(jù)庫,確保在圖數(shù)據(jù)庫中的 "Law" 節(jié)點(diǎn)具有唯一的 id
屬性。在加載的過程中,使用了 APOC 庫的 apoc.merge.node
過程,它可以合并節(jié)點(diǎn),確保數(shù)據(jù)的唯一性。
2、創(chuàng)建關(guān)系
CALL apoc.periodic.iterate(
'call apoc.load.jdbc("jdbc:clickhouse://192.xxx.x.xxx:8123/xxx?user=xxx&password=xxx", " select * from xxx.xxx", [])yield row',
'merge (n1:row.from_label {id: row.from_id})
merge (n2:row.to_label {id: row.to_id})
with n1, n2, row
CALL apoc.merge.relationship(
n1,
row.rel_type,
row.name,
{},
{},
n2,
{}
) YIELD rel
return id(rel), type(rel), rel',
{batchSize:2000, parallel:false}
)
;
這段代碼的目的是從 ClickHouse 數(shù)據(jù)庫中加載關(guān)系數(shù)據(jù)到 Neo4j 圖數(shù)據(jù)庫,并在加載的過程中使用 APOC 庫提供的 apoc.merge.relationship
過程來合并關(guān)系,確保在圖數(shù)據(jù)庫中的關(guān)系具有唯一性。
逐行解釋這段代碼:
-
CALL apoc.periodic.iterate(...
: 這是一個(gè)調(diào)用 APOC 提供的apoc.periodic.iterate
過程的 Cypher 查詢。該過程允許對(duì)數(shù)據(jù)進(jìn)行迭代處理。 -
'call apoc.load.jdbc("jdbc:clickhouse://192.168.1.168:8123/law?user=default&password=QuBmUhBv", " select * from law.rel_law_bzjtkx_include", []) yield row'
: 在迭代中,首先調(diào)用apoc.load.jdbc
過程,從 ClickHouse 數(shù)據(jù)庫中加載關(guān)系數(shù)據(jù)。這里使用的是 ClickHouse 數(shù)據(jù)庫的 JDBC 連接字符串,提供用戶名和密碼用于連接。 -
'merge (n1:row.from_label {id: row.from_id}) merge (n2:row.to_label {id: row.to_id}) with n1, n2, row'
: 對(duì)于從 ClickHouse 加載的每一行數(shù)據(jù),使用merge
關(guān)鍵字創(chuàng)建起始節(jié)點(diǎn)n1
和目標(biāo)節(jié)點(diǎn)n2
。這里使用了row.from_label
和row.to_label
作為節(jié)點(diǎn)標(biāo)簽,并使用row.from_id
和row.to_id
作為節(jié)點(diǎn)的id
屬性值。 -
CALL apoc.merge.relationship(n1, row.rel_type, row.name, {}, {}, n2, {}) YIELD rel
: 使用 APOC 提供的apoc.merge.relationship
過程,該過程可以合并關(guān)系。具體參數(shù)包括:-
n1
: 起始節(jié)點(diǎn)。 -
row.rel_type
: 關(guān)系類型,來自于加載的數(shù)據(jù)的rel_type
列。 -
row.name
: 關(guān)系的名稱,來自于加載的數(shù)據(jù)的name
列。 -
{}
: 關(guān)系的屬性,這里為空對(duì)象。 -
{}
: 關(guān)系的屬性更新規(guī)則,這里為空對(duì)象。 -
n2
: 目標(biāo)節(jié)點(diǎn)。
-
-
YIELD rel
: 返回被合并的關(guān)系對(duì)象。 -
return id(rel), type(rel), rel'
: 返回合并關(guān)系的 ID、關(guān)系類型和關(guān)系對(duì)象。 -
{batchSize:2000, parallel:false}
: 定義了迭代的參數(shù)。batchSize
表示每次迭代處理的行數(shù),parallel
表示是否并行處理。在這里,設(shè)置為串行(false
)。文章來源:http://www.zghlxwxcb.cn/news/detail-832601.html
總體來說,這段代碼的目的是從 ClickHouse 數(shù)據(jù)庫中加載關(guān)系數(shù)據(jù)到 Neo4j 圖數(shù)據(jù)庫,確保在圖數(shù)據(jù)庫中的關(guān)系具有唯一性。在加載的過程中,使用了 APOC 庫的 apoc.merge.relationship
過程,它可以合并關(guān)系,確保數(shù)據(jù)的唯一性。文章來源地址http://www.zghlxwxcb.cn/news/detail-832601.html
到了這里,關(guān)于使用apoc將數(shù)據(jù)從數(shù)據(jù)庫導(dǎo)入neo4j的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!