logger.debug("Read entity: " + entity.toString()); Map<String, Value> properties = entity.getProperties(); Set<String> propertiesToReturn = (fields == null ? properties.keySet() : fields);
private Builder(final com.google.datastore.v1.Entity entity) { this.entity = com.google.datastore.v1.Entity.newBuilder(entity); this.properties = entity.getProperties().entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> Value.builder(e.getValue()).build())); }
/** * Return a map of properties to their values for this entity. * * @return a map of property values. */ public Map<String, Value> getProperties() { if (properties == null) { properties = entity .getProperties() .entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> Value.builder(e.getValue()).build())); } return properties; }
com.google.datastore.v1.Entity getPb(final String namespace) { final Map<String, com.google.datastore.v1.Value> propertiesLocal = entity.getProperties() .entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> Value.builder(e.getValue()).build().getPb(namespace))); return com.google.datastore.v1.Entity .newBuilder() .putAllProperties(propertiesLocal) .setKey(getKey().getPb(namespace)) .build(); } }
/** * List the greetings in the specified guestbook. */ private void listGreetings(String guestbookName) throws DatastoreException { Query.Builder query = Query.newBuilder(); query.addKindBuilder().setName(GREETING_KIND); query.setFilter(makeFilter(KEY_PROPERTY, PropertyFilter.Operator.HAS_ANCESTOR, makeValue(makeKey(GUESTBOOK_KIND, guestbookName)))); query.addOrder(makeOrder(DATE_PROPERTY, PropertyOrder.Direction.DESCENDING)); List<Entity> greetings = runQuery(query.build()); if (greetings.size() == 0) { System.out.println("no greetings in " + guestbookName); } for (Entity greeting : greetings) { Map<String, Value> propertyMap = greeting.getProperties(); System.out.println( DatastoreHelper.toDate(propertyMap.get(DATE_PROPERTY)) + ": " + DatastoreHelper.getString(propertyMap.get(USER_PROPERTY)) + " says " + DatastoreHelper.getString(propertyMap.get(MESSAGE_PROPERTY))); } }
String question = entity.getProperties().get("question").getStringValue(); Long answer = entity.getProperties().get("answer").getIntegerValue(); System.out.println(question); String result = System.console().readLine("> ");
return entity.getProperties().get("entity_bytes").getIntegerValue();
/** * Cloud Datastore system tables with statistics are periodically updated. This method fetches * the latest timestamp (in microseconds) of statistics update using the {@code __Stat_Total__} * table. */ private static long queryLatestStatisticsTimestamp( Datastore datastore, @Nullable String namespace) throws DatastoreException { Query.Builder query = Query.newBuilder(); // Note: namespace either being null or empty represents the default namespace, in which // case we treat it as not provided by the user. if (Strings.isNullOrEmpty(namespace)) { query.addKindBuilder().setName("__Stat_Total__"); } else { query.addKindBuilder().setName("__Stat_Ns_Total__"); } query.addOrder(makeOrder("timestamp", DESCENDING)); query.setLimit(Int32Value.newBuilder().setValue(1)); RunQueryRequest request = makeRequest(query.build(), namespace); RunQueryResponse response = datastore.runQuery(request); QueryResultBatch batch = response.getBatch(); if (batch.getEntityResultsCount() == 0) { throw new NoSuchElementException("Datastore total statistics unavailable"); } Entity entity = batch.getEntityResults(0).getEntity(); return entity.getProperties().get("timestamp").getTimestampValue().getSeconds() * 1000000; }