外部アプリケーションの接続

アプリケーションとドライバーは、接続ストリングを使用して IBM Cloud® Databases for Elasticsearch への接続を行います。 デプロイメントごとに、ドライバーとアプリケーションのための専用の接続文字列があります。 接続文字列は、配置の [概要] ページの [エンドポイント] パネルに表示され、クラウド データベース CLI プラグインAPI からも取得できます。

接続文字列は、配置で作成した任意の資格情報で使用できます。 すべての接続およびアプリケーションで管理ユーザーを使用することが可能ですが、使用するアプリケーションに接続するための専用のユーザーを作成する方がよい場合もあります。 詳細は 接続文字列の取得 を参照してください。

言語のドライバーによる接続

ドライバがデプロイメントに接続するために必要なすべての情報は、サービス資格情報ページで作成された資格情報の「https」セクションにあります。 参照用に、接続文字列の構成要素を次の表に示します。

https/URI接続情報
フィールド名 索引 説明
Type 接続のタイプ - Elasticsearch の場合は「uri」。
Scheme URIのスキーム - Elasticsearch の場合は "https"。
Path uriのパス。
Authentication Username 接続に使用するユーザー名。
Authentication Password ユーザーのパスワードは、 $PASSWORD のように表示されます。
Authentication Method 認証が行われる方法。「direct」認証はドライバーによって処理されます。
Hosts 0... ホスト名と接続先ポート。
Composed 0... Scheme、Authentication、Host、Pathを組み合わせたURI。
Certificate Name データベース展開のためのサービス専有証明書の割り当て名。
Certificate 基本 64 base64 でエンコードされたバージョンの証明書
  • 0... は、1 つの配列にこれらのエントリーが 1 つ以上ある可能性があることを示しています。

多くの Elasticsearch ドライバーは、接続情報の「composed」フィールドで特定の URI フォーマットの接続ストリングを検出した場合に、デプロイメントへの接続を行えます。 以下の例を参照してください。

https://admin:$PASSWORD@d5eeee66-5bc4-498a-b73b-1307848f1eac.8f7bfd8f3faa4218aec56e069eb46187.databases.appdomain.cloud:31821

以下の例では、Java を使用して接続します。

import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.client.JestClient;
import io.searchbox.cluster.Health;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContextBuilder;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.security.*;
import java.security.cert.CertificateException;


import java.io.IOException;


public class ESConnect {

    public static void main(String[] args) {

        // Add CA cert to truststore using something like:
        //  keytool -import -alias mycert -file /path/to/cert -keystore ./mycert -storetype pkcs12 -storepass mysecret
        // and use the path of the keystore below
        File truststore = new File("/Users/code/java-example/icdcerts");

        try {
            // use your secret and add the variable containing the truststore that you created above with the secret as a CharArray
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(truststore, "mysecret".toCharArray()).build();

            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);

            // set up a Jest factory
            JestClientFactory factory = new JestClientFactory();

            //configure and build Jest HTTP client with IBM Cloud Databases for Elasticsearch connection strings
            factory.setHttpClientConfig(
                    // add the Elasticsearch host and port
                    new HttpClientConfig.Builder("https://60d1b41b-2478-4767-9fc0-d99b1d00b6d1.bkvfu0nd0m8k95k94ujg.databases.appdomain.cloud:31347")
                            .multiThreaded(true)
                            // Add the credentials username and password
                            .defaultCredentials("admin", "mypassword")
                            .sslSocketFactory(sslSocketFactory)
                            .build());

            // create a JestClient
            JestClient client = factory.getObject();
            // create the call for the cluster health
            Health health = new Health.Builder().build();
            // get the cluster health as a JestResult
            JestResult result = client.execute(health);
            // print out the cluster's health
            System.out.printf("\n\n<------ CLUSTER HEALTH ------>\n%s\n\n", result.getJsonObject());
            // shutdown the connection
            client.close();

        } catch (IOException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException | CertificateException e) {
            e.printStackTrace();
        }

    }

}

以下の例では、Python ライブラリー elasticsearch-py を使用して接続します。

from elasticsearch import Elasticsearch
from ssl import create_default_context


context = create_default_context(cafile="path/to/cert.pem")

es = Elasticsearch(
    ['60d1b41b-2478-4767-9fc0-d99b1d00b6d1.bkvfu0nd0m8k95k94ujg.databases.appdomain.cloud'],
    http_auth=('admin', 'password'),
    port='31347',
    ssl_context=context
)

health = es.cluster.health()
print(health)

ドライバーTLSおよびサービス独自証明書のサポート

Databases for Elasticsearch への接続はすべて TLS 1.2 対応になっているため、接続に使用するドライバーは暗号化をサポートできる必要があります。 また、ドライバが接続時にサーバを検証できるように、配備にはサービス専有証明書も付属しています。

詳しくは、 Cloud Databases Certificates FAQを参照してください。

サービス独自証明書の使用

  1. Endpoints」 パネルまたはサービス・クレデンシャル接続情報の「 Base64 」フィールドから証明書情報をコピーする。
  2. 必要に応じて、Base64 ストリングをテキストにデコードします。
  3. 証明書をファイルに保存します (提供される名前、または独自のファイル名を使用できます)。
  4. ドライバーまたはクライアントに証明書へのパスを提供します。

サービス独自証明書のCLIプラグインサポート

コマンド ibmcloud cdb deployment-cacert "your-service-name" を使用して、デプロイメントのデコードされた証明書を CLI プラグインと共に表示することができます。 これにより、base64 がテキストにデコードされます。 コマンドの出力をコピーしてファイルに保存し、そのファイルのパスをドライバーに提供します。

その他のドライバー

Elasticsearch には、さまざまな言語ドライバーがあります。 最もよく使用されているドライバーを以下の表にいくつか示します。

一般的なElasticsearchドライバ
言語 ドライバー 資料
Node elasticsearch-js リンク
Ruby elasticsearch-ruby リンク
Ruby on Rails elasticsearch-rails リンク
Python elasticsearch-py リンク
Java Jest リンク
Go elastic リンク