LoginSignup
0
0

More than 3 years have passed since last update.

Microsoft Azure SDK For SQL API of Azure Cosmos DB Service 4.6 を使ってみる

Last updated at Posted at 2020-10-06

Azure CosmosDB Java SDK version 4.x が2020年に公開されております。

以下ドキュメントに使い方が公開されているのですが、最低限のHello Worldコードとしてはどうなるのかを作ってみました。
https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-java-v4

pom.xml

<!-- https://mvnrepository.com/artifact/com.azure/azure-cosmos -->
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-cosmos</artifactId>
    <version>4.6.0</version>
</dependency>

接続に必要な情報はAzure Portalで取得できます。

image.png

package hello.cosmosdb;

import java.util.Iterator;

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.util.CosmosPagedIterable;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 
 * https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-java-v4
 * https://docs.microsoft.com/en-us/java/api/overview/azure/cosmos-readme?view=azure-java-stable
 * https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/367c796257feb37d2f8003a86fe51b0658fcf6ff/src/main/java/com/azure/cosmos/examples/queries/sync/QueriesQuickstart.java#L119
 * 
 * @author Hiroki Oya
 *
 */
public class HelloCosmosDbJavaSDK_V4 {

    private CosmosDatabase database;
    private CosmosContainer container;

    public void query() throws Exception {

        // [Azure Portal] - [your_cosmosdb] - [Keys] - [URI]
        String serviceEndpoint = "https://your_cosmosdb.documents.azure.com:443/";

        // [Azure Portal] - [your_cosmosdb] - [Keys] - [PRIMARY KEY | SECONDARY KEY]
        String key = "your_key_here";

        // [Azure Portal] - [your_cosmosdb] - [Data Explorer]
        String databaseId = "TEST";

        // [Azure Portal] - [your_cosmosdb] - [Data Explorer]
        String containerId = "container_for_test";

        String query = "SELECT * from c";

        CosmosClient client = new CosmosClientBuilder() //
                .endpoint(serviceEndpoint) //
                .key(key) //
                .buildClient();

        database = client.getDatabase(databaseId);
        container = database.getContainer(containerId);

        CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();

        CosmosPagedIterable<JsonNode> page = container.queryItems(query, options, JsonNode.class);

        Iterator<JsonNode> it = page.iterator();

        while (it.hasNext()) {
            System.err.println(it.next());
        }

        client.close();

    }

    public static void main(String[] args) throws Exception {
        HelloCosmosDbJavaSDK_V4 h = new HelloCosmosDbJavaSDK_V4();
        h.query();
    }

}

Reference

Azure Cosmos DB Java SDK v4 for SQL API release notes and resources | Microsoft Docs
https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-java-v4

Maven Repository: com.azure » azure-cosmos
https://mvnrepository.com/artifact/com.azure/azure-cosmos

Azure CosmosDB Client Library for Java | Microsoft Docs
https://docs.microsoft.com/en-us/java/api/overview/azure/cosmos-readme?view=azure-java-stable

azure-cosmos-java-sql-api-samples/QueriesQuickstart.java at 367c796257feb37d2f8003a86fe51b0658fcf6ff · Azure-Samples/azure-cosmos-java-sql-api-samples · GitHub
https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/367c796257feb37d2f8003a86fe51b0658fcf6ff/src/main/java/com/azure/cosmos/examples/queries/sync/QueriesQuickstart.java

Azure Cosmos DB Workshop - Querying in Azure Cosmos DB
https://cosmosdb.github.io/labs/dotnet/labs/03-querying_in_azure_cosmosdb.html

以上です。

0
0
0

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
0