什么是Google.Cloud.Datastore.V1中的密钥 [英] What is Key in 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"
}]
}
在这里,我总结一下我的问题:
- 密钥类的用途是什么?
- Key的路径是什么,为什么是数组?
-
id
是实体的唯一密钥吗? - 如何通过
Key
和Id
来Query
?
- What is Key class for?
- What is Key's path and why it's array?
- Is
id
the unique key to the entity? - How to
Query
byKey
andId
?
感谢您的阅读,请不要介意我的英语不好.
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:
- 实体的命名空间,允许多租户
- 实体的种类,出于Cloud Datastore查询的目的对其进行了分类
- 单个实体的标识符,可以是
- 键名字符串
- 整数数字ID
- The namespace of the entity, which allows for multitenancy
- The kind of the entity, which categorizes it for the purpose of Cloud Datastore queries
- An identifier for the individual entity, which can be either
- a key name string
- an integer numeric ID
祖先路径"部分原因是
Path
属性中存在数组的原因-完全限定的键是名称空间,后跟一系列The "ancestor path" part is the reason there's an array in the
Path
property - a fully qualified key is a namespace followed by a sequence of path elements, each of which consists of a kind and "integer ID" or "string name" part.例如,您可能有一个图书馆应用程序.那有书架和书本.忽略名称空间部分,特定的书可能具有一个包含以下路径元素的ID:
So for example, you might have a library application. That has shelves and books. Ignoring the namespace part, a particular book might have an ID with path elements of:
- 种类=货架; Id = 1(数字ID)
- Kind = Book;名称= xyz(字符串名称)
另一种思考方式就像是集合和文档名称的交替序列,例如"/shelves/1/books/xyz".这就是 Firestore 表示其文档ID的方式.
Another way of thinking of this is like an alternating sequence of collection and document names, e.g. "/shelves/1/books/xyz". That's how Firestore represents its document IDs.
任何一个路径元素都不必是唯一的,但是完整的路径是唯一的.没有真正的概念通过键和ID"进行查询. -您可以查找完整的键(例如
DatastoreDb.Lookup
),也可以包含父键作为祖先路径查询.Any one path element doesn't have to be unique, but the complete path is unique. There's no real concept of querying by "key and ID" - you can look up a complete key (e.g.
DatastoreDb.Lookup
) or include a parent key as an ancestor path query.这篇关于什么是Google.Cloud.Datastore.V1中的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!