|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.cosmos.examples.nonpartitioncontainercrud; |
| 5 | + |
| 6 | +import com.azure.cosmos.CosmosClient; |
| 7 | +import com.azure.cosmos.CosmosClientBuilder; |
| 8 | +import com.azure.cosmos.CosmosContainer; |
| 9 | +import com.azure.cosmos.CosmosDatabase; |
| 10 | +import com.azure.cosmos.examples.common.AccountSettings; |
| 11 | +import com.azure.cosmos.models.CosmosBulkOperations; |
| 12 | +import com.azure.cosmos.models.CosmosItemOperation; |
| 13 | +import com.azure.cosmos.models.CosmosItemRequestOptions; |
| 14 | +import com.azure.cosmos.models.CosmosItemResponse; |
| 15 | +import com.azure.cosmos.models.PartitionKey; |
| 16 | +import com.fasterxml.jackson.databind.JsonNode; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | + |
| 22 | +public class NonPartitionContainerCrudQuickStart { |
| 23 | + |
| 24 | + private static final Logger logger = LoggerFactory.getLogger(NonPartitionContainerCrudQuickStart.class); |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + CosmosClient cosmosClient = new CosmosClientBuilder() |
| 28 | + .endpoint(AccountSettings.HOST) |
| 29 | + .key(AccountSettings.MASTER_KEY) |
| 30 | + .contentResponseOnWriteEnabled(true) |
| 31 | + .buildClient(); |
| 32 | + |
| 33 | + // Get the database and container which are pre-created using v2 versions of the SDK |
| 34 | + // such that they don't contain partition key definition. |
| 35 | + CosmosDatabase cosmosDatabase = cosmosClient.getDatabase("testdb"); |
| 36 | + CosmosContainer cosmosContainer = cosmosDatabase.getContainer("testcontainer"); |
| 37 | + |
| 38 | + |
| 39 | + Family family = new Family("id-1", "John", "Doe", "Doe"); |
| 40 | + logger.info("Creating Item"); |
| 41 | + cosmosContainer.createItem(family, new PartitionKey(family._partitionKey), new CosmosItemRequestOptions()); |
| 42 | + logger.info("Item created"); |
| 43 | + |
| 44 | + logger.info("Creating items through bulk"); |
| 45 | + family = new Family("id-2", "Jane", "Doe", "Doe"); |
| 46 | + CosmosItemOperation createItemOperation = CosmosBulkOperations.getCreateItemOperation(family, |
| 47 | + new PartitionKey(family._partitionKey)); |
| 48 | + cosmosContainer.executeBulkOperations(Collections.singletonList(createItemOperation)); |
| 49 | + logger.info("Items through bulk created"); |
| 50 | + |
| 51 | + // To read an existing document which doesn't have any partition key associated with it, |
| 52 | + // read it using PartitionKey.NONE |
| 53 | + CosmosItemResponse<JsonNode> cosmosItemResponse = cosmosContainer.readItem("itemId", PartitionKey.NONE, JsonNode.class); |
| 54 | + logger.info("Cosmos Item response is : {}", cosmosItemResponse.getItem().toPrettyString()); |
| 55 | + } |
| 56 | + |
| 57 | + static class Family { |
| 58 | + public String id; |
| 59 | + public String firstName; |
| 60 | + public String lastName; |
| 61 | + public String _partitionKey; |
| 62 | + |
| 63 | + public Family(String id, String firstName, String lastName, String _partitionKey) { |
| 64 | + this.id = id; |
| 65 | + this.firstName = firstName; |
| 66 | + this.lastName = lastName; |
| 67 | + this._partitionKey = _partitionKey; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments