-
-
Save anishi1222/a998a1c8fa87d166aaa39a14b6308ad3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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