Class DatastoreHelper (2.20.0)

public final class DatastoreHelper

Helper methods for Datastore.

Inheritance

java.lang.Object > DatastoreHelper

Static Fields

KEY_PROPERTY_NAME

public static final String KEY_PROPERTY_NAME

The property used in the Datastore to get the key of the entity. *

Field Value
Type Description
String

LOCAL_HOST_ENV_VAR

public static final String LOCAL_HOST_ENV_VAR

Name of the environment variable used to set the local host.

Field Value
Type Description
String

PRIVATE_KEY_FILE_ENV_VAR

public static final String PRIVATE_KEY_FILE_ENV_VAR

Name of the environment variable used to set the private key file.

Field Value
Type Description
String

PROJECT_ID_ENV_VAR

public static final String PROJECT_ID_ENV_VAR

Name of the environment variable used to set the project ID.

Field Value
Type Description
String

SCATTER_PROPERTY_NAME

public static final String SCATTER_PROPERTY_NAME

The property used in the Datastore to give us a random distribution. *

Field Value
Type Description
String

SERVICE_ACCOUNT_ENV_VAR

public static final String SERVICE_ACCOUNT_ENV_VAR

Name of the environment variable used to set the service account.

Field Value
Type Description
String

Static Methods

getBoolean(Value value)

public static boolean getBoolean(Value value)
Parameter
Name Description
value Value
Returns
Type Description
boolean

the boolean contained in value

getByteString(Value value)

public static ByteString getByteString(Value value)
Parameter
Name Description
value Value
Returns
Type Description
ByteString

the blob contained in value

getDatastoreFromEnv()

public static Datastore getDatastoreFromEnv()
Returns
Type Description
Datastore
Exceptions
Type Description
GeneralSecurityException
IOException

getDouble(Value value)

public static double getDouble(Value value)
Parameter
Name Description
value Value
Returns
Type Description
double

the double contained in value

getEntity(Value value)

public static Entity getEntity(Value value)
Parameter
Name Description
value Value
Returns
Type Description
Entity

the entity contained in value

getKey(Value value)

public static Key getKey(Value value)
Parameter
Name Description
value Value
Returns
Type Description
Key

the key contained in value

getKeyComparator()

public static Comparator<Key> getKeyComparator()
Returns
Type Description
Comparator<Key>

getList(Value value)

public static List<Value> getList(Value value)
Parameter
Name Description
value Value
Returns
Type Description
List<Value>

the array contained in value as a list.

getLong(Value value)

public static long getLong(Value value)
Parameter
Name Description
value Value
Returns
Type Description
long

the long contained in value

getOptionsFromEnv()

public static DatastoreOptions.Builder getOptionsFromEnv()

Constructs a Datastore from environment variables and/or the Compute Engine metadata server.

The project ID is determined from, in order of preference:

  • DATASTORE_PROJECT_ID environment variable
  • Compute Engine

Credentials are taken from, in order of preference:

  1. No credentials (if the DATASTORE_EMULATOR_HOST environment variable is set)
  2. Service Account specified by the DATASTORE_SERVICE_ACCOUNT and DATASTORE_PRIVATE_KEY_FILE environment variables
  3. Google Application Default as described here.
Returns
Type Description
DatastoreOptions.Builder
Exceptions
Type Description
GeneralSecurityException
IOException

getProjectIdFromComputeEngine()

public static String getProjectIdFromComputeEngine()

Gets the project ID from the Compute Engine metadata server. Returns null if the project ID cannot be determined (because, for instance, the code is not running on Compute Engine).

Returns
Type Description
String

getQuerySplitter()

public static QuerySplitter getQuerySplitter()

Gets a QuerySplitter.

The returned QuerySplitter#getSplits cannot accept a query that contains inequality filters, a sort filter, or a missing kind.

Returns
Type Description
QuerySplitter

getServiceAccountCredential(String serviceAccountId, String privateKeyFile)

public static Credential getServiceAccountCredential(String serviceAccountId, String privateKeyFile)

Constructs credentials for the given account and key.

Parameters
Name Description
serviceAccountId String

service account ID (typically an e-mail address).

privateKeyFile String

the file name from which to get the private key.

Returns
Type Description
com.google.api.client.auth.oauth2.Credential

valid credentials or null

Exceptions
Type Description
GeneralSecurityException
IOException

getServiceAccountCredential(String serviceAccountId, String privateKeyFile, Collection<String> serviceAccountScopes)

public static Credential getServiceAccountCredential(String serviceAccountId, String privateKeyFile, Collection<String> serviceAccountScopes)

Constructs credentials for the given account and key file.

Parameters
Name Description
serviceAccountId String

service account ID (typically an e-mail address).

privateKeyFile String

the file name from which to get the private key.

serviceAccountScopes Collection<String>

Collection of OAuth scopes to use with the the service account flow or null if not.

Returns
Type Description
com.google.api.client.auth.oauth2.Credential

valid credentials or null

Exceptions
Type Description
GeneralSecurityException
IOException

getServiceAccountCredential(String serviceAccountId, PrivateKey privateKey, Collection<String> serviceAccountScopes)

public static Credential getServiceAccountCredential(String serviceAccountId, PrivateKey privateKey, Collection<String> serviceAccountScopes)

Constructs credentials for the given account and key.

Parameters
Name Description
serviceAccountId String

service account ID (typically an e-mail address).

privateKey PrivateKey

the private key for the given account.

serviceAccountScopes Collection<String>

Collection of OAuth scopes to use with the the service account flow or null if not.

Returns
Type Description
com.google.api.client.auth.oauth2.Credential

valid credentials or null

Exceptions
Type Description
GeneralSecurityException
IOException

getString(Value value)

public static String getString(Value value)
Parameter
Name Description
value Value
Returns
Type Description
String

the string contained in value

getTimestamp(Value value)

public static long getTimestamp(Value value)
Parameter
Name Description
value Value
Returns
Type Description
long

the timestamp in microseconds contained in value

makeAncestorFilter(Key ancestor)

public static Filter.Builder makeAncestorFilter(Key ancestor)

Makes an ancestor filter.

Parameter
Name Description
ancestor Key
Returns
Type Description
Filter.Builder

makeAndFilter(Filter[] subfilters)

public static Filter.Builder makeAndFilter(Filter[] subfilters)

Make a composite filter from the given sub-filters using AND to combine filters.

Parameter
Name Description
subfilters Filter[]
Returns
Type Description
Filter.Builder

makeAndFilter(Iterable<Filter> subfilters)

public static Filter.Builder makeAndFilter(Iterable<Filter> subfilters)

Make a composite filter from the given sub-filters using AND to combine filters.

Parameter
Name Description
subfilters Iterable<Filter>
Returns
Type Description
Filter.Builder

makeDelete(Key key)

public static Mutation.Builder makeDelete(Key key)
Parameter
Name Description
key Key

the key of the entity to delete

Returns
Type Description
Mutation.Builder

a mutation that will delete an entity

makeFilter(String property, PropertyFilter.Operator operator, Value value)

public static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator, Value value)

Make a filter on a property for use in a query.

Parameters
Name Description
property String
operator PropertyFilter.Operator
value Value
Returns
Type Description
Filter.Builder

makeFilter(String property, PropertyFilter.Operator operator, Value.Builder value)

public static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator, Value.Builder value)

Make a filter on a property for use in a query.

Parameters
Name Description
property String
operator PropertyFilter.Operator
value Value.Builder
Returns
Type Description
Filter.Builder

makeInsert(Entity entity)

public static Mutation.Builder makeInsert(Entity entity)
Parameter
Name Description
entity Entity

the entity to insert

Returns
Type Description
Mutation.Builder

a mutation that will insert an entity

makeKey(Object[] elements)

public static Key.Builder makeKey(Object[] elements)

Make a key from the specified path of kind/id-or-name pairs and/or Keys.

The id-or-name values must be either String, Long, Integer or Short.

The last id-or-name value may be omitted, in which case an entity without an id is created (for use with automatic id allocation).

The PartitionIds of all Keys in the path must be equal. The returned Key.Builder will use this PartitionId.

Parameter
Name Description
elements Object[]
Returns
Type Description
Key.Builder

makeOrder(String property, PropertyOrder.Direction direction)

public static PropertyOrder.Builder makeOrder(String property, PropertyOrder.Direction direction)

Make a sort order for use in a query.

Parameters
Name Description
property String
direction PropertyOrder.Direction
Returns
Type Description
PropertyOrder.Builder

makePropertyReference(String propertyName)

public static PropertyReference.Builder makePropertyReference(String propertyName)

Make a property reference for use in a query.

Parameter
Name Description
propertyName String
Returns
Type Description
PropertyReference.Builder

makeUpdate(Entity entity)

public static Mutation.Builder makeUpdate(Entity entity)
Parameter
Name Description
entity Entity

the entity to update

Returns
Type Description
Mutation.Builder

a mutation that will update an entity

makeUpsert(Entity entity)

public static Mutation.Builder makeUpsert(Entity entity)
Parameter
Name Description
entity Entity

the entity to upsert

Returns
Type Description
Mutation.Builder

a mutation that will upsert an entity

makeValue(boolean value)

public static Value.Builder makeValue(boolean value)

Make a boolean value.

Parameter
Name Description
value boolean
Returns
Type Description
Value.Builder

makeValue(Entity entity)

public static Value.Builder makeValue(Entity entity)

Make an entity value.

Parameter
Name Description
entity Entity
Returns
Type Description
Value.Builder

makeValue(Entity.Builder entity)

public static Value.Builder makeValue(Entity.Builder entity)

Make a entity value.

Parameter
Name Description
entity Entity.Builder
Returns
Type Description
Value.Builder

makeValue(Key key)

public static Value.Builder makeValue(Key key)

Make a key value.

Parameter
Name Description
key Key
Returns
Type Description
Value.Builder

makeValue(Key.Builder key)

public static Value.Builder makeValue(Key.Builder key)

Make a key value.

Parameter
Name Description
key Key.Builder
Returns
Type Description
Value.Builder

makeValue(Value value1, Value value2, Value[] rest)

public static Value.Builder makeValue(Value value1, Value value2, Value[] rest)

Make a list value containing the specified values.

Parameters
Name Description
value1 Value
value2 Value
rest Value[]
Returns
Type Description
Value.Builder

makeValue(Value.Builder value1, Value.Builder value2, Value.Builder[] rest)

public static Value.Builder makeValue(Value.Builder value1, Value.Builder value2, Value.Builder[] rest)

Make an array value containing the specified values.

Parameters
Name Description
value1 Value.Builder
value2 Value.Builder
rest Builder[]
Returns
Type Description
Value.Builder

makeValue(ByteString blob)

public static Value.Builder makeValue(ByteString blob)

Make a ByteString value.

Parameter
Name Description
blob ByteString
Returns
Type Description
Value.Builder

makeValue(LatLng value)

public static Value.Builder makeValue(LatLng value)

Makes a GeoPoint value.

Parameter
Name Description
value com.google.type.LatLng
Returns
Type Description
Value.Builder

makeValue(LatLng.Builder value)

public static Value.Builder makeValue(LatLng.Builder value)

Makes a GeoPoint value.

Parameter
Name Description
value com.google.type.LatLng.Builder
Returns
Type Description
Value.Builder

makeValue(double value)

public static Value.Builder makeValue(double value)

Make a floating point value.

Parameter
Name Description
value double
Returns
Type Description
Value.Builder

makeValue(Iterable<Value> values)

public static Value.Builder makeValue(Iterable<Value> values)

Make an array value containing the specified values.

Parameter
Name Description
values Iterable<Value>
Returns
Type Description
Value.Builder

makeValue(String value)

public static Value.Builder makeValue(String value)

Make a string value.

Parameter
Name Description
value String
Returns
Type Description
Value.Builder

makeValue(Date date)

public static Value.Builder makeValue(Date date)

Make a timestamp value given a date.

Parameter
Name Description
date Date
Returns
Type Description
Value.Builder

makeValue(long key)

public static Value.Builder makeValue(long key)

Make an integer value.

Parameter
Name Description
key long
Returns
Type Description
Value.Builder

toDate(Value value)

public static Date toDate(Value value)

Convert a timestamp value into a Date clipping off the microseconds.

Parameter
Name Description
value Value

a timestamp value to convert

Returns
Type Description
Date

the resulting Date