-
Neo4j 数据导入
-
一、安装与部署Neo4j
<p> 直接在官网下载安装包安装,解压即可。</p> </li> <li> <p> </p> <p><strong>2.mysql 数据导入neo4j的jar包</strong></p> <pre class="has" name="code"><code class="hljs css"><span class="hljs-selector-tag">apoc-3</span><span class="hljs-selector-class">.4</span><span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.1-all</span><span class="hljs-selector-class">.jar</span> <span class="hljs-selector-tag">mysql-connector-java-8</span><span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.8-dmr</span><span class="hljs-selector-class">.jar</span></code><div class="hljs-button signin" data-title="登录后复制" onclick="hljs.signin(event)"></div></pre> <p> </p> </li> <li> <p><strong>3.将对应jar包放在Neo4j的安装目录plugins文件目录里,然后在Neo4j的conf目录里的neo4j.conf文件的最后面加上</strong></p> <p>dbms.security.procedures.unrestricted=apoc.*,algo.*</p> </li> <li> <p>apoc.import.file.enabled=true<br> apoc.export.file.enabled=true</p> </li> <li> <p>注意:(APOC jar包要与neo4j版本对应,否则启动Neo4j有可能出现无法连接这种情况 我这里用到的Neo4j版本为3.4.6 APOC为3.4.0 本人曾经在Neo4j-3.5.0版本plugins文件目录里 添加APOC-3.4版本,出现启动Neo4j无法连接这种情况 )</p> </li> <li> <p><strong>4.程序:</strong></p> </li> <li> <pre class="has" name="code"><code class="hljs swift"><ol class="hljs-ln" style="2869px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">package com.dataexa.neo4j.test;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> org.neo4j.driver.v1.AuthTokens;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> org.neo4j.driver.v1.Driver;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> org.neo4j.driver.v1.GraphDatabase;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> org.neo4j.driver.v1.Session;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Connect</span> </span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> void main(<span class="hljs-type">String</span>[] args) {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-type">Driver</span> driver = <span class="hljs-type">GraphDatabase</span>.driver(<span class="hljs-string">"bolt://localhost:7687"</span>, <span class="hljs-type">AuthTokens</span>.basic(<span class="hljs-string">"neo4j"</span>, <span class="hljs-string">"root"</span>));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-type">Session</span> session = driver.session();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-type">String</span> cypher = <span class="hljs-string">"create constraint on (n:ITEM) ASSERT n.itemid is unique"</span>; <span class="hljs-comment">//创建唯一索引,这样可以更快的导入数据</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> session.run(cypher);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> cypher = <span class="hljs-string">"CALL apoc.load.jdbc( "</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">" 'jdbc:mysql://localhost/abc(数据库名)?user=root(数据库的登录用户)&password=root(数据库登录密码)&useUnicode=true&characterEncoding=utf8', "</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">" 'select * from yntest(表名)' "</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">") YIELD row "</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">"CREATE (:People { cardNo :row.cardNo,name : row.name,sex : row.sex,"</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">"birthday : row.birthday,phone : row.phone})"</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">"CREATE (:People1 { cardNo1 : row.cardNo1,name1 : row.name1,"</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">"sex1 : row.sex1,birthday1 : row.birthday1,phone1 : row.phone1})"</span> +</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-string">"CREATE (:People)-[r:relation{edge:'铁路同行',txcs:row.txcs}]->(People1)"</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">///////"CREATE (:People { cardNo :row.cardNo,name : row.name,sex : row.sex," + "birthday : row.birthday,phone : row.phone})" + "CREATE (:People1 { cardNo1 : row.cardNo1,name1 : row.name1," + "sex1 : row.sex1,birthday1 : row.birthday1,phone1 : row.phone1})" + "CREATE (:People)-[r:relation{edge:'铁路同行',txcs:row.txcs}]->(People1)";</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//////以上是mysql中的表开头字段</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> session.run(cypher);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> session.close();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> driver.close();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button signin" data-title="登录后复制" onclick="hljs.signin(event)"></div></pre> </li>