Select * from Java SDK to Azure Cosmos DB

Maven

<dependency>
	<groupId>com.microsoft.azure</groupId>
	<artifactId>azure-documentdb</artifactId>
	<version>2.4.0</version>
</dependency>

Java

import java.util.List;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.microsoft.azure.documentdb.ConnectionPolicy;
import com.microsoft.azure.documentdb.ConsistencyLevel;
import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.FeedOptions;

public class HelloDocumentDb002QueryDocument {

	static String DATABASE_ID = "youdatabase";
	static String COLLECTION_ID = "yourcontainer";

	public static void main(String[] args) throws Exception {

		FeedOptions queryOptions = new FeedOptions();
		queryOptions.setEnableCrossPartitionQuery(true);

		String host = "yourhost";
		// read only
		String key = "youkey-readonly";

		DocumentClient client = new DocumentClient("https://" //
				+ host //
				+ ".documents.azure.com:443", key, //
				new ConnectionPolicy(), ConsistencyLevel.Session);

		String q = "SELECT * FROM c";

		List<Document> results = client
				.queryDocuments("dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID, q, queryOptions).getQueryIterable()
				.toList();

		for (Document doc : results) {
			System.err.println(doc);
			String json = doc.toString();
			System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(json)));

		}
		client.close();
	}
}


Recommended Posts

Select * from Java SDK to Azure Cosmos DB
Upsert from Java SDK to Azure Cosmos DB
Delete All from Java SDK in Azure Cosmos DB
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Delete documents that don't have PK in Azure Cosmos DB (Java SDK)
From Java to Ruby !!
Randomly select 100 records from DB
New features from Java7 to Java8
Connect from Java to PostgreSQL
Connect to DB with Java
From Ineffective Java to Effective Java
Append text to BlobItem in Azure BlobStorage SDK Java V8
Java to be involved from today
Java, interface to start from beginner
Change DB from SQLite to MySQL
The road from JavaScript to Java
[Java] Conversion from array to List
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
Try using Microsoft Azure SDK For SQL API of Azure Cosmos DB Service 4.6
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Precautions when migrating from VB6.0 to JAVA
Creating Java Web Applications to Azure Web Apps
Get Timestamp with Azure BlobStorage Java SDK
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Update if the document already exists in Azure Cosmos DB Java SDK, create new if it does not exist
Try calling Watson NLU that seems to support Japanese from the Java SDK
[Java] From two Lists to one array list
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
Deploy Java web app to Azure with maven
To become a VB.net programmer from a Java shop
How to dump from database (DB) to seeds file
Migrate from Java to Server Side Kotlin + Spring-boot
Java upload and download notes to Azure storage
[Java] Convert DB code to code value using enum
How to get Class from Element in Java
[Java] How to switch from open jdk to oracle jdk
I want to write quickly from java to sqlite
Minecraft BE server development from PHP to Java
[Java] Introduction to Java
Introduction to java
Launch Docker from Java to convert Office documents to PDF
Convert Java enum enums and JSON to and from Jackson
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
Sum from Java_1 to 100
I tried to operate SQS using AWS Java SDK
[Java] How to extract the file name from the path
Use aggregate queries (Count) with Azure CosmosDB Java SDK
6 features I missed after returning to Java from Scala
Try calling IBM Watson Assistant 2018-07-10 from the Java SDK.
Java development for beginners to start from 1-Vol.1-eclipse setup
Android-Upload image files to Azure Blob Storage in Java
How to change from Oracle Java 8 to Adopt Open JDK 9
[Java] How to erase a specific character from a character string