Package com.google.datastore.v1.client
Class DatastoreHelper
- java.lang.Object
-
- com.google.datastore.v1.client.DatastoreHelper
-
-
Field Summary
Fields Modifier and Type Field Description static String
KEY_PROPERTY_NAME
The property used in the Datastore to get the key of the entity.static String
LOCAL_HOST_ENV_VAR
Name of the environment variable used to set the local host.static String
PRIVATE_KEY_FILE_ENV_VAR
Name of the environment variable used to set the private key file.static String
PROJECT_ID_ENV_VAR
Name of the environment variable used to set the project ID.static String
SCATTER_PROPERTY_NAME
The property used in the Datastore to give us a random distribution.static String
SERVICE_ACCOUNT_ENV_VAR
Name of the environment variable used to set the service account.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
getBoolean(Value value)
static com.google.protobuf.ByteString
getByteString(Value value)
static Datastore
getDatastoreFromEnv()
static double
getDouble(Value value)
static Entity
getEntity(Value value)
static Key
getKey(Value value)
static Comparator<Key>
getKeyComparator()
static List<Value>
getList(Value value)
static long
getLong(Value value)
static DatastoreOptions.Builder
getOptionsFromEnv()
Constructs aDatastore
from environment variables and/or the Compute Engine metadata server.static String
getProjectIdFromComputeEngine()
Gets the project ID from the Compute Engine metadata server.static QuerySplitter
getQuerySplitter()
Gets aQuerySplitter
.static com.google.api.client.auth.oauth2.Credential
getServiceAccountCredential(String serviceAccountId, String privateKeyFile)
Constructs credentials for the given account and key.static com.google.api.client.auth.oauth2.Credential
getServiceAccountCredential(String serviceAccountId, String privateKeyFile, Collection<String> serviceAccountScopes)
Constructs credentials for the given account and key file.static com.google.api.client.auth.oauth2.Credential
getServiceAccountCredential(String serviceAccountId, PrivateKey privateKey, Collection<String> serviceAccountScopes)
Constructs credentials for the given account and key.static String
getString(Value value)
static long
getTimestamp(Value value)
static Filter.Builder
makeAncestorFilter(Key ancestor)
Makes an ancestor filter.static Filter.Builder
makeAndFilter(Filter... subfilters)
Make a composite filter from the given sub-filters using AND to combine filters.static Filter.Builder
makeAndFilter(Iterable<Filter> subfilters)
Make a composite filter from the given sub-filters using AND to combine filters.static Mutation.Builder
makeDelete(Key key)
static Filter.Builder
makeFilter(String property, PropertyFilter.Operator operator, Value value)
Make a filter on a property for use in a query.static Filter.Builder
makeFilter(String property, PropertyFilter.Operator operator, Value.Builder value)
Make a filter on a property for use in a query.static Mutation.Builder
makeInsert(Entity entity)
static Key.Builder
makeKey(Object... elements)
Make a key from the specified path of kind/id-or-name pairs and/or Keys.static PropertyOrder.Builder
makeOrder(String property, PropertyOrder.Direction direction)
Make a sort order for use in a query.static PropertyReference.Builder
makePropertyReference(String propertyName)
Make a property reference for use in a query.static Mutation.Builder
makeUpdate(Entity entity)
static Mutation.Builder
makeUpsert(Entity entity)
static Value.Builder
makeValue(boolean value)
Make a boolean value.static Value.Builder
makeValue(double value)
Make a floating point value.static Value.Builder
makeValue(long key)
Make an integer value.static Value.Builder
makeValue(Entity entity)
Make an entity value.static Value.Builder
makeValue(Entity.Builder entity)
Make a entity value.static Value.Builder
makeValue(Key key)
Make a key value.static Value.Builder
makeValue(Key.Builder key)
Make a key value.static Value.Builder
makeValue(Value.Builder value1, Value.Builder value2, Value.Builder... rest)
Make an array value containing the specified values.static Value.Builder
makeValue(Value value1, Value value2, Value... rest)
Make a list value containing the specified values.static Value.Builder
makeValue(com.google.protobuf.ByteString blob)
Make a ByteString value.static Value.Builder
makeValue(com.google.type.LatLng value)
Makes a GeoPoint value.static Value.Builder
makeValue(com.google.type.LatLng.Builder value)
Makes a GeoPoint value.static Value.Builder
makeValue(Iterable<Value> values)
Make an array value containing the specified values.static Value.Builder
makeValue(String value)
Make a string value.static Value.Builder
makeValue(Date date)
Make a timestamp value given a date.static Date
toDate(Value value)
Convert a timestamp value into aDate
clipping off the microseconds.
-
-
-
Field Detail
-
SCATTER_PROPERTY_NAME
public static final String SCATTER_PROPERTY_NAME
The property used in the Datastore to give us a random distribution. *- See Also:
- Constant Field Values
-
KEY_PROPERTY_NAME
public static final String KEY_PROPERTY_NAME
The property used in the Datastore to get the key of the entity. *- See Also:
- Constant Field Values
-
PROJECT_ID_ENV_VAR
public static final String PROJECT_ID_ENV_VAR
Name of the environment variable used to set the project ID.- See Also:
- Constant Field Values
-
LOCAL_HOST_ENV_VAR
public static final String LOCAL_HOST_ENV_VAR
Name of the environment variable used to set the local host.- See Also:
- Constant Field Values
-
SERVICE_ACCOUNT_ENV_VAR
public static final String SERVICE_ACCOUNT_ENV_VAR
Name of the environment variable used to set the service account.- See Also:
- Constant Field Values
-
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.- See Also:
- Constant Field Values
-
-
Method Detail
-
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:
GeneralSecurityException
IOException
-
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 ornull
if not.- Returns:
- valid credentials or
null
- Throws:
GeneralSecurityException
IOException
-
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 ornull
if not.- Returns:
- valid credentials or
null
- Throws:
GeneralSecurityException
IOException
-
getOptionsFromEnv
public static DatastoreOptions.Builder getOptionsFromEnv() throws GeneralSecurityException, IOException
Constructs aDatastore
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:
- 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:
GeneralSecurityException
IOException
-
getProjectIdFromComputeEngine
@Nullable public static String getProjectIdFromComputeEngine()
Gets the project ID from the Compute Engine metadata server. Returnsnull
if the project ID cannot be determined (because, for instance, the code is not running on Compute Engine).
-
getDatastoreFromEnv
public static Datastore getDatastoreFromEnv() throws GeneralSecurityException, IOException
- Throws:
GeneralSecurityException
IOException
- See Also:
getOptionsFromEnv()
-
getQuerySplitter
public static QuerySplitter 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
public static Comparator<Key> getKeyComparator()
-
makeOrder
public static PropertyOrder.Builder makeOrder(String property, PropertyOrder.Direction direction)
Make a sort order for use in a query.
-
makeAncestorFilter
public static Filter.Builder makeAncestorFilter(Key ancestor)
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
public static Filter.Builder makeAndFilter(Filter... subfilters)
Make a composite filter from the given sub-filters using AND to combine filters.
-
makeAndFilter
public static Filter.Builder makeAndFilter(Iterable<Filter> subfilters)
Make a composite filter from the given sub-filters using AND to combine filters.
-
makePropertyReference
public static PropertyReference.Builder makePropertyReference(String propertyName)
Make a property reference for use in a query.
-
makeValue
public static Value.Builder makeValue(Iterable<Value> values)
Make an array value containing the specified values.
-
makeValue
public static Value.Builder makeValue(Value value1, Value value2, Value... rest)
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
public static Value.Builder makeValue(Key key)
Make a key value.
-
makeValue
public static Value.Builder makeValue(Key.Builder key)
Make a key value.
-
makeValue
public static Value.Builder makeValue(long key)
Make an integer value.
-
makeValue
public static Value.Builder makeValue(double value)
Make a floating point value.
-
makeValue
public static Value.Builder makeValue(boolean value)
Make a boolean value.
-
makeValue
public static Value.Builder makeValue(String value)
Make a string value.
-
makeValue
public static Value.Builder makeValue(Entity entity)
Make an entity value.
-
makeValue
public static Value.Builder makeValue(Entity.Builder entity)
Make a entity value.
-
makeValue
public static Value.Builder makeValue(com.google.protobuf.ByteString blob)
Make a ByteString value.
-
makeValue
public static Value.Builder makeValue(Date date)
Make a timestamp value given a date.
-
makeValue
public static Value.Builder makeValue(com.google.type.LatLng value)
Makes a GeoPoint value.
-
makeValue
public static Value.Builder makeValue(com.google.type.LatLng.Builder value)
Makes a GeoPoint value.
-
makeKey
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.
-
getDouble
public static double getDouble(Value value)
- Returns:
- the double contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a double.
-
getKey
public static Key getKey(Value value)
- Returns:
- the key contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a key.
-
getByteString
public static com.google.protobuf.ByteString getByteString(Value value)
- Returns:
- the blob contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a blob.
-
getEntity
public static Entity getEntity(Value value)
- Returns:
- the entity contained in value
- Throws:
IllegalArgumentException
- if the value does not contain an entity.
-
getString
public static String getString(Value value)
- Returns:
- the string contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a string.
-
getBoolean
public static boolean getBoolean(Value value)
- Returns:
- the boolean contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a boolean.
-
getLong
public static long getLong(Value value)
- Returns:
- the long contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a long.
-
getTimestamp
public static long getTimestamp(Value value)
- Returns:
- the timestamp in microseconds contained in value
- Throws:
IllegalArgumentException
- if the value does not contain a timestamp.
-
getList
public static List<Value> getList(Value value)
- Returns:
- the array contained in value as a list.
- Throws:
IllegalArgumentException
- if the value does not contain an array.
-
toDate
public static Date toDate(Value value)
Convert a timestamp value into aDate
clipping 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
public static Mutation.Builder makeInsert(Entity entity)
- Parameters:
entity
- the entity to insert- Returns:
- a mutation that will insert an entity
-
makeUpdate
public static Mutation.Builder makeUpdate(Entity entity)
- Parameters:
entity
- the entity to update- Returns:
- a mutation that will update an entity
-
makeUpsert
public static Mutation.Builder makeUpsert(Entity entity)
- Parameters:
entity
- the entity to upsert- Returns:
- a mutation that will upsert an entity
-
makeDelete
public static Mutation.Builder makeDelete(Key key)
- Parameters:
key
- the key of the entity to delete- Returns:
- a mutation that will delete an entity
-
-