Java Code Examples for com.google.datastore.v1.Entity#Builder

The following examples show how to use com.google.datastore.v1.Entity#Builder . 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: Guestbook.java    From google-cloud-datastore with Apache License 2.0 7 votes vote down vote up
/**
 * Add a greeting to the specified guestbook.
 */
private void addGreeting(String guestbookName, String user, String message)
    throws DatastoreException {
  Entity.Builder greeting = Entity.newBuilder();
  greeting.setKey(makeKey(GUESTBOOK_KIND, guestbookName, GREETING_KIND));
  greeting.putProperties(USER_PROPERTY, makeValue(user).build());
  greeting.putProperties(MESSAGE_PROPERTY, makeValue(message).build());
  greeting.putProperties(DATE_PROPERTY, makeValue(new Date()).build());
  Key greetingKey = insert(greeting.build());
  System.out.println("greeting key is: " + greetingKey);
}
 
Example 2
Source File: DatastoreConverters.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws InvalidProtocolBufferException {
  String entityJson = c.element();
  Entity.Builder entityBuilder = Entity.newBuilder();
  entityJsonParser.merge(entityJson, entityBuilder);

  // Build entity who's key has an empty project Id.
  // This allows DatastoreIO to handle what project Entities are loaded into
  Key k = entityBuilder.build().getKey();
  entityBuilder.setKey(Key.newBuilder()
      .addAllPath(k.getPathList())
      .setPartitionId(PartitionId.newBuilder()
          .setProjectId("")
          .setNamespaceId(k.getPartitionId().getNamespaceId())));

  c.output(entityBuilder.build());
}
 
Example 3
Source File: AutoComplete.java    From beam with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  Entity.Builder entityBuilder = Entity.newBuilder();
  com.google.datastore.v1.Key key =
      makeKey(makeKey(kind, ancestorKey).build(), kind, c.element().getKey()).build();

  entityBuilder.setKey(key);
  List<Value> candidates = new ArrayList<>();
  Map<String, Value> properties = new HashMap<>();
  for (CompletionCandidate tag : c.element().getValue()) {
    Entity.Builder tagEntity = Entity.newBuilder();
    properties.put("tag", makeValue(tag.value).build());
    properties.put("count", makeValue(tag.count).build());
    candidates.add(makeValue(tagEntity).build());
  }
  properties.put("candidates", makeValue(candidates).build());
  entityBuilder.putAllProperties(properties);
  c.output(entityBuilder.build());
}
 
Example 4
Source File: DataStoreV1Table.java    From beam with Apache License 2.0 6 votes vote down vote up
@DoFn.ProcessElement
public void processElement(ProcessContext context) {
  Row row = context.element();

  Schema schemaWithoutKeyField =
      Schema.builder()
          .addFields(
              row.getSchema().getFields().stream()
                  .filter(field -> !field.getName().equals(keyField))
                  .collect(Collectors.toList()))
          .build();
  Entity.Builder entityBuilder = constructEntityFromRow(schemaWithoutKeyField, row);
  entityBuilder.setKey(constructKeyFromRow(row));

  context.output(entityBuilder.build());
}
 
Example 5
Source File: V1TestUtil.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Build an entity for the given ancestorKey, kind, namespace and value.
 *
 * @param largePropertySize if greater than 0, add an unindexed property of the given size.
 */
static Entity makeEntity(
    Long value, Key ancestorKey, String kind, @Nullable String namespace, int largePropertySize) {
  Entity.Builder entityBuilder = Entity.newBuilder();
  Key.Builder keyBuilder = makeKey(ancestorKey, kind, UUID.randomUUID().toString());
  // NOTE: Namespace is not inherited between keys created with DatastoreHelper.makeKey, so
  // we must set the namespace on keyBuilder. TODO: Once partitionId inheritance is added,
  // we can simplify this code.
  if (namespace != null) {
    keyBuilder.getPartitionIdBuilder().setNamespaceId(namespace);
  }

  entityBuilder.setKey(keyBuilder.build());
  entityBuilder.putProperties("value", makeValue(value).build());
  if (largePropertySize > 0) {
    entityBuilder.putProperties(
        "unindexed_value",
        makeValue(new String(new char[largePropertySize]).replace("\0", "A"))
            .setExcludeFromIndexes(true)
            .build());
  }
  return entityBuilder.build();
}
 
Example 6
Source File: DataStoreV1Table.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an entire {@code Row} to an appropriate DataStore {@code Entity.Builder}.
 *
 * @param row {@code Row} to convert.
 * @return resulting {@code Entity.Builder}.
 */
private Entity.Builder constructEntityFromRow(Schema schema, Row row) {
  Entity.Builder entityBuilder = Entity.newBuilder();
  for (Schema.Field field : schema.getFields()) {
    Value val = mapObjectToValue(row.getValue(field.getName()));
    entityBuilder.putProperties(field.getName(), val);
  }
  return entityBuilder;
}
 
Example 7
Source File: DatastoreV1Test.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Builds a per-kind statistics response with the given entity size. */
private static RunQueryResponse makeStatKindResponse(long entitySizeInBytes) {
  RunQueryResponse.Builder statKindResponse = RunQueryResponse.newBuilder();
  Entity.Builder entity = Entity.newBuilder();
  entity.setKey(makeKey("dummyKind", "dummyId"));
  entity.putProperties("entity_bytes", makeValue(entitySizeInBytes).build());
  EntityResult.Builder entityResult = EntityResult.newBuilder();
  entityResult.setEntity(entity);
  QueryResultBatch.Builder batch = QueryResultBatch.newBuilder();
  batch.addEntityResults(entityResult);
  statKindResponse.setBatch(batch);
  return statKindResponse.build();
}
 
Example 8
Source File: DatastoreV1Test.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Builds a response of the given timestamp. */
private static RunQueryResponse makeLatestTimestampResponse(long timestamp) {
  RunQueryResponse.Builder timestampResponse = RunQueryResponse.newBuilder();
  Entity.Builder entity = Entity.newBuilder();
  entity.setKey(makeKey("dummyKind", "dummyId"));
  entity.putProperties("timestamp", makeValue(new Date(timestamp * 1000)).build());
  EntityResult.Builder entityResult = EntityResult.newBuilder();
  entityResult.setEntity(entity);
  QueryResultBatch.Builder batch = QueryResultBatch.newBuilder();
  batch.addEntityResults(entityResult);
  timestampResponse.setBatch(batch);
  return timestampResponse.build();
}
 
Example 9
Source File: DatastoreConverters.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
public void merge(String json, Entity.Builder entityBuilder)
    throws InvalidProtocolBufferException {
  jsonParser.merge(json, entityBuilder);
}
 
Example 10
Source File: DatastoreConverters.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
public Entity parse(String json) throws InvalidProtocolBufferException {
  Entity.Builder entityBuilter = Entity.newBuilder();
  merge(json, entityBuilter);
  return entityBuilter.build();
}
 
Example 11
Source File: DatastoreHelper.java    From google-cloud-datastore with Apache License 2.0 4 votes vote down vote up
/**
 * Make a entity value.
 */
public static Value.Builder makeValue(Entity.Builder entity) {
  return makeValue(entity.build());
}