【问题标题】:How can we fetch list of all the kinds from a particular namespace in google datastore?我们如何从谷歌数据存储中的特定命名空间中获取所有类型的列表?
【发布时间】:2021-10-05 15:27:59
【问题描述】:

根据https://cloud.google.com/datastore/docs/concepts/metadataqueries

种类查询返回种类种类的实体,其键名是实体种类的名称。这种类型的查询被隐式地限制在当前命名空间中,并且只支持对 key 伪属性上的范围进行过滤。结果可以按升序(但不是降序)key 值排序。由于 kind 实体没有属性,因此仅键和非仅键查询都返回相同的信息。

我不确定当前命名空间是什么意思,如何设置当前命名空间?在创建客户端对象时传递命名空间作为参数,在查询后给我错误。

代码:

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

有人可以帮忙吗,谢谢。

【问题讨论】:

    标签: python-3.x google-api google-cloud-storage google-cloud-datastore


    【解决方案1】:
    1. 这个错误是由于kinds = [entity.key.id_or_name for entity in query.fetch()] 这一行引起的

    2. 问题是因为entity.key.id_or_name 表示返回命名空间的id (int) 或name (string)。

    3. Datastore 有一个默认命名空间,它是一个空字符串(null、None)。由于空字符串不是有效的键名,因此此命名空间将替换为 integer1(请参阅 documentation)。当您运行项目符号 1 中的代码时,您最终会得到一个类似于 [1, &lt;namespace_2&gt;, &lt;namespace_3&gt;] 的列表,其中 namespace_2, namespace_3 是字符串。然后,当您在 Kinds 查询中使用命名空间时,您会收到错误消息,因为查询需要命名空间的字符串(名称)而不是整数(列表中的第一个值是 1)。

    4. 解决方案在下面的代码中给出。本质上,我用entity.key.name 替换了entity.key.id_or_name,迫使它只返回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)
    

    【讨论】:

      【解决方案2】:

      如果您希望自己获取种类名称,您可能应该使用 stats 实体。值得注意的是,__Stat_Ns_Kind__ 应该有您要查找的数据。

      至于您当前的问题,我猜有一些名称空间键被解释为数字。您可以通过在循环中打印命名空间的值和类型来调试它。

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 1970-01-01
        • 2010-10-31
        • 1970-01-01
        • 2010-09-25
        • 2020-01-26
        相关资源
        最近更新 更多