Java Code Examples for com.google.cloud.datastore.Entity
The following examples show how to use
com.google.cloud.datastore.Entity. These examples are extracted from open source projects.
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 Project: nexus-blobstore-google-cloud Source File: DeletedBlobIndex.java License: Eclipse Public License 1.0 | 7 votes |
/** * Removes all deleted blobs tracked in this index. * */ void removeData() { log.warn("removing all entries in the index of soft-deleted blobs..."); Query<Entity> query = Query.newEntityQueryBuilder() .setNamespace(namespace) .setKind(DELETED_BLOBS) .build(); QueryResults<Entity> results = gcsDatastore.run(query); List<Key> keys = StreamSupport.stream( Spliterators.spliteratorUnknownSize(results, Spliterator.ORDERED), false).map(entity -> entity.getKey()).collect(Collectors.toList()); // datastore has a hard limit of 500 keys in a single delete List<List<Key>> partitions = Lists.partition(keys, 500); partitions.stream().forEach(partition -> gcsDatastore.delete(partition.toArray(new Key[partition.size()])) ); log.warn("deleted {} blobIds from the soft-deleted blob index", keys.size()); }
Example 2
Source Project: nexus-blobstore-google-cloud Source File: ShardedCounterMetricsStore.java License: Eclipse Public License 1.0 | 6 votes |
private Entity getShardCounter(final String location) { KeyFactory keyFactory = datastore.newKeyFactory().addAncestors( NXRM_ROOT, PathElement.of(METRICS_STORE, 1L) ); Key key = keyFactory.setNamespace(namespace) .setKind(SHARD).newKey(location); Entity exists = datastore.get(key); if (exists != null) { log.trace("counter for {} already present", location); return exists; } log.debug("creating metrics store counter shard for {}", location); // otherwise make it Entity entity = Entity.newBuilder(key) .set(SIZE, LongValue.newBuilder(0L).build()) .set(COUNT, LongValue.newBuilder(0L).build()) .build(); return datastore.put(entity); }
Example 3
Source Project: google-cloud-java Source File: DatastoreExample.java License: Apache License 2.0 | 6 votes |
@Override public void run(Transaction tx, Key userKey, Void arg) { Entity user = tx.get(userKey); if (user == null) { System.out.println("Nothing to delete, user does not exist."); return; } Query<Key> query = Query.newKeyQueryBuilder() .setNamespace(NAMESPACE) .setKind(COMMENT_KIND) .setFilter(PropertyFilter.hasAncestor(userKey)) .build(); QueryResults<Key> comments = tx.run(query); int count = 0; while (comments.hasNext()) { tx.delete(comments.next()); count++; } tx.delete(userKey); System.out.printf("Deleting user '%s' and %d comment[s].%n", userKey.getName(), count); }
Example 4
Source Project: tomcat-runtime Source File: DatastoreStore.java License: Apache License 2.0 | 6 votes |
/** * Create a new session usable by Tomcat, from a serialized session in a Datastore Entity. * @param sessionKey The key associated with the session metadata and attributes. * @return A new session containing the metadata and attributes stored in the entity. * @throws ClassNotFoundException Thrown if a class serialized in the entity is not available in * this context. * @throws IOException Thrown when an error occur during the deserialization. */ private DatastoreSession deserializeSession(Key sessionKey) throws ClassNotFoundException, IOException { TraceContext loadingSessionContext = startSpan("Fetching the session from Datastore"); Iterator<Entity> entities = datastore.run(Query.newEntityQueryBuilder() .setKind(sessionKind) .setFilter(PropertyFilter.hasAncestor(sessionKey)) .build()); endSpan(loadingSessionContext); DatastoreSession session = null; if (entities.hasNext()) { session = (DatastoreSession) manager.createEmptySession(); TraceContext deserializationContext = startSpan("Deserialization of the session"); session.restoreFromEntities(sessionKey, Lists.newArrayList(entities)); endSpan(deserializationContext); } return session; }
Example 5
Source Project: data-transfer-project Source File: GoogleJobStore.java License: Apache License 2.0 | 6 votes |
@Override public void addErrorsToJob(UUID jobId, Collection<ErrorDetail> errors) throws IOException { if (errors == null || errors.isEmpty()) { return; } List<Entity> entities = new ArrayList<>(); for (ErrorDetail errorDetail : errors) { Key key = getErrorKey(jobId, errorDetail.id()); entities.add( GoogleCloudUtils.createEntityBuilder( key, ImmutableMap.of( JSON_DATA_FIELD, // TODO: encrypt this data objectMapper.writeValueAsString(errorDetail))) .build()); } datastore.add(entities.toArray(new Entity[entities.size()])); }
Example 6
Source Project: data-transfer-project Source File: GoogleJobStore.java License: Apache License 2.0 | 6 votes |
@Override public <T extends DataModel> void update(UUID jobId, String key, T model) { Transaction transaction = datastore.newTransaction(); Key entityKey = getDataKey(jobId, key); try { Entity previousEntity = transaction.get(entityKey); if (previousEntity == null) { throw new IOException("Could not find record for data key: " + entityKey.getName()); } String serialized = objectMapper.writeValueAsString(model); Entity entity = Entity.newBuilder(entityKey) .set(CREATED_FIELD, Timestamp.now()) .set(model.getClass().getName(), serialized) .build(); transaction.put(entity); transaction.commit(); } catch (IOException t) { transaction.rollback(); throw new RuntimeException("Failed atomic update of key: " + key, t); } }
Example 7
Source Project: data-transfer-project Source File: GoogleJobStore.java License: Apache License 2.0 | 6 votes |
@Override public void addCounts(UUID jobId, Map<String, Integer> newCounts) throws IOException { if (newCounts == null) { return; } Transaction transaction = datastore.newTransaction(); for (String dataType : newCounts.keySet()) { Key key = getCountsKey(jobId, dataType); Entity current = datastore.get(key); Integer oldCount = 0; if (current != null && current.getNames().contains(COUNTS_FIELD)) { // Datastore only allows Long properties, but we only ever write Integers through this // interface so the conversion is OK oldCount = Math.toIntExact(current.getLong(COUNTS_FIELD)); } transaction.put( GoogleCloudUtils.createEntityBuilder( key, ImmutableMap.of(COUNTS_FIELD, oldCount + newCounts.get(dataType))) .build()); } transaction.commit(); }
Example 8
Source Project: java-docs-samples Source File: MessageRepositoryImpl.java License: Apache License 2.0 | 6 votes |
@Override public List<String> retrieveClaims(int limit) { // Get claim saved in Datastore Datastore datastore = getDatastoreInstance(); Query<Entity> query = Query.newEntityQueryBuilder().setKind(claimsKind).setLimit(limit).build(); QueryResults<Entity> results = datastore.run(query); List<String> claims = new ArrayList<>(); while (results.hasNext()) { Entity entity = results.next(); String claim = entity.getString("claim"); if (claim != null) { claims.add(claim); } } return claims; }
Example 9
Source Project: data-transfer-project Source File: GoogleCloudUtils.java License: Apache License 2.0 | 6 votes |
/** * Creates an Entity Builder for the given key and properties. Converts the objects to the proper * datastore values */ static Entity.Builder createEntityBuilder(Key key, Map<String, Object> data) throws IOException { Entity.Builder builder = Entity.newBuilder(key); for (Map.Entry<String, Object> entry : data.entrySet()) { if (entry.getValue() instanceof String) { builder.set(entry.getKey(), (String) entry.getValue()); // StringValue } else if (entry.getValue() instanceof Integer) { builder.set(entry.getKey(), (Integer) entry.getValue()); // LongValue } else if (entry.getValue() instanceof Double) { builder.set(entry.getKey(), (Double) entry.getValue()); // DoubleValue } else if (entry.getValue() instanceof Boolean) { builder.set(entry.getKey(), (Boolean) entry.getValue()); // BooleanValue } else if (entry.getValue() instanceof Timestamp) { builder.set(entry.getKey(), (Timestamp) entry.getValue()); // TimestampValue } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (ObjectOutputStream out = new ObjectOutputStream(bos)) { out.writeObject(entry.getValue()); } builder.set(entry.getKey(), Blob.copyFrom(bos.toByteArray())); // BlobValue } } return builder; }
Example 10
Source Project: google-cloud-java Source File: DatastoreSnippets.java License: Apache License 2.0 | 6 votes |
/** Example of starting a new batch. */ // [TARGET newBatch()] // [VARIABLE "my_key_name_1"] // [VARIABLE "my_key_name_2"] public Batch newBatch(String keyName1, String keyName2) { // [START newBatch] Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1); Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2); Batch batch = datastore.newBatch(); Entity entity1 = Entity.newBuilder(key1).set("name", "John").build(); Entity entity2 = Entity.newBuilder(key2).set("title", "title").build(); batch.add(entity1); batch.add(entity2); batch.submit(); // [END newBatch] return batch; }
Example 11
Source Project: java-docs-samples Source File: UserService.java License: Apache License 2.0 | 6 votes |
/** * Updates a user in Cloud Datastore. */ public User updateUser(String id, String name, String email) { failIfInvalid(name, email); Key key = keyFactory.newKey(id); Entity entity = datastore.get(key); if (entity == null) { throw new IllegalArgumentException("No user with id '" + id + "' found"); } else { entity = Entity.newBuilder(entity) .set("id", id) .set("name", name) .set("email", email) .build(); datastore.update(entity); } return new User(id, name, email); }
Example 12
Source Project: google-cloud-java Source File: DatastoreSnippets.java License: Apache License 2.0 | 6 votes |
/** Example of fetching a list of Entity objects. */ // [TARGET fetch(Iterable, ReadOption...)] // [VARIABLE "my_first_key_name"] // [VARIABLE "my_second_key_name"] public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) { // [START fetchEntitiesWithKeys] KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key firstKey = keyFactory.newKey(firstKeyName); Key secondKey = keyFactory.newKey(secondKeyName); List<Entity> entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey)); for (Entity entity : entities) { // do something with the entity } // [END fetchEntitiesWithKeys] return entities; }
Example 13
Source Project: tomcat-runtime Source File: DatastoreSessionTest.java License: Apache License 2.0 | 6 votes |
@Test public void testMetadataDeserialization() throws Exception { Entity metadata = Entity.newBuilder(sessionKey) .set(SessionMetadata.MAX_INACTIVE_INTERVAL, 0) .set(SessionMetadata.CREATION_TIME, 1) .set(SessionMetadata.LAST_ACCESSED_TIME, 2) .set(SessionMetadata.IS_NEW, true) .set(SessionMetadata.IS_VALID, true) .set(SessionMetadata.THIS_ACCESSED_TIME, 3) .build(); DatastoreSession session = new DatastoreSession(sessionManager); session.restoreFromEntities(sessionKey, Collections.singleton(metadata)); assertEquals(session.getMaxInactiveInterval(), 0); assertEquals(session.getCreationTime(), 1); assertEquals(session.getLastAccessedTime(), 2); assertEquals(session.isNew(), true); assertEquals(session.isValid(), true); assertEquals(session.getThisAccessedTime(), 3); }
Example 14
Source Project: spring-cloud-gcp Source File: DefaultDatastoreEntityConverterTests.java License: Apache License 2.0 | 6 votes |
@Test public void writeTestSubtypes() { DiscrimEntityD entityD = new DiscrimEntityD(); entityD.stringField = "item D"; entityD.intField = 10; entityD.enumField = TestDatastoreItem.Color.BLACK; Entity.Builder builder = getEntityBuilder(); ENTITY_CONVERTER.write(entityD, builder); Entity entity = builder.build(); assertThat(entity.getString("stringField")).as("validate string field").isEqualTo("item D"); assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(10L); assertThat(entity.getString("enumField")).as("validate enum field").isEqualTo("BLACK"); assertThat(entity.getList("discrimination_column")).as("validate discrimination field") .containsExactly(StringValue.of("D"), StringValue.of("B"), StringValue.of("X")); }
Example 15
Source Project: google-cloud-java Source File: ITDatastoreSnippets.java License: Apache License 2.0 | 6 votes |
@Test public void testRunQuery() { String kindToFind = "ClassToFind"; String kindToMiss = "OtherClass"; String keyNameToFind = registerKey("my_key_name_to_find", kindToFind); String otherKeyNameToFind = registerKey("other_key_name_to_find", kindToFind); String keyNameToMiss = registerKey("my_key_name_to_miss", kindToMiss); String property = "my_property_name"; String valueToFind = "my_value_to_find"; String valueToMiss = "my_value_to_miss"; addEntity(keyNameToFind, kindToFind, property, valueToFind); addEntity(otherKeyNameToFind, kindToFind, property, valueToMiss); addEntity(keyNameToMiss, kindToMiss, property, valueToFind); List<Entity> queryResults = datastoreSnippets.runQuery(kindToFind); assertNotNull(queryResults); assertEquals(2, queryResults.size()); queryResults = datastoreSnippets.runQueryOnProperty(kindToFind, property, valueToFind); assertNotNull(queryResults); assertEquals(1, queryResults.size()); }
Example 16
Source Project: getting-started-java Source File: DatastoreDao.java License: Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(String startCursorString) { Cursor startCursor = null; if (startCursorString != null && !startCursorString.equals("")) { startCursor = Cursor.fromUrlSafe(startCursorString); // Where we left off } Query<Entity> query = Query.newEntityQueryBuilder() // Build the Query .setKind("Book3") // We only care about Books .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off .setOrderBy(OrderBy.asc(Book.TITLE)) // Use default Index "title" .build(); QueryResults<Entity> resultList = datastore.run(query); // Run the query List<Book> resultBooks = entitiesToBooks(resultList); // Retrieve and convert Entities Cursor cursor = resultList.getCursorAfter(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toUrlSafe(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example 17
Source Project: google-cloud-java Source File: DatastoreSnippets.java License: Apache License 2.0 | 6 votes |
/** Example of updating multiple entities. */ // [TARGET update(Entity...)] // [VARIABLE "my_key_name_1"] // [VARIABLE "my_key_name_2"] public void batchUpdateEntities(String keyName1, String keyName2) { // [START batchUpdateEntities] Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1); Entity.Builder entityBuilder1 = Entity.newBuilder(key1); entityBuilder1.set("propertyName", "updatedValue1"); Entity entity1 = entityBuilder1.build(); Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2); Entity.Builder entityBuilder2 = Entity.newBuilder(key2); entityBuilder2.set("propertyName", "updatedValue2"); Entity entity2 = entityBuilder2.build(); datastore.update(entity1, entity2); // [END batchUpdateEntities] }
Example 18
Source Project: java-docs-samples Source File: ConceptsTest.java License: Apache License 2.0 | 6 votes |
/** * Initializes Datastore and cleans out any residual values. Also initializes global variables * used for testing. */ @Before public void setUp() { datastore = HELPER.getOptions().toBuilder().setNamespace("ghijklmnop").build().getService(); StructuredQuery<Key> query = Query.newKeyQueryBuilder().build(); QueryResults<Key> result = datastore.run(query); datastore.delete(Iterators.toArray(result, Key.class)); keyFactory = datastore.newKeyFactory().setKind("Task"); taskKey = keyFactory.newKey("some-arbitrary-key"); testEntity = Entity.newBuilder(taskKey, TEST_FULL_ENTITY).build(); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); calendar.set(1990, JANUARY, 1); startDate = Timestamp.of(calendar.getTime()); calendar.set(2000, JANUARY, 1); endDate = Timestamp.of(calendar.getTime()); calendar.set(1999, DECEMBER, 31); includedDate = Timestamp.of(calendar.getTime()); }
Example 19
Source Project: styx Source File: DatastoreStorage.java License: Apache License 2.0 | 6 votes |
static RunState entityToRunState(Entity entity, WorkflowInstance instance) throws IOException { final long counter = entity.getLong(PROPERTY_COUNTER); final State state = State.valueOf(entity.getString(PROPERTY_STATE)); final long timestamp = entity.getLong(PROPERTY_STATE_TIMESTAMP); final StateData data = StateData.newBuilder() .tries((int) entity.getLong(PROPERTY_STATE_TRIES)) .consecutiveFailures((int) entity.getLong(PROPERTY_STATE_CONSECUTIVE_FAILURES)) .retryCost(entity.getDouble(PROPERTY_STATE_RETRY_COST)) .trigger(DatastoreStorage.<String>readOpt(entity, PROPERTY_STATE_TRIGGER_TYPE).map(type -> TriggerUtil.trigger(type, entity.getString(PROPERTY_STATE_TRIGGER_ID)))) .messages(OBJECT_MAPPER.<List<Message>>readValue(entity.getString(PROPERTY_STATE_MESSAGES), new TypeReference<List<Message>>() { })) .retryDelayMillis(readOpt(entity, PROPERTY_STATE_RETRY_DELAY_MILLIS)) .lastExit(DatastoreStorage.<Long>readOpt(entity, PROPERTY_STATE_LAST_EXIT).map(Long::intValue)) .executionId(readOpt(entity, PROPERTY_STATE_EXECUTION_ID)) .runnerId(readOpt(entity, PROPERTY_STATE_RUNNER_ID)) .executionDescription(readOptJson(entity, PROPERTY_STATE_EXECUTION_DESCRIPTION, ExecutionDescription.class)) .resourceIds(readOptJson(entity, PROPERTY_STATE_RESOURCE_IDS, new TypeReference<Set<String>>() { })) .triggerParameters(readOptJson(entity, PROPERTY_STATE_TRIGGER_PARAMETERS, TriggerParameters.class)) .build(); return RunState.create(instance, state, data, Instant.ofEpochMilli(timestamp), counter); }
Example 20
Source Project: getting-started-java Source File: DatastoreSessionFilter.java License: Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // initialize local copy of datastore session variables datastore = DatastoreOptions.getDefaultInstance().getService(); keyFactory = datastore.newKeyFactory().setKind("SessionVariable"); // Delete all sessions unmodified for over two days DateTime dt = DateTime.now(DateTimeZone.UTC); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf))) .build(); QueryResults<Entity> resultList = datastore.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); datastore.delete(stateEntity.getKey()); } }
Example 21
Source Project: getting-started-java Source File: DatastoreSessionFilter.java License: Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 22
Source Project: getting-started-java Source File: DatastoreSessionFilter.java License: Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // initialize local copy of datastore session variables datastore = DatastoreOptions.getDefaultInstance().getService(); keyFactory = datastore.newKeyFactory().setKind("SessionVariable"); // Delete all sessions unmodified for over two days DateTime dt = DateTime.now(DateTimeZone.UTC); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf))) .build(); QueryResults<Entity> resultList = datastore.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); datastore.delete(stateEntity.getKey()); } }
Example 23
Source Project: catatumbo Source File: DefaultDatastoreWriter.java License: Apache License 2.0 | 6 votes |
/** * Updates or inserts the given list of entities in the Cloud Datastore. If the entities do not * have a valid ID, IDs may be generated. * * @param entities * the entities to update/or insert. * @return the updated or inserted entities * @throws EntityManagerException * if any error occurs while saving. */ @SuppressWarnings("unchecked") public <E> List<E> upsert(List<E> entities) { if (entities == null || entities.isEmpty()) { return new ArrayList<>(); } try { entityManager.executeEntityListeners(CallbackType.PRE_UPSERT, entities); FullEntity<?>[] nativeEntities = toNativeFullEntities(entities, entityManager, Intent.UPSERT); Class<?> entityClass = entities.get(0).getClass(); List<Entity> upsertedNativeEntities = nativeWriter.put(nativeEntities); List<E> upsertedEntities = (List<E>) toEntities(entityClass, upsertedNativeEntities); entityManager.executeEntityListeners(CallbackType.POST_UPSERT, upsertedEntities); return upsertedEntities; } catch (DatastoreException exp) { throw DatastoreUtils.wrap(exp); } }
Example 24
Source Project: google-cloud-java Source File: ITTransactionSnippets.java License: Apache License 2.0 | 5 votes |
@Test public void testActive() { Transaction transaction = datastore.newTransaction(); TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); Key key = transactionSnippets.active(); Entity result = datastore.get(key); assertNotNull(result); datastore.delete(key); }
Example 25
Source Project: nexus-blobstore-google-cloud Source File: ShardedCounterMetricsStore.java License: Eclipse Public License 1.0 | 5 votes |
private QueryResults<Entity> getShards() { Query<Entity> shardQuery = Query.newEntityQueryBuilder() .setFilter(PropertyFilter.hasAncestor(shardRoot)) .setNamespace(namespace) .setKind(SHARD) .build(); return datastore.run(shardQuery); }
Example 26
Source Project: styx Source File: DatastoreStorageTransaction.java License: Apache License 2.0 | 5 votes |
@Override public WorkflowInstance writeActiveState(WorkflowInstance instance, RunState state) throws IOException { // Note: the parent entity need not actually exist final Key indexEntryKey = activeWorkflowInstanceIndexShardEntryKey(tx.getDatastore().newKeyFactory(), instance); final Entity indexEntry = Entity.newBuilder(indexEntryKey).build(); tx.add(indexEntry); tx.add(runStateToEntity(tx.getDatastore().newKeyFactory(), instance, state)); return instance; }
Example 27
Source Project: google-cloud-java Source File: QuerySnippets.java License: Apache License 2.0 | 5 votes |
/** Example of creating and running an entity query. */ // [TARGET newEntityQueryBuilder()] // [VARIABLE "my_kind"] public QueryResults<Entity> newEntityQuery(String kind) { // [START newEntityQuery] Query<Entity> query = Query.newEntityQueryBuilder().setKind(kind).build(); QueryResults<Entity> results = datastore.run(query); // Use results // [END newEntityQuery] return results; }
Example 28
Source Project: styx Source File: ShardedCounterTest.java License: Apache License 2.0 | 5 votes |
@Test public void shouldCreateCounterEmpty() throws IOException { assertThat(shardedCounter.getCounter(COUNTER_ID1), is(0L)); QueryResults<Entity> results = getShardsForCounter(COUNTER_ID1); // assert all shards exist assertThat(shardedCounter.getCounterSnapshot(COUNTER_ID1).getShards().size(), is(128)); IntStream.range(0, ShardedCounter.NUM_SHARDS).forEach(i -> { assertTrue(results.hasNext()); results.next(); }); }
Example 29
Source Project: data-transfer-project Source File: GoogleJobStore.java License: Apache License 2.0 | 5 votes |
private static Map<String, Object> getProperties(Entity entity) throws IOException, ClassNotFoundException { if (entity == null) { return null; } ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); for (String property : entity.getNames()) { // builder.put(property, entity.getValue(property)); if (entity.getValue(property) instanceof StringValue) { builder.put(property, (String) entity.getString(property)); } else if (entity.getValue(property) instanceof LongValue) { // This conversion is safe because of integer to long conversion above builder.put(property, new Long(entity.getLong(property)).intValue()); } else if (entity.getValue(property) instanceof DoubleValue) { builder.put(property, (Double) entity.getDouble(property)); } else if (entity.getValue(property) instanceof BooleanValue) { builder.put(property, (Boolean) entity.getBoolean(property)); } else if (entity.getValue(property) instanceof TimestampValue) { builder.put(property, (Timestamp) entity.getTimestamp(property)); } else { Blob blob = entity.getBlob(property); Object obj = null; try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) { obj = in.readObject(); } builder.put(property, obj); // BlobValue } } return builder.build(); }
Example 30
Source Project: java-docs-samples Source File: QuickstartSample.java License: Apache License 2.0 | 5 votes |
public static void main(String... args) throws Exception { // Instantiates a client Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); // The kind for the new entity String kind = "Task"; // The name/ID for the new entity String name = "sampletask1"; // The Cloud Datastore key for the new entity Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); // Prepares the new entity Entity task = Entity.newBuilder(taskKey) .set("description", "Buy milk") .build(); // Saves the entity datastore.put(task); System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); //Retrieve entity Entity retrieved = datastore.get(taskKey); System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); }