@Override public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { String documentLink = getDocumentLink(this.databaseName, table, key); ResourceResponse<Document> readResource = null; Document document; long startTime = System.nanoTime(); try { readResource = AzureCosmosClient.client.readDocument(documentLink, getRequestOptions(key)); document = readResource.getResource(); } catch (DocumentClientException e) { if (!this.includeExceptionStackInLog) { e = null; } LOGGER.error("Failed to read key {} in collection {} in database {}", key, table, this.databaseName, e); return Status.ERROR; } finally { long elapsed = (System.nanoTime() - startTime) / NS_IN_US; LOGGER.debug("Read key {} in {}us - ActivityID: {}", key, elapsed, readResource != null ? readResource.getActivityId() : NA_STRING); } if (null != document) { result.putAll(extractResult(document)); LOGGER.trace("Read result: {}", document); } return Status.OK; }
private Database getDatabase() { if (database == null) { // Get the database if it exists List<Database> databaseList = documentClient .queryDatabases( "SELECT * FROM root r WHERE r.id='" + databaseId + "'", null) .getQueryIterable() .toList(); if (databaseList.size() > 0) { // Cache the database object so we won't have to query for it // later to retrieve the selfLink. database = databaseList.get(0); } else { // Create the database if it doesn't exist. try { Database databaseDefinition = new Database(); databaseDefinition.setId(databaseId); database = documentClient.createDatabase(databaseDefinition, null) .getResource(); } catch (DocumentClientException e) { // TODO: Something has gone terribly wrong - the app wasn't // able to query or create the collection. // Verify your connection, endpoint, and key. e.printStackTrace(System.err); } } } return database; }
.createCollection(getDatabase().getSelfLink(), collectionDefinition, null) .getResource(); } catch (DocumentClientException e) {
final Resource resource = documentDbFactory.getDocumentClient() .readDocument(getDocumentLink(this.databaseName, collectionName, id), null) .getResource();
public DocumentCollection createCollection(String dbName, String collectionName, RequestOptions collectionOptions) { DocumentCollection collection = new DocumentCollection(); collection.setId(collectionName); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createCollection in database {} collection {}", dbName, collectionName); } try { final Resource resource = documentDbFactory.getDocumentClient() .createCollection(getDatabaseLink(dbName), collection, collectionOptions) .getResource(); if (resource instanceof DocumentCollection) { collection = (DocumentCollection) resource; } return collection; } catch (DocumentClientException e) { throw new RuntimeException("createCollection exception", e); } }
public <T> T findById(String collectionName, Object id, Class<T> entityClass) { try { final Resource resource = documentDbFactory.getDocumentClient() .readDocument( getDocumentLink(this.databaseName, collectionName, id.toString()), null) .getResource(); if (resource instanceof Document) { final Document document = (Document) resource; return mappingDocumentDbConverter.read(entityClass, document); } else { return null; } } catch (DocumentClientException e) { if (e.getStatusCode() == HttpConstants.StatusCodes.NOTFOUND) { return null; } throw new RuntimeException("findById exception", e); } }
public <T> T insert(String collectionName, T objectToSave, PartitionKey partitionKey) { Assert.hasText(collectionName, "collectionName should not be null, empty or only whitespaces"); Assert.notNull(objectToSave, "objectToSave should not be null"); final Document document = mappingDocumentDbConverter.writeDoc(objectToSave); log.debug("execute createDocument in database {} collection {}", this.databaseName, collectionName); try { final Resource result = getDocumentClient() .createDocument(getCollectionLink(this.databaseName, collectionName), document, getRequestOptions(partitionKey, null), false).getResource(); if (result instanceof Document) { final Document documentInserted = (Document) result; @SuppressWarnings("unchecked") final Class<T> domainClass = (Class<T>) objectToSave.getClass(); return mappingDocumentDbConverter.read(domainClass, documentInserted); } else { return null; } } catch (DocumentClientException e) { throw new DocumentDBAccessException("insert exception", e); } }
final Resource resource = documentDbFactory.getDocumentClient() .createCollection(getDatabaseLink(dbName), collection, collectionOptions) .getResource(); if (resource instanceof DocumentCollection) { collection = (DocumentCollection) resource;
private Database createDatabaseIfNotExists(String dbName) { try { final List<Database> dbList = documentDbFactory.getDocumentClient() .queryDatabases(new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", dbName))), null) .getQueryIterable().toList(); if (!dbList.isEmpty()) { return dbList.get(0); } else { // create new database final Database db = new Database(); db.setId(dbName); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDatabase {}", dbName); } final Resource resource = documentDbFactory.getDocumentClient() .createDatabase(db, null).getResource(); if (resource instanceof Database) { return (Database) resource; } else { LOGGER.error("create database {} get unexpected result: {}" + resource.getSelfLink()); throw new RuntimeException("create database {} get unexpected result: " + resource.getSelfLink()); } } } catch (DocumentClientException ex) { throw new RuntimeException("createOrGetDatabase exception", ex); } }
private Database createDatabaseIfNotExists(String dbName) { try { final List<Database> dbList = documentDbFactory.getDocumentClient() .queryDatabases(new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", dbName))), null) .getQueryIterable().toList(); if (!dbList.isEmpty()) { return dbList.get(0); } else { // create new database final Database db = new Database(); db.setId(dbName); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDatabase {}", dbName); } final Resource resource = documentDbFactory.getDocumentClient() .createDatabase(db, null).getResource(); if (resource instanceof Database) { return (Database) resource; } else { final String errorMessage = MessageFormat.format( "create database {0} and get unexpected result: {1}", dbName, resource.getSelfLink()); LOGGER.error(errorMessage); throw new DatabaseCreationException(errorMessage); } } } catch (DocumentClientException ex) { throw new DocumentDBAccessException("createOrGetDatabase exception", ex); } }
private Database createDatabaseIfNotExists(String dbName) { try { final List<Database> dbList = getDocumentClient() .queryDatabases(new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", dbName))), null) .getQueryIterable().toList(); if (!dbList.isEmpty()) { return dbList.get(0); } else { // create new database final Database db = new Database(); db.setId(dbName); log.debug("execute createDatabase {}", dbName); final Resource resource = getDocumentClient().createDatabase(db, null).getResource(); if (resource instanceof Database) { return (Database) resource; } else { final String errorMessage = MessageFormat.format( "create database {0} and get unexpected result: {1}", dbName, resource.getSelfLink()); log.error(errorMessage); throw new DatabaseCreationException(errorMessage); } } } catch (DocumentClientException ex) { throw new DocumentDBAccessException("createOrGetDatabase exception", ex); } }
public <T> T findById(String collectionName, Object id, Class<T> domainClass) { Assert.hasText(collectionName, "collectionName should not be null, empty or only whitespaces"); Assert.notNull(domainClass, "entityClass should not be null"); assertValidId(id); try { final RequestOptions options = new RequestOptions(); if (isIdFieldAsPartitionKey(domainClass)) { options.setPartitionKey(new PartitionKey(id)); } final String documentLink = getDocumentLink(this.databaseName, collectionName, id); final Resource document = getDocumentClient().readDocument(documentLink, options).getResource(); if (document instanceof Document) { return mappingDocumentDbConverter.read(domainClass, (Document) document); } else { return null; } } catch (DocumentClientException e) { if (e.getStatusCode() == HttpConstants.StatusCodes.NOTFOUND) { return null; } throw new DocumentDBAccessException("findById exception", e); } }
public <T> T findById(String collectionName, Object id, Class<T> entityClass) { try { final Resource resource = documentDbFactory.getDocumentClient() .readDocument(getDocumentLink(this.databaseName, collectionName, id), new RequestOptions()).getResource(); if (resource instanceof Document) { final Document document = (Document) resource; return mappingDocumentDbConverter.read(entityClass, document); } else { return null; } } catch (DocumentClientException e) { if (e.getStatusCode() == HttpConstants.StatusCodes.NOTFOUND) { return null; } throw new DocumentDBAccessException("findById exception", e); } }
final Resource resource = getDocumentClient() .createCollection(getDatabaseLink(dbName), collection, requestOptions) .getResource(); if (resource instanceof DocumentCollection) { collection = (DocumentCollection) resource;