MarkLogicのデータベースにアクセスし、文字列の入ったドキュメントを作成するサンプルと、そのドキュメントから文字列を取得してくるサンプルです。今回は一番単純な例として、単純なテキスト文書を読み込むTextDocumentManager
と、単純な文字列として処理するStringHandle
を使いました。
参考:
http://docs.marklogic.com/guide/java/intro
MarkLogicのユーザーの作成
アプリケーションで使うユーザを事前に作成しておきます。
-
MarkLogicサーバーの管理コンソール( http://localhost:8001/ )に行き、右側のツリーからConfigure -> Security -> Users を選択
-
左側でCreateタブを選択し、以下の内容でユーザーを作成します。
ユーザー名 | パスワード | 必要なRole |
---|---|---|
aaa | pass | rest-reader, rest-writer |
Javaプロジェクトの作成
普通のJavaプロジェクトまたはMavenプロジェクトを作成します。
MarkLogic Java APIライブラリのダウンロード
入手方法は2通りあります。好みで。
- Mavenでダウンロード
<dependency>
<groupId>com.marklogic</groupId>
<artifactId>java-client-api</artifactId>
<version>RELEASE</version>
</dependency>
- MarkLogicのサイトからダウンロード
- http://developer.marklogic.com/products/java
- lib/の中のライブラリを全部クラスパスに追加
データベースに書き込むクラス
適当な名前でクラスを作成し、以下の内容でmainメソッドを書きます。
public static void main(String[] args) {
// データベースクライアントの取得
DatabaseClient client = DatabaseClientFactory.newClient(
"localhost", 8000, "aaa", "pass", DatabaseClientFactory.Authentication.DIGEST
);
// DocumentManagerの取得
TextDocumentManager textMgr = client.newTextDocumentManager();
// Handleの取得
StringHandle handle = new StringHandle();
// 文字列を設定
handle.set("Hello");
// 指定したURLに書き込む
textMgr.write("/example/text.txt", handle);
// データベースクライアントの解放
client.release();
}
実行
JDK1.7以降でビルドして実行します。
15:59:04.685 [main] DEBUG c.m.client.DatabaseClientFactory - Creating new database client for server at localhost:8000
15:59:04.705 [main] DEBUG c.m.client.impl.JerseyServices - Connecting to localhost at 8000 as aaa
15:59:05.001 [main] INFO c.m.client.impl.DocumentManagerImpl - Writing content for /example/text.txt
15:59:05.004 [main] DEBUG c.m.client.impl.JerseyServices - Sending /example/text.txt document in transaction null
15:59:05.155 [main] INFO c.m.client.impl.DatabaseClientImpl - Releasing connection
15:59:05.155 [main] DEBUG c.m.client.impl.JerseyServices - Releasing connection
クエリコンソールで、Exploreボタンをクリックします。
http://localhost:8000/qconsole/
/example/text.txtというドキュメントが増えています。
ドキュメントのリンクをクリックすると、設定した文字列が表示されます。
データベースから読み込むクラス
適当な名前でクラスを作成し、以下の内容でmainメソッドを書きます。
public static void main(String[] args) {
// データベースクライアントの取得
DatabaseClient client = DatabaseClientFactory.newClient(
"localhost", 8000, "aaa", "pass", DatabaseClientFactory.Authentication.DIGEST
);
// DocumentManagerの取得
TextDocumentManager textMgr = client.newTextDocumentManager();
// Handleの取得
StringHandle handle = new StringHandle();
// 指定されたURLから読み込む
textMgr.read("/example/text.txt", handle);
// 文字列を読み出し
String str = handle.get();
// 内容を出力
System.out.println("String: " + str);
// データベースクライアントの解放
client.release();
}
実行
ビルドして実行します。
16:10:33.240 [main] DEBUG c.m.client.DatabaseClientFactory - Creating new database client for server at localhost:8000
16:10:33.266 [main] DEBUG c.m.client.impl.JerseyServices - Connecting to localhost at 8000 as aaa
16:10:33.620 [main] INFO c.m.client.impl.DocumentManagerImpl - Reading metadata and content for /example/text.txt
16:10:33.621 [main] DEBUG c.m.client.impl.JerseyServices - Getting /example/text.txt in transaction null
String: Hello
16:10:34.543 [main] INFO c.m.client.impl.DatabaseClientImpl - Releasing connection
16:10:34.543 [main] DEBUG c.m.client.impl.JerseyServices - Releasing connection
ドキュメントに入っている文字列が、コンソールに表示されます。