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