SlideShare a Scribd company logo
Neo4j を
Javaプログラムから使う
(ローカルとリモート)
初心者向け/Java開発者向け
@madgaoh
今日の登場人物
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
CypherPlugIn
neo4j-rest-graphdb
neo4j-cypher-plugin
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
<<interface>> RestAPI
2
RestGraphDatabase
RestAPIFacade
RestCypherQueryEngine
必要なjarファイルを
classpathに通す
http://docs.neo4j.org/chunked/stable/tutorials-java-embedded-setup.html
• Neo4jをダウンロードして libディレクトリの下にあるjarを全
部classpathに通す
• Mavenを使って依存jar一式を取得する
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
3
ローカルで使う
data
EmbeddedGraphDatabase
neo4j-kernel
EmbeddedGraphDatabase graphdb
= new EmbeddedGraphDatabase("path/to/db/data");
Node nodeA = graphdb.createNode();
Node nodeB = graphdb.createNode();
nodeA.createRelationshipTo(nodeB, MyTypeEnum.FRIEND);
graphdb.shutdown();
4
ローカルで使う
data
EmbeddedGraphDatabase
neo4j-kernel
EmbeddedGraphDatabase graphdb
= new EmbeddedGraphDatabase("path/to/db/data");
Node nodeA = graphdb.createNode();
Node nodeB = graphdb.createNode();
nodeA.createRelationshipTo(nodeB, MyTypeEnum.FRIEND);
graphdb.shutdown();
もしまだ無ければ
新規作成される
注意
最後は必ずシャットダウン
5
ローカルで使う
(ファクトリ経由で生成する)
<<interface>>
GraphDatabaseService
data
EmbeddedGraphDatabase
neo4j-kernel
GraphDatabaseService graphdb
= new GraphDatabaseFactory()
.newEmbeddedDatabase (" path/to/db/data ");
Node nodeA = graphdb.createNode();
Node nodeB = graphdb.createNode();
nodeA.createRelationshipTo(nodeB, MyTypeEnum.FRIEND);
graphdb.shutdown();
6
ローカルで使う
(ビルダーでカスタマイズする)
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
data
EmbeddedGraphDatabase
neo4j-kernel
GraphDatabaseService graphdb
= new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(" path/to/db/data ")
.setConfig(GraphDatabaseSettings.read_only, "true")
.newGraphDatabase();
Node nodeA = graphdb.createNode();
graphdb.shutdown();
7
リードオンリーの指定
ローカルで使う
(ビルダーでカスタマイズする)
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
data
EmbeddedGraphDatabase
neo4j-kernel
GraphDatabaseService graphdb
= new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(" path/to/db/data ")
.setConfig(GraphDatabaseSettings.read_only, "true")
.newGraphDatabase();
Node nodeA = graphdb.createNode();
graphdb.shutdown();
8
もしまだ無ければ
例外発生!
書き込み操作はできない
(例外発生!)
ローカルで使う
(Cypherのクエリで検索する)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
data
GraphDatabaseService graphdb
= new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(" path/to/db/data ")
.newGraphDatabase();
ExecutionEngine engine = new ExecutionEngine(graphdb);
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
ExecutionResult result
= engine.execute("START n = node({id}) RETURN n", params);
9
ローカルで使う
(Cypherのクエリで検索する)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
data
GraphDatabaseService graphdb
= new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(" path/to/db/data ")
.newGraphDatabase();
ExecutionEngine engine = new ExecutionEngine(graphdb);
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
ExecutionResult result
= engine.execute("START n = node({id}) RETURN n", params);
注意
Cypher は Scala で書かれており、
ExecutionEngine は同名のクラスでも
Scala 用と Java 用があるので、正解は
Package名に .javacompat と付く方
10
RESTfulサーバとして起動する
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
> curl -X POST http://server:7474/db/data/node
-d {"last_name": "satake", "first_name": "masahiro"}'
> curl -X GET http://server:7474/db/data/node/1
Jetty (AppServer)
+
Jersey (JAX-RS)
+
Jackson (JSON)
11
neo4j-rest-graphdb
とは
• Neo4jのREST-APIをラップしてくれるクライアントサイド用
Javaライブラリ
• Mavenリポジトリが別になっている
– セントラルに登録されていない
• pom.xml に リポジトリの追加の設定が必要
• GitHubで見ると java-rest-binding という名前になっている
– 寄贈されたもの・・・(だけど neo4j-contrib でもない・・・?)
– 本体のバージョンと完全には同期していない
• 5/22 現在の最新は1.8.1
– Branch 1.8.1を見ると1.8.2を追いかけてはいる
• 同時に1.9-SNAPTSHOT 2.0-SNAPSHOT もある
https://github.com/neo4j/java-rest-binding
12
neo4j-rest-graphdb
(Mavenリポジトリを追加)
<repositories>
<repository>
<id>neo4j-public-repository</id>
<name>Publically available Maven 2 repository for Neo4j</name>
<url>http://m2.neo4j.org</url>
</repository>
</repositories>
:
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
13
neo4j-rest-graphdb
(Server側と本体バージョン合わせる)
1) 本体のライブラリだけ明示的に1.8.2にする
<dependencies>
:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>server-api</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-lucene-index</artifactId>
<version>1.8.2</version>
</dependency>
:
</dependencies>
2) 1.9-SNAPSHOT(RC2) に上げてしまう
<repositories>
<repository>
<id>neo4j-public-repository</id>
<name>Publically available Maven 2 repository for
Neo4j</name>
<url>http://m2.neo4j.org</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
:
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>1.9-SNAPSHOT</version>
</dependency>
</dependencies>
3) ブランチ最新ソースからビルド
14
リモートで使う
(neo4j-rest-graphdbを利用)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
RestAPI api = new RestAPIFacade("http://server:7474/db/data");
Map<String, Object> props = new HashMap<>();
api.createNode(props);
api.close();
<<interface>> RestAPI
15
RestAPIFacade
リモートで使う
(neo4j-rest-graphdbを利用)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
RestAPI api = new RestAPIFacade("http://server:7474/db/data");
Map<String, Object> props = new HashMap<>();
api.createNode(props);
api.close();
<<interface>> RestAPI
16
注意
最後は必ずクローズする
RestAPIFacade
リモートで使う
(ローカルの時と同じinterfaceで)
ExecutionEngine
<<interface>> RestAPI
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
GraphDatabaseService graphdb = new RestGraphDatabase(api);
// or new RestGraphDatabase ("http://...");
// or GraphDatabaseFactory.databaseFor("http://...");
RestGraphDatabase
RestAPIFacade
17
<<interface>> RestAPI
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
RestAPI api = new RestAPIFacade("http://server:7474/db/data");
GraphDatabaseService graphdb = new RestGraphDatabase(api);
// or new RestGraphDatabase ("http://...");
// or GraphDatabaseFactory.databaseFor("http://...");
Node nodeA = graphdb.createNode();
graphdb.shutdown();
ExecutionEngine
RestGraphDatabase
RestAPIFacade
リモートで使う
(ローカルの時と同じInterfaceで)
18
<<interface>> RestAPI
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
RestAPI api = new RestAPIFacade("http://server:7474/db/data");
GraphDatabaseService graphdb = new RestGraphDatabase(api);
// or new RestGraphDatabase ("http://...");
// or GraphDatabaseFactory.databaseFor("http://...");
Node nodeA = graphdb.createNode();
graphdb.shutdown();
ExecutionEngine
RestGraphDatabase
RestAPIFacade
リモートで使う
(ローカルの時と同じInterfaceで)
19
注意
ここでも最後は必ずシャットダウン
(サーバはシャットダウンされない。
内部のRestAPIやリソースの開放)
リモートで使う
(Cypherのクエリで検索する)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
<<interface>> RestAPI
GraphDatabaseService graphdb
= new RestGraphDatabase(api);
ExecutionEngine engine
= new ExecutionEngine(graphdb);
RestGraphDatabase
RestAPIFacade
ExecutionEngine
20
リモートで使う
(Cypherのクエリで検索する)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
neo4j-rest-graphdb
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
<<interface>> RestAPI
RestGraphDatabase
RestAPIFacade
ExecutionEngine
21
GraphDatabaseService graphdb
= new RestGraphDatabase(api);
ExecutionEngine engine
= new ExecutionEngine(graphdb);
注意
Client-Sideで Cypher クエリを使う場合に
Server側の cypher ライブラリは使わない
リモートで使う
(Cypherのクエリで検索する)
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
CypherPlugIn
neo4j-rest-graphdb
neo4j-cypher-plugin
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
<<interface>> RestAPI
RestAPI api
= new RestAPIFacade("URL…");
RestCypherQueryEngine engine
= new RestCypherQueryEngine(api);
RestGraphDatabase
RestAPIFacade
RestCypherQueryEngine
22
ExecutionEngineneo4j-server
(HTTP)
(RESTful)
(JSON)
CypherPlugIn
neo4j-rest-graphdb
neo4j-cypher-plugin
neo4j-cypher
neo4j-kernel
Client-Side
Server-Side
<<interface>> RestAPI
RestAPI api
= new RestAPIFacade("URL…");
RestCypherQueryEngine engine
= new RestCypherQueryEngine(api);
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
QueryResult result
= engine.query ("START n = node({id}) RETURN n", params);
RestGraphDatabase
RestAPIFacade
RestCypherQueryEngine
23
リモートで使う
(Cypherのクエリで検索する)
ご清聴ありがとうございました
ExecutionEngine
<<interface>>
GraphDatabaseService
EmbeddedReadOnly
GraphDatabase
neo4j-server
(HTTP)
(RESTful)
(JSON)
CypherPlugIn
neo4j-rest-graphdb
neo4j-cypher-plugin
neo4j-cypher
neo4j-kernel
EmbeddedGraphDatabase
Client-Side
Server-Side
data
<<interface>> RestAPI
24
RestGraphDatabase
RestAPIFacade
RestCypherQueryEngine

More Related Content

PPT
Maven2 plugin
PDF
Heroku java
PDF
Head toward Java 16 (Night Seminar Edition)
PPTX
Panamaを先取り!? JVMCIでJITと遊ぶ
PDF
Migration Guide from Java 8 to Java 11 #jjug
PDF
明日から使えるgradle
PDF
Gradle a new Generation Build Tool
PDF
Catch up Java 12 and Java 13
Maven2 plugin
Heroku java
Head toward Java 16 (Night Seminar Edition)
Panamaを先取り!? JVMCIでJITと遊ぶ
Migration Guide from Java 8 to Java 11 #jjug
明日から使えるgradle
Gradle a new Generation Build Tool
Catch up Java 12 and Java 13

What's hot (20)

PDF
Head toward Java 15 and Java 16
PDF
Head toward Java 13 and Java 14 #jjug
PDF
JDKの選択肢とサーバーサイドでの選び方
PDF
TDD勉強会キックオフ for Java
PDF
Advanced database monitoring in modern java
PDF
WildFly Swarmではじめる「パーツとしてのJavaEE」
PDF
JDK9 新機能 (日本語&ショートバージョン) #jjug
PDF
jjugccc2018 app review postmortem
PDF
これからのJDK/JVM 何を選ぶ?どう選ぶ?
PDF
OpenJDK コミュニティに参加してみよう #jjug
PDF
最適なOpenJDKディストリビューションの選び方 #codetokyo19B3 #ccc_l5
PDF
OpenShiftでJBoss EAP構築
PPTX
OpenJDKソムリエと巡るJDKワイナリーツアー #sfggjp #javajo
PPT
Jenkinsプラグイン開発
PDF
Firefoxの開発プロセス
PPTX
TDC20111031_Groovy_Geb
PPTX
OpenShift from Easy way to Hard ? Way
PPTX
20161022 Linux on Azureの世界
PPTX
20140518 JJUG MySQL Clsuter as NoSQL
PDF
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
Head toward Java 15 and Java 16
Head toward Java 13 and Java 14 #jjug
JDKの選択肢とサーバーサイドでの選び方
TDD勉強会キックオフ for Java
Advanced database monitoring in modern java
WildFly Swarmではじめる「パーツとしてのJavaEE」
JDK9 新機能 (日本語&ショートバージョン) #jjug
jjugccc2018 app review postmortem
これからのJDK/JVM 何を選ぶ?どう選ぶ?
OpenJDK コミュニティに参加してみよう #jjug
最適なOpenJDKディストリビューションの選び方 #codetokyo19B3 #ccc_l5
OpenShiftでJBoss EAP構築
OpenJDKソムリエと巡るJDKワイナリーツアー #sfggjp #javajo
Jenkinsプラグイン開発
Firefoxの開発プロセス
TDC20111031_Groovy_Geb
OpenShift from Easy way to Hard ? Way
20161022 Linux on Azureの世界
20140518 JJUG MySQL Clsuter as NoSQL
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
Ad

Similar to Neo4j を Javaプログラムから使う (19)

PPTX
Neo4j の「データ操作プログラミング」から 「ビジュアライズ」まで
PDF
無駄にNeo4jを使っている日々
PDF
[db tech showcase Tokyo 2015] E27: Neo4jグラフデータベース by クリエーションライン株式会社 李昌桓
PDF
異次元のグラフデータベースNeo4j
PPTX
Neo4j ハンズオン
PDF
Neo4j Stream, [RDB/NoSQL]Kafka Connector CDC(Change Data Captuer)の紹介
PDF
Do postgres-dream-of-graph-database
PDF
Spring “BigData”
PDF
これからのコンピューティングの変化とJava-JJUG CCC 2015 Fall
PPTX
Getting Started with Graph Database with Python
PPTX
Neoの世界へ
PDF
Infinispan - Open Source Data Grid rev2
PDF
Neo4jで始めるグラフDB入門 - LT Thursday
PDF
Osc shimane-2016-do-postgres-dream-of-graph-database
PDF
社会ネットワーク分析第7回
PDF
Hadoop book-2nd-ch3-update
PDF
はてなブックマーク in Scala
PDF
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
PPTX
JavaFX + NetBeans環境におけるJenkinsの活用(Jenkins第六回勉強会)
Neo4j の「データ操作プログラミング」から 「ビジュアライズ」まで
無駄にNeo4jを使っている日々
[db tech showcase Tokyo 2015] E27: Neo4jグラフデータベース by クリエーションライン株式会社 李昌桓
異次元のグラフデータベースNeo4j
Neo4j ハンズオン
Neo4j Stream, [RDB/NoSQL]Kafka Connector CDC(Change Data Captuer)の紹介
Do postgres-dream-of-graph-database
Spring “BigData”
これからのコンピューティングの変化とJava-JJUG CCC 2015 Fall
Getting Started with Graph Database with Python
Neoの世界へ
Infinispan - Open Source Data Grid rev2
Neo4jで始めるグラフDB入門 - LT Thursday
Osc shimane-2016-do-postgres-dream-of-graph-database
社会ネットワーク分析第7回
Hadoop book-2nd-ch3-update
はてなブックマーク in Scala
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
JavaFX + NetBeans環境におけるJenkinsの活用(Jenkins第六回勉強会)
Ad

Neo4j を Javaプログラムから使う