Skip to content

Commit e8dba25

Browse files
authored
Merge pull request #204367 from kushagraThapar/non_partitioned_container_migration_sample_java_sdk
Added sample to create / read items from non partitioned container
2 parents b81ec7f + bf80dc6 commit e8dba25

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

articles/cosmos-db/sql/migrate-containers-partitioned-to-nonpartitioned.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ The following example shows a sample code to create a document with the system d
4848

4949
**JSON representation of the document**
5050

51+
### [.NET SDK V3](#tab/dotnetv3)
52+
5153
```csharp
5254
DeviceInformationItem = new DeviceInformationItem
5355
{
@@ -109,7 +111,57 @@ await migratedContainer.Items.ReadItemAsync<DeviceInformationItem>(
109111

110112
```
111113

112-
For the complete sample on how to repartition the documents, see the [.Net samples][1] GitHub repository.
114+
### [Java SDK V4](#tab/javav4)
115+
116+
```java
117+
static class Family {
118+
public String id;
119+
public String firstName;
120+
public String lastName;
121+
public String _partitionKey;
122+
123+
public Family(String id, String firstName, String lastName, String _partitionKey) {
124+
this.id = id;
125+
this.firstName = firstName;
126+
this.lastName = lastName;
127+
this._partitionKey = _partitionKey;
128+
}
129+
}
130+
131+
...
132+
133+
CosmosDatabase cosmosDatabase = cosmosClient.getDatabase("testdb");
134+
CosmosContainer cosmosContainer = cosmosDatabase.getContainer("testcontainer");
135+
136+
// Create single item
137+
Family family = new Family("id-1", "John", "Doe", "Doe");
138+
cosmosContainer.createItem(family, new PartitionKey(family._partitionKey), new CosmosItemRequestOptions());
139+
140+
// Create items through bulk operations
141+
family = new Family("id-2", "Jane", "Doe", "Doe");
142+
CosmosItemOperation createItemOperation = CosmosBulkOperations.getCreateItemOperation(family,
143+
new PartitionKey(family._partitionKey));
144+
cosmosContainer.executeBulkOperations(Collections.singletonList(createItemOperation));
145+
```
146+
147+
For the complete sample, see the [.Net samples][1] GitHub repository.
148+
149+
## Migrate the documents
150+
151+
While the container definition is enhanced with a partition key property, the documents within the container arent auto migrated. Which means the system partition key property `/_partitionKey` path is not automatically added to the existing documents. You need to repartition the existing documents by reading the documents that were created without a partition key and rewrite them back with `_partitionKey` property in the documents.
152+
153+
## Access documents that don't have a partition key
154+
155+
Applications can access the existing documents that dont have a partition key by using the special system property called "PartitionKey.None", this is the value of the non-migrated documents. You can use this property in all the CRUD and query operations. The following example shows a sample to read a single Document from the NonePartitionKey.
156+
157+
```java
158+
CosmosItemResponse<JsonNode> cosmosItemResponse =
159+
cosmosContainer.readItem("itemId", PartitionKey.NONE, JsonNode.class);
160+
```
161+
162+
For the complete sample on how to repartition the documents, see the [Java samples][2] GitHub repository.
163+
164+
---
113165

114166
## Compatibility with SDKs
115167

@@ -135,4 +187,5 @@ If new items are inserted with different values for the partition key, querying
135187
* If all you know is the number of vcores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md)
136188
* If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-with-capacity-planner.md)
137189

138-
[1]: https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/NonPartitionContainerMigration
190+
[1]: https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/NonPartitionContainerMigration
191+
[2]: https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/tree/main/src/main/java/com/azure/cosmos/examples/nonpartitioncontainercrud

0 commit comments

Comments
 (0)