LoginSignup
0

More than 5 years have passed since last update.

MarkLogic アプリ開発チュートリアル:単純な Java アプリケーション

Posted at

MarkLogicのデータベースにアクセスし、文字列の入ったドキュメントを作成するサンプルと、そのドキュメントから文字列を取得してくるサンプルです。今回は一番単純な例として、単純なテキスト文書を読み込むTextDocumentManagerと、単純な文字列として処理するStringHandleを使いました。

参考:
http://docs.marklogic.com/guide/java/intro

MarkLogicのユーザーの作成

アプリケーションで使うユーザを事前に作成しておきます。

  1. MarkLogicサーバーの管理コンソール( http://localhost:8001/ )に行き、右側のツリーからConfigure -> Security -> Users を選択
    userpane.png

  2. 左側でCreateタブを選択し、以下の内容でユーザーを作成します。

ユーザー名 パスワード 必要なRole
aaa pass rest-reader, rest-writer

Javaプロジェクトの作成

普通のJavaプロジェクトまたはMavenプロジェクトを作成します。

MarkLogic Java APIライブラリのダウンロード

入手方法は2通りあります。好みで。

  • Mavenでダウンロード
pom.xml
<dependency>
    <groupId>com.marklogic</groupId>
    <artifactId>java-client-api</artifactId>
    <version>RELEASE</version>
</dependency>

データベースに書き込むクラス

適当な名前でクラスを作成し、以下の内容でmainメソッドを書きます。

WriterSample.java
    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というドキュメントが増えています。
list.png

ドキュメントのリンクをクリックすると、設定した文字列が表示されます。
hello.png

データベースから読み込むクラス

適当な名前でクラスを作成し、以下の内容でmainメソッドを書きます。

ReadSample.java
    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

ドキュメントに入っている文字列が、コンソールに表示されます。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0