我们如何从 google 数据存储中的特定命名空间获取所有种类的列表?
How can we fetch list of all the kinds from a particular namespace in google datastore?
根据https://cloud.google.com/datastore/docs/concepts/metadataqueries
Kind queries return entities of kind kind whose key name is the name of an entity kind. Queries of this type are implicitly restricted to the current namespace and support filtering only for ranges over the key pseudoproperty. The results can be sorted by ascending (but not descending) key value. Because kind entities have no properties, both keys-only and non-keys-only queries return the same information.
我不确定当前命名空间是什么意思,如何设置当前命名空间?在创建客户端对象时将命名空间作为参数传递,在查询后出现错误。
代码:
from os import name
from google.cloud import datastore
import csv
# For help authenticating your client, visit
# https://cloud.google.com/docs/authentication/getting-started
client = datastore.Client()
datstore_kinds_file = "datastore_kinds.csv"
# All namespaces
query = client.query(kind="__namespace__")
query.keys_only()
all_namespaces = [entity.key.id_or_name for entity in query.fetch()]
namespaces_list_with_kinds=[]
for namespace in all_namespaces:
print(f"====Processing namespace {namespace}====")
client = datastore.Client(namespace=namespace)
query = client.query(kind="__kind__")
query.keys_only()
kinds = [entity.key.id_or_name for entity in query.fetch()]
kinds.insert(0,namespace)
namespaces_list_with_kinds.append(kinds)
# writing to csv file
with open(datstore_kinds_file, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerows(namespaces_list_with_kinds)
错误:
Traceback (most recent call last):
File "get_all_datastore_kinds.py", line 25, in <module>
kinds = [entity.key.id_or_name for entity in query.fetch()]
File "get_all_datastore_kinds.py", line 25, in <listcomp>
kinds = [entity.key.id_or_name for entity in query.fetch()]
File "/home/vishal/.local/lib/python3.8/site-packages/google/api_core/page_iterator.py", line 212, in _items_iter
for page in self._page_iter(increment=False):
File "/home/vishal/.local/lib/python3.8/site-packages/google/api_core/page_iterator.py", line 243, in _page_iter
page = self._next_page()
File "/home/vishal/.local/lib/python3.8/site-packages/google/cloud/datastore/query.py", line 566, in _next_page
partition_id = entity_pb2.PartitionId(
TypeError: 1 has type int, but expected one of: bytes, unicode
有人可以帮忙吗,谢谢。
如果您希望自己获取种类名称,您可能应该使用统计实体。值得注意的是,__Stat_Ns_Kind__
应该有您要查找的数据。
至于您当前的问题,我猜有些命名空间键被解释为数字。您可以通过在循环中打印名称空间的值和类型来调试它。
错误是由于行 kinds = [entity.key.id_or_name for entity in query.fetch()]
问题是因为 entity.key.id_or_name
对 return 命名空间的 id
(int) 或 name
(string) .
Datastore 有一个默认命名空间,它是一个空字符串(null,None)。由于空字符串不是有效的键名,因此此命名空间被替换为 整数 值 1
(请参阅 documentation)。当您 运行 项目符号 1 中的代码时,您将得到一个类似于 [1, <namespace_2>, <namespace_3>]
的列表,其中 namespace_2, namespace_3
是字符串。当您随后在 Kinds
查询中使用命名空间时,您会收到错误消息,因为查询需要命名空间的字符串(名称)而不是整数(列表中的第一个值为 1)。
解决方案在下面的代码中给出。本质上,我用 entity.key.name
替换了 entity.key.id_or_name
,迫使它只 return string
。默认命名空间将显示值 null
(如果您将其转储为 JSON 或 None
如果您打印到控制台,则为命名空间的名称。
注意我 运行 你的代码,得到了和你一样的错误然后 运行 我自己的代码,我没有得到错误
dsClient = datastore.Client()
# All namespaces
query = dsClient.query(kind="__namespace__")
query.keys_only()
all_namespaces = [entity.key.name for entity in query.fetch()] # entity.key.id_or_name
namespaces_list_with_kinds=[]
for namespace in all_namespaces:
print(f"====Processing namespace {namespace}====")
#client = datastore.Client(namespace=namespace)
query = dsClient.query(namespace = namespace, kind="__kind__")
query.keys_only()
kinds = [entity.key.id_or_name for entity in query.fetch()]
kinds.insert(0,namespace)
namespaces_list_with_kinds.append(kinds)
return json.dumps(namespaces_list_with_kinds)
根据https://cloud.google.com/datastore/docs/concepts/metadataqueries
Kind queries return entities of kind kind whose key name is the name of an entity kind. Queries of this type are implicitly restricted to the current namespace and support filtering only for ranges over the key pseudoproperty. The results can be sorted by ascending (but not descending) key value. Because kind entities have no properties, both keys-only and non-keys-only queries return the same information.
我不确定当前命名空间是什么意思,如何设置当前命名空间?在创建客户端对象时将命名空间作为参数传递,在查询后出现错误。
代码:
from os import name
from google.cloud import datastore
import csv
# For help authenticating your client, visit
# https://cloud.google.com/docs/authentication/getting-started
client = datastore.Client()
datstore_kinds_file = "datastore_kinds.csv"
# All namespaces
query = client.query(kind="__namespace__")
query.keys_only()
all_namespaces = [entity.key.id_or_name for entity in query.fetch()]
namespaces_list_with_kinds=[]
for namespace in all_namespaces:
print(f"====Processing namespace {namespace}====")
client = datastore.Client(namespace=namespace)
query = client.query(kind="__kind__")
query.keys_only()
kinds = [entity.key.id_or_name for entity in query.fetch()]
kinds.insert(0,namespace)
namespaces_list_with_kinds.append(kinds)
# writing to csv file
with open(datstore_kinds_file, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerows(namespaces_list_with_kinds)
错误:
Traceback (most recent call last):
File "get_all_datastore_kinds.py", line 25, in <module>
kinds = [entity.key.id_or_name for entity in query.fetch()]
File "get_all_datastore_kinds.py", line 25, in <listcomp>
kinds = [entity.key.id_or_name for entity in query.fetch()]
File "/home/vishal/.local/lib/python3.8/site-packages/google/api_core/page_iterator.py", line 212, in _items_iter
for page in self._page_iter(increment=False):
File "/home/vishal/.local/lib/python3.8/site-packages/google/api_core/page_iterator.py", line 243, in _page_iter
page = self._next_page()
File "/home/vishal/.local/lib/python3.8/site-packages/google/cloud/datastore/query.py", line 566, in _next_page
partition_id = entity_pb2.PartitionId(
TypeError: 1 has type int, but expected one of: bytes, unicode
有人可以帮忙吗,谢谢。
如果您希望自己获取种类名称,您可能应该使用统计实体。值得注意的是,__Stat_Ns_Kind__
应该有您要查找的数据。
至于您当前的问题,我猜有些命名空间键被解释为数字。您可以通过在循环中打印名称空间的值和类型来调试它。
错误是由于行
kinds = [entity.key.id_or_name for entity in query.fetch()]
问题是因为
entity.key.id_or_name
对 return 命名空间的id
(int) 或name
(string) .Datastore 有一个默认命名空间,它是一个空字符串(null,None)。由于空字符串不是有效的键名,因此此命名空间被替换为 整数 值
1
(请参阅 documentation)。当您 运行 项目符号 1 中的代码时,您将得到一个类似于[1, <namespace_2>, <namespace_3>]
的列表,其中namespace_2, namespace_3
是字符串。当您随后在Kinds
查询中使用命名空间时,您会收到错误消息,因为查询需要命名空间的字符串(名称)而不是整数(列表中的第一个值为 1)。解决方案在下面的代码中给出。本质上,我用
entity.key.name
替换了entity.key.id_or_name
,迫使它只 returnstring
。默认命名空间将显示值null
(如果您将其转储为 JSON 或None
如果您打印到控制台,则为命名空间的名称。注意我 运行 你的代码,得到了和你一样的错误然后 运行 我自己的代码,我没有得到错误
dsClient = datastore.Client()
# All namespaces
query = dsClient.query(kind="__namespace__")
query.keys_only()
all_namespaces = [entity.key.name for entity in query.fetch()] # entity.key.id_or_name
namespaces_list_with_kinds=[]
for namespace in all_namespaces:
print(f"====Processing namespace {namespace}====")
#client = datastore.Client(namespace=namespace)
query = dsClient.query(namespace = namespace, kind="__kind__")
query.keys_only()
kinds = [entity.key.id_or_name for entity in query.fetch()]
kinds.insert(0,namespace)
namespaces_list_with_kinds.append(kinds)
return json.dumps(namespaces_list_with_kinds)