Python google.cloud.datastore.Client() Examples

The following are 30 code examples of google.cloud.datastore.Client(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.cloud.datastore , or try the search function .
Example #1
Source File: datastore.py    From loaner with Apache License 2.0 6 votes vote down vote up
def from_config(cls, config, creds=None):
    """Returns an initialized DatastoreAPI object.

    Args:
      config: common.ProjectConfig, the project configuration.
      creds: auth.CloudCredentials, the credentials to use for client
          authentication.

    Returns:
      An authenticated DatastoreAPI instance.
    """
    if creds is None:
      creds = auth.CloudCredentials(config, cls.SCOPES)
    client = datastore.Client(
        project=config.project, credentials=creds.get_credentials(cls.SCOPES))
    return cls(config, client) 
Example #2
Source File: cloud_datastore_ycsb_benchmark.py    From PerfKitBenchmarker with Apache License 2.0 6 votes vote down vote up
def GetDatastoreDeleteCredentials():
  """Returns credentials to datastore db."""
  if FLAGS.google_datastore_deletion_keyfile.startswith('gs://'):
    # Copy private keyfile to local disk
    cp_cmd = [
        'gsutil', 'cp', FLAGS.google_datastore_deletion_keyfile,
        FLAGS.private_keyfile
    ]
    vm_util.IssueCommand(cp_cmd)
    credentials_path = FLAGS.private_keyfile
  else:
    credentials_path = FLAGS.google_datastore_deletion_keyfile

  credentials = service_account.Credentials.from_service_account_file(
      credentials_path,
      scopes=datastore.client.Client.SCOPE,
  )

  return credentials 
Example #3
Source File: quickstart.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def run_quickstart():
    # [START datastore_quickstart]
    # Imports the Google Cloud client library
    from google.cloud import datastore

    # Instantiates a client
    datastore_client = datastore.Client()

    # The kind for the new entity
    kind = 'Task'
    # The name/ID for the new entity
    name = 'sampletask1'
    # The Cloud Datastore key for the new entity
    task_key = datastore_client.key(kind, name)

    # Prepares the new entity
    task = datastore.Entity(key=task_key)
    task['description'] = 'Buy milk'

    # Saves the entity
    datastore_client.put(task)

    print('Saved {}: {}'.format(task.key.name, task['description']))
    # [END datastore_quickstart] 
Example #4
Source File: test_build_datastore_template.py    From example_dataproc_twitter with MIT License 5 votes vote down vote up
def test_main(self, config_mock):
        kind = 'unittest-example-dataproc'
        config2['input'] = 'tests/system/data/dataflow/*.json.gz'
        config2['similarities_cap'] = 5
        config2['kind'] = kind
        config_mock.items.return_value = config2.items()
        expected = {'sku0': {'items': ['sku8', 'sku7', 'sku6', 'sku5', 'sku4'],
            'scores': [0.8, 0.7, 0.6, 0.5, 0.4]},
                    'sku1': {'items': ['sku0', 'sku2', 'sku3', 'sku4', 'sku5'],
            'scores': [0.8, 0.7, 0.6, 0.5, 0.4]},
                    'sku2': {'items': ['sku2', 'sku0'],
            'scores': [0.7, 0.2]}}
        dsc = ds.Client()
        keys = map(lambda x: dsc.key(kind, x), ['sku0', 'sku1', 'sku2'])

        exporter.main()
 
        ds_keys = dsc.get_multi(keys)
        for key in ds_keys:
            name = key.key.name
            self.assertEqual(expected[name]['items'], key['items'])
            self.assertEqual(expected[name]['scores'], key['scores'])

        dsc.delete_multi(keys)
        key0 = dsc.get(keys[0])
        self.assertEqual(key0, None) 
Example #5
Source File: google_datastore.py    From poem with MIT License 5 votes vote down vote up
def __init__(self):
        self.client = datastore.Client() 
Example #6
Source File: data_ingestion_configurable.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def _fetch_table(table_name):
    try:
        client = datastore.Client()
    except GoogleAuthError:
        # TODO(lcaggioni.ludomagno): fail gracefully
        pass
    return client.get(client.key('Table', table_name)) 
Example #7
Source File: datastore_schema_import.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--schema-file',
                        dest='schema_file',
                        required=True,
                        help='''
            File containing the schema of the input CSV data to be
            imported.
            Filename should be the same as the BQ table name that you
            want: "TABLENAME.csv".
            This file will be used to create a DataStore entity.
            Example:
                COLUMN_1,STRING
                COLUMN_2,FLOAT
            ''')
    args = parser.parse_args()
    client = datastore.Client()

    filename = args.schema_file
    print(('Processing file %s' % filename))
    csvfile = open(filename, 'r')
    table = os.path.splitext(os.path.basename(filename))[0]
    filetext = csv.reader(csvfile, delimiter=',')
    fields = collections.OrderedDict()
    for rows in filetext:
        fields[rows[0]] = rows[1]
    key = client.key('Table', table)
    entry = datastore.Entity(key, exclude_from_indexes=['columns'])
    entry.update({"columns": str(json.dumps(fields), "utf-8")})
    client.put(entry)
    print(('Created/Updated entry for table %s.' % table))
    print('Done.') 
Example #8
Source File: api.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.datastore = datastore.Client()
        self.crm = resource_manager.Client() 
Example #9
Source File: datastore_cache.py    From cloud-opensource-python with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._datastore_client = datastore.Client() 
Example #10
Source File: database.py    From gabenizer with MIT License 5 votes vote down vote up
def client(self) -> datastore.Client:
        if not self._client:
            self._client = datastore.Client()
        return self._client 
Example #11
Source File: datastore.py    From example_dataproc_twitter with MIT License 5 votes vote down vote up
def __init__(self, credentials=None):
        self.client = (ds.Client(credentials=credentials) if credentials
            else ds.Client()) 
Example #12
Source File: datastore_adapter.py    From anom-py with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, *, project=None, credentials=None):
        self.project = project
        self.credentials = credentials
        self.proxy = DatastoreRequestsProxy(credentials=credentials)
        self.client = datastore.Client(
            credentials=self.credentials,
            project=self.project,
            _http=self.proxy,
            _use_grpc=False,
        ) 
Example #13
Source File: model_datastore.py    From getting-started-python with Apache License 2.0 5 votes vote down vote up
def get_client():
    return datastore.Client(current_app.config['PROJECT_ID']) 
Example #14
Source File: cloud_datastore_ycsb_benchmark.py    From PerfKitBenchmarker with Apache License 2.0 5 votes vote down vote up
def DeleteEntities(self, dataset_id, credentials, delete_entities):
    """Deletes entities in a datastore database in batches.

    Args:
      dataset_id: Cloud Datastore client dataset id.
      credentials: Cloud Datastore client credentials.
      delete_entities: Entities to delete.

    Returns:
      number of records deleted.
    Raises:
      ValueError: In case of delete failures.
    """
    try:
      client = datastore.Client(project=dataset_id, credentials=credentials)
      logging.info('Task %d - Started deletion for %s', self.task_id, self.kind)
      while delete_entities:
        chunk = delete_entities[:_CLEANUP_KIND_DELETE_OP_BATCH_SIZE]
        delete_entities = delete_entities[_CLEANUP_KIND_DELETE_OP_BATCH_SIZE:]
        client.delete_multi(chunk)
        self.entity_deletion_count += len(chunk)

      logging.info('Task %d - Completed deletion for %s - %d', self.task_id,
                   self.kind, self.entity_deletion_count)
      return self.entity_deletion_count
    except ValueError as error:
      logging.exception('Task %d - Delete entities for %s failed due to %s',
                        self.task_id, self.kind, error)
      self.deletion_error = True
      raise error 
Example #15
Source File: cloud_datastore_ycsb_benchmark.py    From PerfKitBenchmarker with Apache License 2.0 5 votes vote down vote up
def Prepare(benchmark_spec):
  """Prepare the virtual machines to run cloud datastore.

  Args:
    benchmark_spec: The benchmark specification. Contains all data that is
        required to run the benchmark.
  """
  benchmark_spec.always_call_cleanup = True

  # Check that the database is empty before running
  if FLAGS.google_datastore_deletion_keyfile:
    dataset_id = FLAGS.google_datastore_datasetId
    credentials = GetDatastoreDeleteCredentials()

    client = datastore.Client(project=dataset_id, credentials=credentials)

    for kind in _YCSB_COLLECTIONS:
      # TODO(user): Allow a small number of leftover entities until we
      # figure out why these are not getting deleted.
      if len(list(client.query(kind=kind).fetch(limit=200))) > 100:
        raise errors.Benchmarks.PrepareException(
            'Database is non-empty. Stopping test.')

  else:
    logging.warning('Test could be executed on a non-empty database.')

  vms = benchmark_spec.vms

  # Install required packages and copy credential files
  vm_util.RunThreaded(_Install, vms)

  # Restore YCSB_TAR_URL
  benchmark_spec.executor = ycsb.YCSBExecutor('googledatastore') 
Example #16
Source File: cloud_datastore_ycsb_benchmark.py    From PerfKitBenchmarker with Apache License 2.0 5 votes vote down vote up
def _CreateClient(dataset_id, credentials):
  """Creates a datastore client for the dataset using the credentials.

  Args:
    dataset_id: Cloud Datastore client dataset id.
    credentials: Cloud Datastore client credentials.

  Returns:
    Datastore client.
  """
  return datastore.Client(project=dataset_id, credentials=credentials) 
Example #17
Source File: cloud_client.py    From cleverhans with MIT License 5 votes vote down vote up
def __init__(self, project_id, bucket_name):
    """Initialize client with project id and name of the storage bucket."""
    self.project_id = project_id
    self.bucket_name = bucket_name
    self.client = storage.Client(project=project_id)
    self.bucket = self.client.get_bucket(bucket_name) 
Example #18
Source File: cloud_client.py    From cleverhans with MIT License 5 votes vote down vote up
def __init__(self, project_id, namespace=None):
    """Init this method with given project id and optional namespace."""
    self._client = datastore.Client(project=project_id, namespace=namespace) 
Example #19
Source File: lookup.py    From realtime-embeddings-matching with Apache License 2.0 5 votes vote down vote up
def __init__(self, kind):
    logging.info('Initialising datastore lookup utility...')
    self.kind = kind
    self.client = datastore.Client()
    logging.info('Datastore lookup utility initialised.') 
Example #20
Source File: cloud_client.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, project_id, bucket_name):
    """Initialize client with project id and name of the storage bucket."""
    self.project_id = project_id
    self.bucket_name = bucket_name
    self.client = storage.Client(project=project_id)
    self.bucket = self.client.get_bucket(bucket_name) 
Example #21
Source File: main.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def homepage():
    # Create a Cloud Datastore client.
    datastore_client = datastore.Client()

    # Use the Cloud Datastore client to fetch information from Datastore about
    # each photo.
    query = datastore_client.query(kind='Faces')
    image_entities = list(query.fetch())

    # Return a Jinja2 HTML template and pass in image_entities as a parameter.
    return render_template('homepage.html', image_entities=image_entities) 
Example #22
Source File: main.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def index():
    ds = datastore.Client()

    user_ip = request.remote_addr

    # Keep only the first two octets of the IP address.
    if is_ipv6(user_ip):
        user_ip = ':'.join(user_ip.split(':')[:2])
    else:
        user_ip = '.'.join(user_ip.split('.')[:2])

    entity = datastore.Entity(key=ds.key('visit'))
    entity.update({
        'user_ip': user_ip,
        'timestamp': datetime.datetime.utcnow()
    })

    ds.put(entity)
    query = ds.query(kind='visit', order=('-timestamp',))

    results = []
    for x in query.fetch(limit=10):
        try:
            results.append('Time: {timestamp} Addr: {user_ip}'.format(**x))
        except KeyError:
            print("Error with result format, skipping entry.")

    output = 'Last 10 visits:\n{}'.format('\n'.join(results))

    return output, 200, {'Content-Type': 'text/plain; charset=utf-8'}
# [END gae_flex_datastore_app] 
Example #23
Source File: tasks_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def client():
    client = datastore.Client(PROJECT)

    yield client

    # Delete anything created during the test.
    with client.batch():
        client.delete_multi(
            [x.key for x in client.query(kind='Task').fetch()]) 
Example #24
Source File: tasks.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def create_client(project_id):
    return datastore.Client(project_id)
# [END datastore_build_service]


# [START datastore_add_entity] 
Example #25
Source File: snippets.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def main(project_id):
    client = datastore.Client(project_id)

    for name, function in globals().iteritems():
        if name in ('main', 'defaultdict') or not callable(function):
            continue

        print(name)
        pprint(function(client))
        print('\n-----------------\n') 
Example #26
Source File: state_manager.py    From turbinia with Apache License 2.0 5 votes vote down vote up
def __init__(self):
    config.LoadConfig()
    try:
      self.client = datastore.Client(project=config.TURBINIA_PROJECT)
    except EnvironmentError as e:
      message = (
          'Could not create Datastore client: {0!s}\n'
          'Have you run $ gcloud auth application-default login?'.format(e))
      raise TurbiniaException(message) 
Example #27
Source File: task_manager.py    From turbinia with Apache License 2.0 5 votes vote down vote up
def _backend_setup(self, server=True, *args, **kwargs):
    """
    Args:
      server (bool): Whether this is the client or a server

    Raises:
      TurbiniaException: When there are errors creating PSQ Queue
    """

    log.debug(
        'Setting up PSQ Task Manager requirements on project {0:s}'.format(
            config.TURBINIA_PROJECT))
    self.server_pubsub = turbinia_pubsub.TurbiniaPubSub(config.PUBSUB_TOPIC)
    if server:
      self.server_pubsub.setup_subscriber()
    else:
      self.server_pubsub.setup_publisher()
    psq_publisher = pubsub.PublisherClient()
    psq_subscriber = pubsub.SubscriberClient()
    datastore_client = datastore.Client(project=config.TURBINIA_PROJECT)
    try:
      self.psq = psq.Queue(
          psq_publisher, psq_subscriber, config.TURBINIA_PROJECT,
          name=config.PSQ_TOPIC, storage=psq.DatastoreStorage(datastore_client))
    except exceptions.GoogleCloudError as e:
      msg = 'Error creating PSQ Queue: {0:s}'.format(str(e))
      log.error(msg)
      raise turbinia.TurbiniaException(msg) 
Example #28
Source File: conftest.py    From python-ndb with Apache License 2.0 5 votes vote down vote up
def client_context(namespace):
    client = ndb.Client()
    context_manager = client.context(
        cache_policy=False, legacy_data=False, namespace=namespace,
    )
    with context_manager as context:
        yield context 
Example #29
Source File: conftest.py    From python-ndb with Apache License 2.0 5 votes vote down vote up
def _make_ds_client(namespace):
    emulator = bool(os.environ.get("DATASTORE_EMULATOR_HOST"))
    if emulator:
        client = datastore.Client(namespace=namespace, _http=requests.Session)
    else:
        client = datastore.Client(namespace=namespace)

    return client 
Example #30
Source File: gcdatastore.py    From Flask-Blogging with MIT License 5 votes vote down vote up
def __init__(self, namespace=None):
        self._logger = logging.getLogger("flask-blogging")
        self._client = datastore.Client(namespace=namespace)