def fetch_keys(kind, fetch_max=FETCH_MAX, query=None, cursor=None): if query is None: query = datastore.Query(kind=kind, projection=['__key__']) iterator = query.fetch(limit=fetch_max, start_cursor=cursor) entities, _, cursor = iterator.next_page() return query, entities, cursor
def test_save_key_self_reference(self): parent_key = datastore.Key('Residence', 'NewYork') key = datastore.Key('Person', 'name', parent=parent_key) entity = datastore.Entity(key=key) entity['fullName'] = u'Full name' entity['linkedTo'] = key # Self reference. datastore.put([entity]) self.case_entities_to_delete.append(entity) query = datastore.Query(kind='Person') # Adding ancestor to ensure consistency. query.ancestor = parent_key query.add_filter('linkedTo', '=', key) stored_persons = list(query.fetch(limit=2)) self.assertEqual(stored_persons, [entity])
def test_save_key_self_reference(self): key = datastore.Key('Person', 'name') entity = datastore.Entity(key=key) entity['fullName'] = u'Full name' entity['linkedTo'] = key # Self reference. with datastore.Transaction(): datastore.put([entity]) self.case_entities_to_delete.append(entity) query = datastore.Query(kind='Person') query.add_filter('linkedTo', '=', key) stored_persons = list(query.fetch(limit=2)) self.assertEqual(len(stored_persons), 1) stored_person = stored_persons[0] self.assertEqual(stored_person['fullName'], entity['fullName']) self.assertEqual(stored_person.key.path, key.path) self.assertEqual(stored_person.key.namespace, key.namespace)
new_story = datastore.Entity(key=story_key) #Add some fields new_story["url"] = unicode("cloud.google.com") new_story["title"] = unicode("Google is an awesome cloud provider") new_story["text"] = unicode( "Check out this cool website I found for Cloud Stuff!") new_story['timestamp'] = datetime.datetime.now() print(new_story) #Push entity to the Cloud Datastore client.put(new_story) #Query for the stories #This query is eventually consistant query = datastore.Query(client=client, kind='Story', order=["-timestamp"]) results = list(query.fetch()) print(results) #Make another story new_story = datastore.Entity(key=client.key("Story")) new_story["url"] = unicode("blog.sandeepdinesh.com") new_story["title"] = unicode("Interesting Blog") new_story["text"] = unicode( "This is a pretty cool blog I write. Check it out!") new_story['timestamp'] = datetime.datetime.now() client.put(new_story) #Query for the stories results = list(query.fetch()) print(results)
# Now let's try a more advanced query. # First, let's create some entities. SAMPLE_DATA = [(1234, 'Computer', 10), (2345, 'Computer', 8), (3456, 'Laptop', 10), (4567, 'Printer', 11), (5678, 'Printer', 12), (6789, 'Computer', 13)] sample_keys = [] for id, name, age in SAMPLE_DATA: key = datastore.Key('Thing', id) sample_keys.append(key) entity = datastore.Entity(key) entity['name'] = name entity['age'] = age datastore.put([entity]) # We'll start by look at all Thing entities: query = datastore.Query(kind='Thing') # Let's look at the first two. print(list(query.fetch(limit=2))) # Now let's check for Thing entities named 'Computer' query.add_filter('name', '=', 'Computer') print(list(query.fetch())) # If you want to filter by multiple attributes, # you can call .add_filter multiple times on the query. query.add_filter('age', '=', 10) print(list(query.fetch())) # Now delete them. datastore.delete(sample_keys)
def _base_query(self): return datastore.Query(kind='Character', ancestor=self.ANCESTOR_KEY)
def test_empty_kind(self): query = datastore.Query(kind='Post') query.ancestor = self.PARENT posts = list(query.fetch(limit=2)) self.assertEqual(posts, [])
from gcloud import datastore if __name__ == '__main__': datastore_client = datastore.Client.from_service_account_json( json_credentials_path='data/pulsarcloud-sweeper.json', project='ml-pulsarcloud') qry = datastore.Query(kind='ResourceConfiguration', client=datastore_client) for result in qry.fetch(): print(result) qry_filter = datastore.Query(kind='ResourceConfiguration', client=datastore_client) qry_filter.add_filter('config_name', '=', 'A1') print(type(qry_filter)) for result in qry_filter.fetch(): print(result)