Package com.google.datastore.v1.client
Class DatastoreHelper
java.lang.Object
com.google.datastore.v1.client.DatastoreHelper
Helper methods for
Datastore.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe property used in the Datastore to get the key of the entitystatic final StringName of the environment variable used to set the local host.static final StringName of the environment variable used to set the private key file.static final StringName of the environment variable used to set the project ID.static final StringThe property used in the Datastore to give us a random distributionstatic final StringName of the environment variable used to set the service account. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleangetBoolean(Value value) static com.google.protobuf.ByteStringgetByteString(Value value) static Datastorestatic doublestatic Entitystatic Keystatic Comparator<Key>static longstatic DatastoreOptions.BuilderConstructs aDatastorefrom environment variables and/or the Compute Engine metadata server.static StringGets the project ID from the Compute Engine metadata server.static QuerySplitterGets aQuerySplitter.static com.google.api.client.auth.oauth2.CredentialgetServiceAccountCredential(String serviceAccountId, String privateKeyFile) Constructs credentials for the given account and key.static com.google.api.client.auth.oauth2.CredentialgetServiceAccountCredential(String serviceAccountId, String privateKeyFile, Collection<String> serviceAccountScopes) Constructs credentials for the given account and key file.static com.google.api.client.auth.oauth2.CredentialgetServiceAccountCredential(String serviceAccountId, PrivateKey privateKey, Collection<String> serviceAccountScopes) Constructs credentials for the given account and key.static Stringstatic longgetTimestamp(Value value) static Filter.BuildermakeAncestorFilter(Key ancestor) Makes an ancestor filter.static Filter.BuildermakeAndFilter(Filter... subfilters) Make a composite filter from the given sub-filters using AND to combine filters.static Filter.BuildermakeAndFilter(Iterable<Filter> subfilters) Make a composite filter from the given sub-filters using AND to combine filters.static Mutation.BuildermakeDelete(Key key) static Filter.BuildermakeFilter(String property, PropertyFilter.Operator operator, Value value) Make a filter on a property for use in a query.static Filter.BuildermakeFilter(String property, PropertyFilter.Operator operator, Value.Builder value) Make a filter on a property for use in a query.static Mutation.BuildermakeInsert(Entity entity) static Key.BuilderMake a key from the specified path of kind/id-or-name pairs and/or Keys.static PropertyOrder.BuildermakeOrder(String property, PropertyOrder.Direction direction) Make a sort order for use in a query.static PropertyReference.BuildermakePropertyReference(String propertyName) Make a property reference for use in a query.static Mutation.BuildermakeUpdate(Entity entity) static Mutation.BuildermakeUpsert(Entity entity) static Value.BuildermakeValue(boolean value) Make a boolean value.static Value.BuildermakeValue(double value) Make a floating point value.static Value.BuildermakeValue(long key) Make an integer value.static Value.BuilderMake an entity value.static Value.BuildermakeValue(Entity.Builder entity) Make a entity value.static Value.BuilderMake a key value.static Value.BuildermakeValue(Key.Builder key) Make a key value.static Value.BuildermakeValue(Value.Builder value1, Value.Builder value2, Value.Builder... rest) Make an array value containing the specified values.static Value.BuilderMake a list value containing the specified values.static Value.BuildermakeValue(com.google.protobuf.ByteString blob) Make a ByteString value.static Value.BuildermakeValue(com.google.type.LatLng value) Makes a GeoPoint value.static Value.BuildermakeValue(com.google.type.LatLng.Builder value) Makes a GeoPoint value.static Value.BuilderMake an array value containing the specified values.static Value.BuilderMake a string value.static Value.BuilderMake a timestamp value given a date.static DateConvert a timestamp value into aDateclipping off the microseconds.
-
Field Details
-
SCATTER_PROPERTY_NAME
The property used in the Datastore to give us a random distribution. *- See Also:
-
KEY_PROPERTY_NAME
The property used in the Datastore to get the key of the entity. *- See Also:
-
PROJECT_ID_ENV_VAR
Name of the environment variable used to set the project ID.- See Also:
-
LOCAL_HOST_ENV_VAR
Name of the environment variable used to set the local host.- See Also:
-
SERVICE_ACCOUNT_ENV_VAR
Name of the environment variable used to set the service account.- See Also:
-
PRIVATE_KEY_FILE_ENV_VAR
Name of the environment variable used to set the private key file.- See Also:
-
-
Method Details
-
getServiceAccountCredential
public static com.google.api.client.auth.oauth2.Credential getServiceAccountCredential(String serviceAccountId, String privateKeyFile) throws GeneralSecurityException, IOException Constructs credentials for the given account and key.- Parameters:
serviceAccountId- service account ID (typically an e-mail address).privateKeyFile- the file name from which to get the private key.- Returns:
- valid credentials or
null - Throws:
GeneralSecurityExceptionIOException
-
getServiceAccountCredential
public static com.google.api.client.auth.oauth2.Credential getServiceAccountCredential(String serviceAccountId, String privateKeyFile, Collection<String> serviceAccountScopes) throws GeneralSecurityException, IOException Constructs credentials for the given account and key file.- Parameters:
serviceAccountId- service account ID (typically an e-mail address).privateKeyFile- the file name from which to get the private key.serviceAccountScopes- Collection of OAuth scopes to use with the the service account flow ornullif not.- Returns:
- valid credentials or
null - Throws:
GeneralSecurityExceptionIOException
-
getServiceAccountCredential
public static com.google.api.client.auth.oauth2.Credential getServiceAccountCredential(String serviceAccountId, PrivateKey privateKey, Collection<String> serviceAccountScopes) throws GeneralSecurityException, IOException Constructs credentials for the given account and key.- Parameters:
serviceAccountId- service account ID (typically an e-mail address).privateKey- the private key for the given account.serviceAccountScopes- Collection of OAuth scopes to use with the the service account flow ornullif not.- Returns:
- valid credentials or
null - Throws:
GeneralSecurityExceptionIOException
-
getOptionsFromEnv
public static DatastoreOptions.Builder getOptionsFromEnv() throws GeneralSecurityException, IOExceptionConstructs aDatastorefrom 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:
- No credentials (if the DATASTORE_EMULATOR_HOST environment variable is set)
- Service Account specified by the DATASTORE_SERVICE_ACCOUNT and DATASTORE_PRIVATE_KEY_FILE environment variables
- Google Application Default as described here.
- Throws:
GeneralSecurityExceptionIOException
-
getProjectIdFromComputeEngine
Gets the project ID from the Compute Engine metadata server. Returnsnullif the project ID cannot be determined (because, for instance, the code is not running on Compute Engine). -
getDatastoreFromEnv
- Throws:
GeneralSecurityExceptionIOException- See Also:
-
getQuerySplitter
Gets aQuerySplitter.The returned
QuerySplitter.getSplits(com.google.datastore.v1.Query, com.google.datastore.v1.PartitionId, int, com.google.datastore.v1.client.Datastore)cannot accept a query that contains inequality filters, a sort filter, or a missing kind. -
getKeyComparator
-
makeOrder
Make a sort order for use in a query. -
makeAncestorFilter
Makes an ancestor filter. -
makeFilter
public static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator, Value value) Make a filter on a property for use in a query. -
makeFilter
public static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator, Value.Builder value) Make a filter on a property for use in a query. -
makeAndFilter
Make a composite filter from the given sub-filters using AND to combine filters. -
makeAndFilter
Make a composite filter from the given sub-filters using AND to combine filters. -
makePropertyReference
Make a property reference for use in a query. -
makeValue
Make an array value containing the specified values. -
makeValue
Make a list value containing the specified values. -
makeValue
public static Value.Builder makeValue(Value.Builder value1, Value.Builder value2, Value.Builder... rest) Make an array value containing the specified values. -
makeValue
Make a key value. -
makeValue
Make a key value. -
makeValue
Make an integer value. -
makeValue
Make a floating point value. -
makeValue
Make a boolean value. -
makeValue
Make a string value. -
makeValue
Make an entity value. -
makeValue
Make a entity value. -
makeValue
Make a ByteString value. -
makeValue
Make a timestamp value given a date. -
makeValue
Makes a GeoPoint value. -
makeValue
Makes a GeoPoint value. -
makeKey
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.
-
getDouble
- Returns:
- the double contained in value
- Throws:
IllegalArgumentException- if the value does not contain a double.
-
getKey
- Returns:
- the key contained in value
- Throws:
IllegalArgumentException- if the value does not contain a key.
-
getByteString
- Returns:
- the blob contained in value
- Throws:
IllegalArgumentException- if the value does not contain a blob.
-
getEntity
- Returns:
- the entity contained in value
- Throws:
IllegalArgumentException- if the value does not contain an entity.
-
getString
- Returns:
- the string contained in value
- Throws:
IllegalArgumentException- if the value does not contain a string.
-
getBoolean
- Returns:
- the boolean contained in value
- Throws:
IllegalArgumentException- if the value does not contain a boolean.
-
getLong
- Returns:
- the long contained in value
- Throws:
IllegalArgumentException- if the value does not contain a long.
-
getTimestamp
- Returns:
- the timestamp in microseconds contained in value
- Throws:
IllegalArgumentException- if the value does not contain a timestamp.
-
getList
- Returns:
- the array contained in value as a list.
- Throws:
IllegalArgumentException- if the value does not contain an array.
-
toDate
Convert a timestamp value into aDateclipping off the microseconds.- Parameters:
value- a timestamp value to convert- Returns:
- the resulting
Date - Throws:
IllegalArgumentException- if the value does not contain a timestamp.
-
makeInsert
- Parameters:
entity- the entity to insert- Returns:
- a mutation that will insert an entity
-
makeUpdate
- Parameters:
entity- the entity to update- Returns:
- a mutation that will update an entity
-
makeUpsert
- Parameters:
entity- the entity to upsert- Returns:
- a mutation that will upsert an entity
-
makeDelete
- Parameters:
key- the key of the entity to delete- Returns:
- a mutation that will delete an entity
-