[JAVA] Try using Microsoft Azure SDK For SQL API of Azure Cosmos DB Service 4.6

Azure CosmosDB Java SDK version 4.x will be released in 2020.

The usage is published in the following document, but I tried to make what will happen as the minimum Hello World code. 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>

You can get the information you need to connect in the 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

that's all.

Recommended Posts

Try using Microsoft Azure SDK For SQL API of Azure Cosmos DB Service 4.6
Try similar search of Image Search using Java SDK [Search]
Try using S3Proxy with Microsoft Azure Blob Storage
Select * from Java SDK to Azure Cosmos DB
Delete All from Java SDK in Azure Cosmos DB
Try putting a lot of data into Cosmos DB
Try Spark Submit to EMR using AWS SDK for Java