2
1

More than 5 years have passed since last update.

Azure Cosmos DB に Java SDK からSelect*

Posted at

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();
    }
}


2
1
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
2
1