com.microsoft.azure.documentdb.DocumentClientException Java Examples
The following examples show how to use
com.microsoft.azure.documentdb.DocumentClientException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SinglePartitionCollectionDocumentCrudSample.java From azure-documentdb-java with MIT License | 6 votes |
private void replaceOffer(DocumentCollection collection, int desiredOfferThroughput) throws DocumentClientException { FeedResponse<Offer> offers = client.queryOffers(String.format("SELECT * FROM c where c.offerResourceId = '%s'", collection.getResourceId()), null); Iterator<Offer> offerIterator = offers.getQueryIterator(); if (!offerIterator.hasNext()) { throw new IllegalStateException("Cannot find Collection's corresponding offer"); } Offer offer = offerIterator.next(); int collectionThroughput = offer.getContent().getInt("offerThroughput"); assertThat(collectionThroughput, equalTo(400)); offer.getContent().put("offerThroughput", desiredOfferThroughput); Offer newOffer = client.replaceOffer(offer).getResource(); assertThat(newOffer.getContent().getInt("offerThroughput"), equalTo(desiredOfferThroughput)); }
Example #2
Source File: DocumentQuerySamples.java From azure-documentdb-java with MIT License | 6 votes |
@Before public void setUp() throws DocumentClientException { // create client client = new DocumentClient(AccountCredentials.HOST, AccountCredentials.MASTER_KEY, null, null); // removes the database "exampleDB" (if exists) deleteDatabase(); // create database exampleDB; createDatabase(); // create collection createMultiPartitionCollection(); // populate documents populateDocuments(); }
Example #3
Source File: DocumentQuerySamples.java From azure-documentdb-java with MIT License | 6 votes |
private void populateDocuments() throws DocumentClientException { String[] cities = new String [] { "Amherst", "Seattle", "Redmond", "北京市", "बंगलौर - विकिपीडिया", "شیراز" }; String[] poetNames = new String[] { "Emily Elizabeth Dickinson", "Hafez", "Lao Tzu" }; for(int i = 0; i < 100; i++) { Document documentDefinition = new Document(); documentDefinition.setId("test-document" + i); documentDefinition.set("city", cities[i % cities.length]); documentDefinition.set("cnt", i); documentDefinition.set("poetName", poetNames[ i % poetNames.length ]); documentDefinition.set("popularity", RandomUtils.nextDouble(0, 1)); client.createDocument(collectionLink, documentDefinition, null, true); } }
Example #4
Source File: StoredProcedureSamples.java From azure-documentdb-java with MIT License | 6 votes |
@Test public void executeStoredProcWithArgs() throws DocumentClientException { // create stored procedure StoredProcedure storedProcedure = new StoredProcedure( "{" + " 'id': 'multiplySample'," + " 'body':" + " 'function (value, num) {" + " getContext().getResponse().setBody(" + " \"2*\" + value + \" is \" + num * 2 );" + " }'" + "}"); client.createStoredProcedure(collection.getSelfLink(), storedProcedure, null).getResource(); // execute stored procedure RequestOptions requestOptions = new RequestOptions(); requestOptions.setPartitionKey(new PartitionKey("Seattle")); String storedProcLink = String.format("/dbs/%s/colls/%s/sprocs/%s", databaseId, collectionId, "multiplySample"); Object[] storedProcedureArgs = new Object[] {"a", 123}; String storedProcResultAsString = client.executeStoredProcedure(storedProcLink, requestOptions, storedProcedureArgs).getResponseAsString(); assertThat(storedProcResultAsString, equalTo("\"2*a is 246\"")); }
Example #5
Source File: DocumentBulkImporter.java From azure-documentdb-java with MIT License | 6 votes |
/** * Initializes {@link DocumentBulkImporter}. This happens only once * @throws DocumentClientException */ private void initialize() throws DocumentClientException { logger.debug("Initializing ..."); this.bulkImportStoredProcLink = String.format("%s/sprocs/%s", collectionLink, BULK_IMPORT_STORED_PROCECURE_NAME); logger.trace("Fetching partition map of collection"); Range<String> fullRange = new Range<String>( PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey, true, false); this.collectionRoutingMap = getCollectionRoutingMap(client); Collection<PartitionKeyRange> partitionKeyRanges = this.collectionRoutingMap.getOverlappingRanges(fullRange); this.partitionKeyRangeIds = partitionKeyRanges.stream().map(partitionKeyRange -> partitionKeyRange.getId()).collect(Collectors.toList()); logger.debug("Initialization completed"); }
Example #6
Source File: DocumentBulkImporter.java From azure-documentdb-java with MIT License | 6 votes |
private void safeInit() throws Exception { int count = 0; long startTime = System.currentTimeMillis(); while(true) { try { initialize(); break; } catch (Exception e) { count++; DocumentClientException dce = ExceptionUtils.getThrottelingException(e); long now = System.currentTimeMillis(); if (count < retryOptions.getMaxRetryAttemptsOnThrottledRequests() && now - startTime < (retryOptions.getMaxRetryWaitTimeInSeconds() * 1000) && dce != null && dce.getStatusCode() == HttpConstants.StatusCodes.TOO_MANY_REQUESTS ) { Thread.sleep(count * dce.getRetryAfterInMilliseconds() + INITIALIZATION_SLEEP_TIME_ON_THROTTLING); continue; } else { throw e; } } } }
Example #7
Source File: DatabaseCrudSamples.java From azure-documentdb-java with MIT License | 6 votes |
@Test public void databaseCreateAndQuery() throws DocumentClientException { Database databaseDefinition = new Database(); databaseDefinition.setId(databaseId); RequestOptions options = new RequestOptions(); client.createDatabase(databaseDefinition, options); FeedResponse<Database> queryResults = client.queryDatabases(String.format("SELECT * FROM r where r.id = '%s'", databaseId), null); Iterator<Database> it = queryResults.getQueryIterator(); assertThat(it.hasNext(), equalTo(true)); Database foundDatabase = it.next(); assertThat(foundDatabase.getId(), equalTo(databaseDefinition.getId())); }
Example #8
Source File: DocumentQuerySamples.java From azure-documentdb-java with MIT License | 6 votes |
@Test public void simpleDocumentQuery() throws DocumentClientException { // as this is a multi collection enable cross partition query FeedOptions options = new FeedOptions(); options.setEnableCrossPartitionQuery(true); Iterator<Document> it = client.queryDocuments(collectionLink, "SELECT * from r", options).getQueryIterator(); int i = 0; while(it.hasNext()) { Document d = it.next(); System.out.println("id is " + d.getId()); System.out.println("citi is " + d.getString("city")); System.out.println("cnt is " + d.getInt("cnt")); System.out.println("popularity is " + d.getDouble("popularity")); i++; } assertThat(i, equalTo(100)); }
Example #9
Source File: DocumentQuerySamples.java From azure-documentdb-java with MIT License | 6 votes |
@Test public void orderByQuery() throws DocumentClientException { // as this is a multi collection enable cross partition query FeedOptions options = new FeedOptions(); options.setEnableCrossPartitionQuery(true); Iterator<Document> it = client.queryDocuments(collectionLink, "SELECT * from r ORDER BY r.cnt", options).getQueryIterator(); int i = 0; while(it.hasNext()) { Document d = it.next(); System.out.println("id is " + d.getId()); System.out.println("citi is " + d.getString("city")); System.out.println("cnt is " + d.getInt("cnt")); System.out.println("popularity is " + d.getDouble("popularity")); assertThat(d.getInt("cnt"), equalTo(i)); i++; } assertThat(i, equalTo(100)); }
Example #10
Source File: CollectionCrudSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Test public void deleteCollection() throws DocumentClientException { String databaseLink = String.format("/dbs/%s", databaseId); String collectionId = "testCollection"; // create collection DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(collectionId); client.createCollection(databaseLink, collectionDefinition, null); // delete collection String collectionLink = String.format("/dbs/%s/colls/%s", databaseId, collectionId); client.deleteCollection(collectionLink, null); }
Example #11
Source File: EndToEndTestBase.java From azure-documentdb-java with MIT License | 5 votes |
public void cleanUpDatabase(DocumentClient client) throws DocumentClientException { try { if (client != null) { client.deleteDatabase("/dbs/" + databaseId, null); } } catch (Exception e) { } }
Example #12
Source File: TestUtils.java From azure-documentdb-java with MIT License | 5 votes |
public static DocumentClientException getThrottleException() { DocumentClientException e = Mockito.mock(DocumentClientException.class); when(e.getStatusCode()).thenReturn(429); when(e.getRetryAfterInMilliseconds()).thenReturn(1l); return e; }
Example #13
Source File: DocumentQuerySamples.java From azure-documentdb-java with MIT License | 5 votes |
private void createMultiPartitionCollection() throws DocumentClientException { String databaseLink = String.format("/dbs/%s", databaseId); DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(collectionId); // set /city as the partition key path PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); Collection<String> paths = new ArrayList<String>(); paths.add(partitionKeyPath); partitionKeyDefinition.setPaths(paths); collectionDefinition.setPartitionKey(partitionKeyDefinition); // set the throughput to be 10,200 RequestOptions options = new RequestOptions(); options.setOfferThroughput(10200); // set indexing policy to be range range for string and number IndexingPolicy indexingPolicy = new IndexingPolicy(); Collection<IncludedPath> includedPaths = new ArrayList<IncludedPath>(); IncludedPath includedPath = new IncludedPath(); includedPath.setPath("/*"); Collection<Index> indexes = new ArrayList<Index>(); Index stringIndex = Index.Range(DataType.String); stringIndex.set("precision", -1); indexes.add(stringIndex); Index numberIndex = Index.Range(DataType.Number); numberIndex.set("precision", -1); indexes.add(numberIndex); includedPath.setIndexes(indexes); includedPaths.add(includedPath); indexingPolicy.setIncludedPaths(includedPaths); collectionDefinition.setIndexingPolicy(indexingPolicy); // create a collection client.createCollection(databaseLink, collectionDefinition, options); }
Example #14
Source File: CollectionCrudSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Before public void setUp() throws DocumentClientException { // create client client = new DocumentClient(AccountCredentials.HOST, AccountCredentials.MASTER_KEY, null, null); // removes the database "exampleDB" (if exists) deleteDatabase(); // create database exampleDB; Database databaseDefinition = new Database(); databaseDefinition.setId(databaseId); client.createDatabase(databaseDefinition, null); }
Example #15
Source File: CollectionCrudSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Test public void createSinglePartitionCollection() throws DocumentClientException { String databaseLink = String.format("/dbs/%s", databaseId); String collectionId = "testCollection"; DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(collectionId); // create a collection client.createCollection(databaseLink, collectionDefinition, null); }
Example #16
Source File: DocumentCrudSamples.java From azure-documentdb-java with MIT License | 5 votes |
private void createMultiPartitionCollection() throws DocumentClientException { String databaseLink = String.format("/dbs/%s", databaseId); DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(collectionId); // set /city as the partition key path PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); Collection<String> paths = new ArrayList<String>(); paths.add(partitionKeyPath); partitionKeyDefinition.setPaths(paths); collectionDefinition.setPartitionKey(partitionKeyDefinition); // set the throughput to be 10,200 RequestOptions options = new RequestOptions(); options.setOfferThroughput(10200); // set indexing policy to be range range for string and number IndexingPolicy indexingPolicy = new IndexingPolicy(); Collection<IncludedPath> includedPaths = new ArrayList<IncludedPath>(); IncludedPath includedPath = new IncludedPath(); includedPath.setPath("/*"); Collection<Index> indexes = new ArrayList<Index>(); Index stringIndex = Index.Range(DataType.String); stringIndex.set("precision", -1); indexes.add(stringIndex); Index numberIndex = Index.Range(DataType.Number); numberIndex.set("precision", -1); indexes.add(numberIndex); includedPath.setIndexes(indexes); includedPaths.add(includedPath); indexingPolicy.setIncludedPaths(includedPaths); collectionDefinition.setIndexingPolicy(indexingPolicy); // create a collection client.createCollection(databaseLink, collectionDefinition, options); }
Example #17
Source File: OfferCrudSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Before public void setUp() throws DocumentClientException { // create client client = new DocumentClient(AccountCredentials.HOST, AccountCredentials.MASTER_KEY, null, null); // removes the database "exampleDB" (if exists) deleteDatabase(); // create database createDatabase(); }
Example #18
Source File: StoredProcedureSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Before public void setUp() throws DocumentClientException { // create client client = new DocumentClient(AccountCredentials.HOST, AccountCredentials.MASTER_KEY, null, null); // // removes the database "exampleDB" (if exists) deleteDatabase(); // create database exampleDB; createDatabase(); // create collection createMultiPartitionCollection(); }
Example #19
Source File: StoredProcedureSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Test public void scriptConsoleLogEnabled() throws DocumentClientException { // create a stored procedure String storeProcedureStr = "{" + " 'id':'storedProcedureSample'," + " 'body':" + " 'function() {" + " var mytext = \"x\";" + " var myval = 1;" + " try {" + " console.log(\"The value of %s is %s.\", mytext, myval);" + " getContext().getResponse().setBody(\"Success!\");" + " }" + " catch(err) {" + " getContext().getResponse().setBody(\"inline err: [\" + err.number + \"] \" + err);" + " }" + " }'" + "}"; StoredProcedure storedProcedure = new StoredProcedure(storeProcedureStr); client.createStoredProcedure(collection.getSelfLink(), storedProcedure, null); // execute stored procedure String storedProcLink = String.format("/dbs/%s/colls/%s/sprocs/%s", databaseId, collectionId, "storedProcedureSample"); RequestOptions requestOptions = new RequestOptions(); requestOptions.setScriptLoggingEnabled(true); requestOptions.setPartitionKey(new PartitionKey("Seattle")); StoredProcedureResponse response = client.executeStoredProcedure(storedProcLink, requestOptions, new Object[]{}); String logResult = "The value of x is 1."; assertThat(response.getScriptLog(), is(logResult)); assertThat(response.getResponseHeaders().get(HttpConstants.HttpHeaders.SCRIPT_LOG_RESULTS), is(logResult)); }
Example #20
Source File: StoredProcedureSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Test public void executeStoredProcWithPojoArgs() throws DocumentClientException { // create stored procedure StoredProcedure storedProcedure = new StoredProcedure( "{" + " 'id': 'storedProcedurePojoSample'," + " 'body':" + " 'function (value) {" + " getContext().getResponse().setBody(" + " \"a is \" + value.temp);" + " }'" + "}"); client.createStoredProcedure(collection.getSelfLink(), storedProcedure, null).getResource(); // execute stored procedure String storedProcLink = String.format("/dbs/%s/colls/%s/sprocs/%s", databaseId, collectionId, "storedProcedurePojoSample"); RequestOptions requestOptions = new RequestOptions(); requestOptions.setPartitionKey(new PartitionKey("Seattle")); // POJO class SamplePojo { public String temp = "my temp value"; } SamplePojo samplePojo = new SamplePojo(); Object[] storedProcedureArgs = new Object[] { samplePojo }; String storedProcResultAsString = client.executeStoredProcedure(storedProcLink, requestOptions, storedProcedureArgs).getResponseAsString(); assertThat(storedProcResultAsString, equalTo("\"a is my temp value\"")); }
Example #21
Source File: StoredProcedureSamples.java From azure-documentdb-java with MIT License | 5 votes |
private void createMultiPartitionCollection() throws DocumentClientException { String databaseLink = String.format("/dbs/%s", databaseId); DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(collectionId); // set /city as the partition key path PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); Collection<String> paths = new ArrayList<String>(); paths.add(partitionKeyPath); partitionKeyDefinition.setPaths(paths); collectionDefinition.setPartitionKey(partitionKeyDefinition); // set the throughput to be 10,200 RequestOptions options = new RequestOptions(); options.setOfferThroughput(10200); // set indexing policy to be range range for string and number IndexingPolicy indexingPolicy = new IndexingPolicy(); Collection<IncludedPath> includedPaths = new ArrayList<IncludedPath>(); IncludedPath includedPath = new IncludedPath(); includedPath.setPath("/*"); Collection<Index> indexes = new ArrayList<Index>(); Index stringIndex = Index.Range(DataType.String); stringIndex.set("precision", -1); indexes.add(stringIndex); Index numberIndex = Index.Range(DataType.Number); numberIndex.set("precision", -1); indexes.add(numberIndex); includedPath.setIndexes(indexes); includedPaths.add(includedPath); indexingPolicy.setIncludedPaths(includedPaths); collectionDefinition.setIndexingPolicy(indexingPolicy); // create a collection this.collection = client.createCollection(databaseLink, collectionDefinition, options).getResource(); }
Example #22
Source File: SinglePartitionCollectionDocumentCrudSample.java From azure-documentdb-java with MIT License | 5 votes |
@Before public void setUp() throws DocumentClientException { // create client client = new DocumentClient(AccountCredentials.HOST, AccountCredentials.MASTER_KEY, null, null); // removes the database "exampleDB" (if exists) deleteDatabase(); // create database exampleDB; createDatabase(); // create collection // this example has /id as the partition key createSinglePartitionCollection("/id"); }
Example #23
Source File: DocumentCrudSamples.java From azure-documentdb-java with MIT License | 5 votes |
@Before public void setUp() throws DocumentClientException { // create client client = new DocumentClient(AccountCredentials.HOST, AccountCredentials.MASTER_KEY, null, null); // removes the database "exampleDB" (if exists) deleteDatabase(); // create database exampleDB; createDatabase(); // create collection createMultiPartitionCollection(); }
Example #24
Source File: DocumentBulkImporter.java From azure-documentdb-java with MIT License | 5 votes |
private DocumentClientException toDocumentClientException(Exception e) { if (e instanceof DocumentClientException) { return (DocumentClientException) e; } else { return new DocumentClientException(500, e); } }
Example #25
Source File: Main.java From azure-documentdb-java with MIT License | 5 votes |
public static DocumentClient documentClientFrom(CmdLineConfiguration cfg) throws DocumentClientException { ConnectionPolicy policy = new ConnectionPolicy(); RetryOptions retryOptions = new RetryOptions(); retryOptions.setMaxRetryAttemptsOnThrottledRequests(0); policy.setRetryOptions(retryOptions); policy.setConnectionMode(cfg.getConnectionMode()); policy.setMaxPoolSize(cfg.getMaxConnectionPoolSize()); return new DocumentClient(cfg.getServiceEndpoint(), cfg.getMasterKey(), policy, cfg.getConsistencyLevel()); }
Example #26
Source File: DocDBKeyValueContainerTest.java From remote-monitoring-services-java with MIT License | 5 votes |
@Before public void setUp() throws DocumentClientException, InvalidConfigurationException, CreateResourceException { Config config = new Config(); IServicesConfig servicesConfig = config.getServicesConfig(); DocumentClientFactory factory = new DocumentClientFactory(config); container = new DocDBKeyValueContainer(factory, servicesConfig); inModel = new ValueServiceModel(key, value); container.upsert(collectionId, key, inModel); }
Example #27
Source File: DocDBKeyValueContainerTest.java From remote-monitoring-services-java with MIT License | 5 votes |
@Test(timeout = 5000) @Category({UnitTest.class}) public void putTest() throws DocumentClientException, CreateResourceException { String inputKey = key + UUID.randomUUID().toString(); ValueServiceModel value = container.create(collectionId, inputKey, inModel); assertThat(value.Key, is(inputKey)); container.delete(collectionId, inputKey); }
Example #28
Source File: DocDBKeyValueContainerTest.java From remote-monitoring-services-java with MIT License | 5 votes |
@Test(timeout = 5000) @Category({UnitTest.class}) public void postTest() throws DocumentClientException, CreateResourceException { String inputKey = key + UUID.randomUUID().toString(); ValueServiceModel value = container.upsert(collectionId, inputKey, inModel); assertThat(value.Key, is(inputKey)); container.delete(collectionId, inputKey); }
Example #29
Source File: CreateCosmosDBWithEventualConsistency.java From azure-libraries-for-java with MIT License | 5 votes |
private static void createDBAndAddCollection(String masterKey, String endPoint) throws DocumentClientException { try { DocumentClient documentClient = new DocumentClient(endPoint, masterKey, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session); // Define a new database using the id above. Database myDatabase = new Database(); myDatabase.setId(DATABASE_ID); myDatabase = documentClient.createDatabase(myDatabase, null) .getResource(); System.out.println("Created a new database:"); System.out.println(myDatabase.toString()); // Define a new collection using the id above. DocumentCollection myCollection = new DocumentCollection(); myCollection.setId(COLLECTION_ID); // Set the provisioned throughput for this collection to be 1000 RUs. RequestOptions requestOptions = new RequestOptions(); requestOptions.setOfferThroughput(1000); // Create a new collection. myCollection = documentClient.createCollection( "dbs/" + DATABASE_ID, myCollection, requestOptions) .getResource(); } catch (Exception ex) { throw ex; } }
Example #30
Source File: ManageHACosmosDB.java From azure-libraries-for-java with MIT License | 5 votes |
private static void createDBAndAddCollection(String masterKey, String endPoint) throws DocumentClientException { try { DocumentClient documentClient = new DocumentClient(endPoint, masterKey, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session); // Define a new database using the id above. Database myDatabase = new Database(); myDatabase.setId(DATABASE_ID); myDatabase = documentClient.createDatabase(myDatabase, null) .getResource(); System.out.println("Created a new database:"); System.out.println(myDatabase.toString()); // Define a new collection using the id above. DocumentCollection myCollection = new DocumentCollection(); myCollection.setId(COLLECTION_ID); // Set the provisioned throughput for this collection to be 1000 RUs. RequestOptions requestOptions = new RequestOptions(); requestOptions.setOfferThroughput(4000); // Create a new collection. myCollection = documentClient.createCollection( "dbs/" + DATABASE_ID, myCollection, requestOptions) .getResource(); } catch (Exception ex) { throw ex; } }