Skip to content

Instantly share code, notes, and snippets.

@anishi1222
Last active December 13, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anishi1222/a998a1c8fa87d166aaa39a14b6308ad3 to your computer and use it in GitHub Desktop.
Save anishi1222/a998a1c8fa87d166aaa39a14b6308ad3 to your computer and use it in GitHub Desktop.
public void testBed(Customer targetCustomer, String[] operation) {
try (CosmosClient client = new CosmosClientBuilder()
.endpoint(ENDPOINT)
.key(KEY)
.consistencyLevel(ConsistencyLevel.SESSION)
.contentResponseOnWriteEnabled(true)
.preferredRegions(List.of("Japan East"))
.readRequestsFallbackEnabled(true)
.endpointDiscoveryEnabled(true)
.gatewayMode()
.connectionSharingAcrossClientsEnabled(true)
.buildClient()) {
CosmosContainer container = client
.getDatabase(DATABASE)
.getContainer(CONTAINER);
TransactionalBatch txBatch = TransactionalBatch
.createTransactionalBatch(new PartitionKey(targetCustomer.getMyPartitionKey()));
boolean illegalOperation = false;
for (String s : operation) {
switch (s.toUpperCase()) {
case "CREATE":
// add Create Operation
txBatch.createItemOperation(targetCustomer);
break;
case "REPLACE":
// add Replace Operation
targetCustomer.setCity("きょうと");
show(s.toUpperCase(), targetCustomer);
txBatch.replaceItemOperation(targetCustomer.getId(), targetCustomer);
break;
case "UPSERT":
// add Upsert Operation
targetCustomer.setRegion("日本のどこか");
show(s.toUpperCase(), targetCustomer);
txBatch.upsertItemOperation(targetCustomer);
break;
case "DELETE":
// add Delete Operation
show(s.toUpperCase(), targetCustomer);
txBatch.deleteItemOperation(targetCustomer.getId());
break;
case "READ":
// add read operation
txBatch.readItemOperation(targetCustomer.getId()).getItem();
break;
default:
illegalOperation = true;
break;
}
}
if (!illegalOperation) {
TransactionalBatchResponse txResponse = container.executeTransactionalBatch(txBatch);
txResponse.getResults().forEach(txResult -> logger.info(
"Result [" + txResult.getStatusCode() + "] - [" + txResult.getSubStatusCode() + "] Operation: " + txResult.getOperation().getOperationType().name()));
}
} catch (CosmosException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment