什么是Google.Cloud.Datastore.V1中的密钥 [英] What is Key in Google.Cloud.Datastore.V1

查看:128
本文介绍了什么是Google.Cloud.Datastore.V1中的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个nuget包的新手,并与 Key 类混淆.

I'm new to this nuget package and confused with the Key class.

这是我的代码基于 Google. Cloud.Datastore.V1 文档:

Here is my code base on Google.Cloud.Datastore.V1 document:

public long InsertMessage<T>(T iEntity) where T : IEntity<T>
{
    var keyFactory = _db.CreateKeyFactory(Kind);
    var entity = iEntity.ToEntity();
    entity.Key = keyFactory.CreateIncompleteKey();

    using (var transaction = _db.BeginTransaction())
    {
        transaction.Insert(entity);
        var commitResponse = transaction.Commit();
        var insertedKey = commitResponse.MutationResults[0].Key;
        Logger.Info($"Inserted key: {insertedKey}");
        return insertedKey.Path[0].Id;
    }
}

我要做的就是创建一个实体并创建一个不完整的密钥,发送给服务器,然后从服务器取回填充的密钥.

All I do is to create an entity and create an incomplete key, send to the server, then get the populated key back from server.

我认为密钥被用作实体的唯一身份.

如果有误会,请纠正我.

If there's misunderstanding please correct me.

我可以通过查询获取实体,如下所示:

I can get entities by Query as below:

var query = new Query(Kind)
{
    Filter = Filter.Equal("key", key),
    Order = { { "created", PropertyOrder.Types.Direction.Ascending } }
};
foreach (var entity in _db.RunQueryLazily(query))
{
    list.Add(entity);
}

但是我不知道如何使用插入时通过Filter.Equal("key", key)获取唯一实体的键.

But I don't know how to use the key I got when inserted to get the unique entity by Filter.Equal("key", key).

该示例显示键的结构为:

{
    "partitionId":
    {
        "projectId": "projectId",
        "namespaceId": "namespaceId"
    },
    "path": [
    {
        "kind": "kind",
        "id": "id"
    }]
}

在这里,我总结一下我的问题:

  1. 密钥类的用途是什么?
  2. Key的路径是什么,为什么是数组?
  3. id是实体的唯一密钥吗?
  4. 如何通过KeyIdQuery?
  1. What is Key class for?
  2. What is Key's path and why it's array?
  3. Is id the unique key to the entity?
  4. How to Query by Key and Id?

感谢您的阅读,请不要介意我的英语不好.

Thanks for reading and please don't mind my poor English.

推荐答案

键是文档的唯一标识符. 文档表示最好:

A key is a unique identifier for a document. The documentation says it best:

Cloud Datastore中的每个实体都有一个唯一标识它的 key .密钥包含以下组件:

Each entity in Cloud Datastore has a key that uniquely identifies it. The key consists of the following components:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆