How would I retrieve an Embedded Entity with repea

2019-08-11 16:06发布

问题:

I created entities on datastore using the AppEngine SDK's python APIs and I'd like to retrieve them on Google Cloud Dataflow (Java). The entity's structure is something like this:

  • entity
    • embedded_entity (ndb.StructuredProperty(repeated=True))
      • name
      • name
      • name
      • name

Retrieving would be something like this, but I know I am missing the step where I extract the data.

static class EmbeddedStringExtractor extends DoFn<Entity, String> {
  @Override
  public void processElement(ProcessContext c) {
    Map<String, Value> main_entity_map = DatastoreHelper.getPropertyMap(c.element());
    Entity embedded_entity = entity.get("embedded_entity").getEntityValue();
    // missing step
    Map<String, Value> embedded_entity_map = DatastoreHelper.getPropertyMap(embedded_entity);
    String some_string = DatastoreHelper.getString(embedded_entity_map.get("name"));
  }
}

How would I loop through all the repeated properties?

回答1:

Ok, got it. This is how I enabled it:

static class EmbeddedStringExtractor extends DoFn<Entity, String> {
  @Override
  public void processElement(ProcessContext c) {
    Map<String, Value> main_entity_map = DatastoreHelper.getPropertyMap(c.element());
    List<Value> embedded_entity_values = entity.get("embedded_entity").getListValueList();

      for (Value embedded_entity_value: embedded_entity_values){
          Entity embedded_entity = embedded_entity_value.getEntityValue();
          Map<String, Value> embedded_map = DatastoreHelper.getPropertyMap(embedded_entity);
          String embedded_name = DatastoreHelper.getString(embedded_map.get("name"));
      }

    // And then just c.output whatever you need.

   }
}

Just needed to get used to the Java APIs.