我正在尝试从数据存储中获取所有数据实体。当我遇到Google文档时,发现与查询投影(Link to the Docs)类似。这是我用来从数据存储区获取所有实体的代码。
List<Distance> list = new ArrayList<>();
list.add(new Distance(100));
list.add(new Distance(10));
list.add(new Distance(1));
list.add(new Distance(40));
list.add(new Distance(30));
List<Distance> backedList=list.subList(<int indexFrom>,<int indexTo>); //backed List- which is backed by original collection, Any change done in backedList will reflect in Original List
Collections.sort(backedList); // Assuming Comparable is implemented in Distance
这是我现在面临的错误
def do_the_query_projection(self, kind_name):
query = self.client.query(kind=kind_name)
query.projection = ['attr_1', 'attr_2', 'attr_3']
#create a list to store
f, m, r = [], [], []
for task in query.fetch():
f.append(task['attr_1'])
m.append(task['attr_2'])
r.append(task['attr_3'])
return f, m, r
是否还有其他方法可以从Cloud Datastore检索所有数据实体?我们是否需要创建复合索引来检索数据?我是GCP的新手。
答案 0 :(得分:2)
尝试在没有投影的情况下进行查询。
当您已经在要检索的列上有了复合索引时,投影查询很有用,因为它使您可以从索引中检索这些列,而无需从数据表中检索任何内容。但是,如果没有索引,则数据存储区将不得不以任何一种方式检索完整数据,因此在这种情况下,它不允许您指定投影。
答案 1 :(得分:1)
要检索数据存储区中的所有实体
def retrieve_all_entities(self):
query = self.client.query(kind=self.kind_name)
all_keys = query.fetch() #fetches all the entities from the datastore
kinds, r, m, f = [] , [], [], []
for keys in all_keys:
kinds.append(keys.key.id_or_name)
r.append(keys['attr_1'])
m.append(keys['attr_2'])
f.append(keys['attr_3'])
return kinds, r, m, f