OrientDB Java APIの紹介
1概要
OrientDB
はhttps://en.wikipedia.org/wiki/Graph
database[Graph]、https://en.wikipediaで動作するように設計されたオープンソースのMulti-Model NoSQLデータベース技術です。 org/wiki/Document-oriented
database[ドキュメント]、https://en.wikipedia.org/wiki/Key-value__database[Key-Value]、https://orientdb.com/spatial-module-orientdb-2-2/https://orientdb.com/docs/last/SQL.html[SQL]構文でクエリを管理しながら、[GeoSpatial]およびhttps://orientdb.com/nosql/livequery/[Reactive]モデルを作成します。
この記事では、セットアップについて説明し、OrientDB Java APIを使用します。
2インストール
まず、バイナリパッケージをインストールする必要があります。
執筆時点で最新の安定版http://orientdb.com/download-2/[OrientDB](https://bit.ly/orientdb-ce-2-2-31[2.2.x])をダウンロードしましょう。この記事)。
次に、それを解凍し、その内容を便利なディレクトリに移動する必要があります(
ORIENTDB
HOME
を使用)。コマンドラインで簡単に使用できるように、
bin__フォルダを必ず環境変数に追加してください。
最後に、
$ ORIENTDB
HOME/bin
にある
orientdb.sh
ファイルを、
の場所にOrientDBディレクトリの場所(
ORIENTDB
HOME
)を埋めて編集する必要があります
これで、完全に機能するOrientDBが完成しました。我々は使用することができます:
**
-
__
-
__
-
start
アクションと
stop
アクションの両方にユーザーパスワード(
orientdb.sh
ファイルで設定したもの)が必要です。
サーバーが起動すると、ポート2480が使用されます。したがって、http://localhost:2480/studio/index.html[URL]:** を使用してローカルにアクセスできます。
リンク:/uploads/orientdb-running-1024×528.png%201024w[]
手動インストールの詳細については、https://orientdb.com/docs/2.2.x/Tutorial-Installation.htmlを参照してください。
注意:OrientDBにはhttps://www.java.com/en/download[Java]バージョン1.7以上が必要です。
以前のバージョンはhttp://orientdb.com/download-previous/[here]から入手できます。
3 OrientDB Java APIセットアップ
OrientDBを使用すると、Java開発者は次のような3つの異なるAPIを使用できます。
-
グラフAPI – グラフデータベース
-
ドキュメントAPI – ドキュメント指向データベース
-
オブジェクトAPI – OrientDB Documentに直接バインドされているオブジェクト
OrientDBを統合して使用するだけで、単一のコードベース内でこれらすべてのタイプを使用できます。
プロジェクトのクラスパスに含めることができるいくつかの利用可能なjarを見てみましょう。
-
orientdb-core – ** 。jar
:コアライブラリを持ってきます -
blueprints-core – ** 。jar
:アダプタのコアコンポーネントを持ってきます -
orientdb-graphdb – ** 。jar
:GraphデータベースAPIを与える -
orientdb-object – ** 。jar
:オブジェクトデータベースAPIを提供します -
orientdb-distributed – ** 。jar
:分散データベースプラグインを提供します。
サーバークラスタで作業する
orientdb-tools –
。jar:コンソールコマンドを引き渡す
-
orientdb-client – ** 。jar
:リモートクライアントを提供します -
orientdb-enterprise – ** 。jar
:プロトコルクラスとネットワーククラスを有効にします。
クライアントとサーバーで共有
最後の2つは、リモートサーバーでデータを管理している場合にのみ必要です。
Mavenプロジェクトから始めて、次の依存関係を使用しましょう。
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-core</artifactId>
<version>2.2.31</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-graphdb</artifactId>
<version>2.2.31</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-object</artifactId>
<version>2.2.31</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>2.6.0</version>
</dependency>
最新バージョンのOrientDBのhttp://www.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.orientechnologies%22%20AND%20a%3A%22orientdb-core%22については、Maven Centralリポジトリを確認してください。[コア]、http://www.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.orientechnologies%22%20AND%20a%3A%22orientdb-graphdb%22[GraphDB]、http://www.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.orientechnologies%22%20AND%20a%3A%22orientdb-object%22[Object]APIとhttp://www.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.tinkerpop.blueprints%22%20AND 20a%3A%22blueprints-core%22[ブループリント – コア]。
4.使い方
OrientDBはグラフを扱うためにhttp://tinkerpop.apache.org[TinkerPop Blueprints]実装を使用しています。
TinkerPop
は、グラフデータベースを構築するさまざまな方法を提供するグラフコンピューティングフレームワークです。
実装]
さらに、OrientDBでは、APIの種類にかかわらず、3種類のhttps://orientdb.com/docs/2.2.x/Graph-Schema.html[Schemas]を操作できます。
-
Schema-Full – 厳密モードが有効になっているため、すべてのフィールドが指定されています
クラス作成中
** Schema-Less – クラスは特定のプロパティなしで作成されます。
必要に応じてそれらを追加します。
デフォルトのモードです
** Schema-Hybrid – スキーマフルとスキーマレスの組み合わせです。
事前定義されたフィールドを持つクラスを作成することはできますが、レコードに他のカスタムフィールドを定義させることができます。
4.1. グラフAPI
これはグラフベースのデータベースなので、データはhttps://orientdb.com/docs/2.2.x/Graph-VE.html#vertices[Vertices](ノード)を含むネットワークとして表され、https://orientdbによって相互接続されます。 .com/docs/2.2.x/Graph-VE.html#edges[エッジ](円弧)。
最初のステップとして、ユーザー
admin
とパスワード
admin.
を持つ
BaeldungDB
という名前のGraphデータベースをUIを使って作成しましょう。
次の図に示すように、
graph
がデータベースタイプとして選択されているので、その結果、そのデータに
GRAPH Tab
でアクセスできるようになります。
リンク:/uploads/create-graph-db-1024×534.png%201024w[]
ORIENTDB
HOME
が
OrientDB__のインストールフォルダに対応する環境変数であることを確認しながら、目的のデータベースに接続しましょう。
@BeforeClass
public static void setup() {
String orientDBFolder = System.getenv("ORIENTDB__HOME");
graph = new OrientGraphNoTx("plocal:" + orientDBFolder +
"/databases/BaeldungDB", "admin", "admin");
}
フィールドに検証を追加する方法を示しながら、
Article
、
Author
、および
Editor
クラスを開始しましょう。
@BeforeClass
public static void init() {
graph.createVertexType("Article");
OrientVertexType writerType
= graph.createVertexType("Writer");
writerType.setStrictMode(true);
writerType.createProperty("firstName", OType.STRING);
//...
OrientVertexType authorType
= graph.createVertexType("Author", "Writer");
authorType.createProperty("level", OType.INTEGER).setMax("3");
OrientVertexType editorType
= graph.createVertexType("Editor", "Writer");
editorType.createProperty("level", OType.INTEGER).setMin("3");
Vertex vEditor = graph.addVertex("class:Editor");
vEditor.setProperty("firstName", "Maxim");
//...
Vertex vAuthor = graph.addVertex("class:Author");
vAuthor.setProperty("firstName", "Jerome");
//...
Vertex vArticle = graph.addVertex("class:Article");
vArticle.setProperty("title", "Introduction to ...");
//...
graph.addEdge(null, vAuthor, vEditor, "has");
graph.addEdge(null, vAuthor, vArticle, "wrote");
}
上記のコードスニペットでは、単純なデータベースを単純に表現しました。
-
Article
は記事を含むスキーマのないクラスです。 -
Writer
は必要なライターを保持するスキーマフルスーパークラスです。
情報
**
Writer
は、詳細を保持する
Author
のサブタイプです。
-
Editor
はエディタを保持する
Writer
のスキーマレスサブタイプです。
詳細
**
lastName
フィールドは保存された著者には入力されていませんが、それでもまだ
次のグラフに表示されます。
** すべてのクラス間に関係があります:
Author
は書くことができます
記事
を持つ必要があります
Editor
**
Vertex
はいくつかのフィールドを持つエンティティを表します
-
エッジ
は2つの__頂点をリンクするエンティティです。 -
フルクラスのオブジェクトに別のプロパティを追加しようとすると、http://orientdb.com/javadoc/latest/index.html?com/orientechnologies/orient/core/exception/になりますOValidationException.html[OValidationException]。**
OrientDB studio
を使用してデータベースに接続すると、データのグラフ表示が表示されます。
リンク:/uploads/orientdb-graph-1024×528.png%201024w[]
データベースの全レコード(頂点)の数を知る方法を見てみましょう。
long size = graph.countVertices();
それでは、
Writer(Author&Editor)
オブジェクトの数だけを見てみましょう。
@Test
public void givenBaeldungDB__checkWeHaveTwoWriters() {
long size = graph.countVertices("Writer");
assertEquals(2, size);
}
次のステップでは、次のステートメントを使用して、すべての
Writer
のデータを見つけることができます。
Iterable<Vertex> writers = graph.getVerticesOfClass("Writer");
最後に、
level
7ですべての__Editorを検索しましょう。ここでは、一致するものだけがあります。
@Test
public void givenBaeldungDB__getEditorWithLevelSeven() {
String onlyEditor = "";
for(Vertex v : graph.getVertices("Editor.level", 7)) {
onlyEditor = v.getProperty("firstName").toString();
}
assertEquals("Maxim", onlyEditor);
}
クラス名は、要求時に特定の頂点を探すために常に指定されています。詳細についてはhttps://orientdb.com/docs/2.2.x/Graph.co.jpbase-Tinkerpop.html[こちら]をご覧ください。
4.2. ドキュメントAPI
次のオプションはOrientDBのドキュメントモデルを使うことです。これは、タイプがテキスト、ピクチャ、またはバイナリ形式である可能性があるフィールドに格納された情報を持つ単純なレコードを介したデータ操作を公開します。
UIをもう一度使って
BaeldungDBTwo
という名前のデータベースを作成しましょう。 :
リンク:/uploads/create-document-db-1024×529.png%201024w[]
注:同様に、このAPIはスキーマフルモード、スキーマレスモード、またはスキーマハイブリッドモードのいずれでも使用できます。
データベース接続は、
ODatabaseDocumentTx
オブジェクトをインスタンス化し、データベースのURLとデータベースユーザーの資格情報を指定するだけでよいため、簡単なままです。
@BeforeClass
public static void setup() {
String orientDBFolder = System.getenv("ORIENTDB__HOME");
db = new ODatabaseDocumentTx("plocal:"
+ orientDBFolder + "/databases/BaeldungDBTwo")
.open("admin", "admin");
}
Author
情報を含む簡単な文書を保存することから始めましょう。**
これで、クラスが自動的に作成されたことがわかります。
@Test
public void givenDB__whenSavingDocument__thenClassIsAutoCreated() {
ODocument doc = new ODocument("Author");
doc.field("name", "Paul");
doc.save();
assertEquals("Author", doc.getSchemaClass().getName());
}
したがって、
Authors
の数を数えるには、次のようにします。
long size = db.countClass("Author");
leuthor
sオブジェクトを
level
7で検索するために、フィールド値を使用して文書をもう一度照会しましょう。
@Test
public void givenDB__whenSavingAuthors__thenWeGetOnesWithLevelSeven() {
for (ODocument author : db.browseClass("Author")) author.delete();
ODocument authorOne = new ODocument("Author");
authorOne.field("firstName", "Leo");
authorOne.field("level", 7);
authorOne.save();
ODocument authorTwo = new ODocument("Author");
authorTwo.field("firstName", "Lucien");
authorTwo.field("level", 9);
authorTwo.save();
List<ODocument> result = db.query(
new OSQLSynchQuery<ODocument>("select ** from Author where level = 7"));
assertEquals(1, result.size());
}
同様に、
Author
クラスのすべてのレコードを削除するには、次のようにします。
for (ODocument author : db.browseClass("Author")) {
author.delete();
}
OrientDBスタジオの
BROWSEタブ
では、すべての
__Authorの
__オブジェクトを取得するためのクエリを作成できます。
リンク:/uploads/orientdb-document-1024×434.png%201024w[]
4.3. オブジェクトAPI
OrientDBはデータベースのオブジェクトタイプを持っていません。したがって、Object APIはDocumentデータベースに依存しています。
Object APIタイプでは、他のすべての概念はPOJOへの追加1つだけで同じままです。
まず、
OObjectDatabaseTx
クラスを使用して
BaeldungDBThree
に接続します。
@BeforeClass
public static void setup() {
String orientDBFolder = System.getenv("ORIENTDB__HOME");
db = new OObjectDatabaseTx("plocal:"
+ orientDBFolder + "/databases/BaeldungDBThree")
.open("admin", "admin");
}
次に、
Author
が
Author
データを保持するために使用されるPOJOであると仮定することによって、それを登録する必要があります。
db.getEntityManager().registerEntityClass(Author.class);
Author
には、以下のフィールドの取得メソッドと設定メソッドがあります。
-
ファーストネーム
-
苗字
-
level
引数のないコンストラクタを承認したら、複数行の命令で
Author
を作成しましょう。
Author author = db.newInstance(Author.class);
author.setFirstName("Luke");
author.setLastName("Sky");
author.setLevel(9);
db.save(author);
一方、
Author
の
firstName
、
lastName
、および
level
をそれぞれとる別のコンストラクターがある場合、インスタンス化は1行になります。
Author author = db.newInstance(Author.class, "Luke", "Sky", 9);
db.save(author);
以下の行は、Authorクラスのすべてのレコードを閲覧および削除するために使用しています。
for (Author author : db.browseClass(Author.class)) {
db.delete(author);
}
すべての作者を数えるには、SQLクエリを書く必要なしにクラスとデータベースインスタンスを提供するだけです。
long authorsCount = db.countClass(Author.class);
同様に、次のように
level
7を使って著者に問い合わせます。
@Test
public void givenDB__whenSavingAuthors__thenWeGetOnesWithLevelSeven() {
for (Author author : db.browseClass(Author.class)) {
db.delete(author);
}
Author authorOne
= db.newInstance(Author.class, "Leo", "Marta", 7);
db.save(authorOne);
Author authorTwo
= db.newInstance(Author.class, "Lucien", "Aurelien", 9);
db.save(authorTwo);
List<Author> result
= db.query(new OSQLSynchQuery<Author>(
"select ** from Author where level = 7"));
assertEquals(1, result.size());
}
最後に、これはhttps://orientdb.com/docs/2.2.x/Object.co.jpbase.html[公式ガイド]で、高度なObject APIの使用法を紹介しています。
5結論
この記事では、OrientDBをそのJava APIを持つデータベース管理システムとして使用する方法を説明しました。また、フィールドに検証を追加し、いくつかの簡単なクエリを書く方法も学びました。
いつものように、この記事のソースコードはhttps://github.com/eugenp/tutorials/tree/master/persistence-modules/orientdb[GitHubに載っています]。