@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { Document record = new Document(); record.set("id", key); for (Entry<String, ByteIterator> val : values.entrySet()) { record.set(val.getKey(), val.getValue().toString()); } try { documentClient.createDocument(collection.getSelfLink(), record, null, false); } catch (DocumentClientException e) { e.printStackTrace(System.err); return Status.ERROR; } return Status.OK; }
@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { Document documentDefinition = getDocumentDefinition(key, values); ResourceResponse<Document> resourceResponse = null; long startTime = System.nanoTime(); try { if (this.useUpsert) { resourceResponse = AzureCosmosClient.client.upsertDocument(getDocumentCollectionLink(this.databaseName, table), documentDefinition, getRequestOptions(key), true); } else { resourceResponse = AzureCosmosClient.client.createDocument(getDocumentCollectionLink(this.databaseName, table), documentDefinition, getRequestOptions(key), true); } } catch (DocumentClientException e) { if (!this.includeExceptionStackInLog) { e = null; } LOGGER.error("Failed to insert key {} to collection {} in database {}", key, table, this.databaseName, e); return Status.ERROR; } finally { long elapsed = (System.nanoTime() - startTime) / NS_IN_US; LOGGER.debug("Inserted key {} in {}us - ActivityID: {}", key, elapsed, resourceResponse != null ? resourceResponse.getActivityId() : NA_STRING); } return Status.OK; }
@Override public ResourceResponse<Document> invoke() throws Exception { return client.createDocument(collectionLink, document, options, disableAutomaticIdGeneration); } });
public <T> T insert(String collectionName, T objectToSave) { final Document document = new Document(); mappingDocumentDbConverter.write(objectToSave, document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDocument in database {} collection {}", this.databaseName, collectionName); } try { if (!this.collectionCache.contains(collectionName)) { createCollectionIfNotExists(this.databaseName, collectionName); this.collectionCache.add(collectionName); } documentDbFactory.getDocumentClient() .createDocument(getCollectionLink(this.databaseName, collectionName), document, null, false); return objectToSave; } catch (DocumentClientException e) { throw new RuntimeException("insert 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); } }
public <T> T insert(String collectionName, T objectToSave, PartitionKey partitionKey) { final Document document = new Document(); mappingDocumentDbConverter.write(objectToSave, document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDocument in database {} collection {}", this.databaseName, collectionName); } try { documentDbFactory.getDocumentClient() .createDocument(getCollectionLink(this.databaseName, collectionName), document, getRequestOptions(partitionKey, null), false); return objectToSave; } catch (DocumentClientException e) { throw new DocumentDBAccessException("insert exception", e); } }