使用祖先和路径的 GCP DataStore 实体
GCP DataStore entity using ancestor and path
post 问题是 。
我使用的库是这个 - Google Cloud Libraries for .NET.
现在我对单个实体的简单使用没有问题,但是我在尝试通过 .
检索实体的身份时遇到了另一个问题
问题如下:
如何使用库插入具有祖先的实体?
插入祖先实体后,如何获取插入实体的标识?
如何使用PathElement
?
我的英语水平很差,所以我不完全理解文档,所以请不要介意和原谅我。
如有不当或描述不当的地方,请告诉我。
不胜感激。
要创建具有祖先的实体,您只需确保您提供的键具有该祖先。如果您乐于指定新路径元素的名称部分,创建新键的最简单方法是在祖先键上调用 Key.WithElement
,但使用新路径元素。
如果您想生成一个密钥,目前有点难,因为在 Key
上没有像 WithElement
这样的方法来创建一个具有不完整最终元素的新密钥。不过,您可以轻松编写自己的扩展方法来做到这一点。
插入实体时,通过Insert
方法返回插入的键。这是一个完整的示例,首先创建一个 "shelf" 实体,然后创建三个 "book" 实体作为该实体的子实体:
- 密钥中具有指定名称的一个
- 一个不完整的密钥让服务器填写ID,通过扩展方法创建
- 一个
KeyFactory
生成的密钥不完整,以演示该选项。
代码:
using Google.Cloud.Datastore.V1;
using System;
using static Google.Cloud.Datastore.V1.Key.Types;
static class KeyExtensions
{
// Useful extension method to create an incomplete "child" key
public static Key WithIncompleteElement(this Key key, string kind)
{
Key ret = key.Clone();
ret.Path.Add(new PathElement { Kind = kind });
return ret;
}
}
class Program
{
static void Main(string[] args)
{
string projectId = "YOUR-PROJECT-ID-HERE";
DatastoreDb client = DatastoreDb.Create(projectId);
Entity shelf = new Entity
{
Key = client.CreateKeyFactory("shelf").CreateIncompleteKey(),
["genre"] = "fiction"
};
Key shelfKey = client.Insert(shelf);
// Insert a book specifying a complete key
Entity book1 = new Entity
{
Key = shelfKey.WithElement("book", "potter1"),
["title"] = "Harry Potter and the Philosopher's Stone"
};
Key book1Key = client.Insert(book1);
// Insert a book by creating an incomplete key with the extension method
Entity book2 = new Entity
{
Key = shelfKey.WithIncompleteElement("book"),
["title"] = "Harry Potter and the Chamber of Secrets"
};
Key book2Key = client.Insert(book2);
Console.WriteLine($"Inserted key: {book2Key}");
// Insert a book by creating an incomplete key with a KeyFactory
KeyFactory keyFactory = new KeyFactory(shelf, "book");
Entity book3 = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
["title"] = "Harry Potter and the Prisoner of Azkaban"
};
Key book3Key = client.Insert(book3);
Console.WriteLine($"Inserted key: {book3Key}");
Console.WriteLine();
// List all the books
var books = client.RunQuery(new Query { Kind = { new KindExpression { Name = "book" } } });
Console.WriteLine("All books:");
foreach (var book in books.Entities)
{
Console.WriteLine($"{(string) book["title"]}: Key={book.Key}");
}
}
}
post 问题是
我使用的库是这个 - Google Cloud Libraries for .NET.
现在我对单个实体的简单使用没有问题,但是我在尝试通过 .
检索实体的身份时遇到了另一个问题问题如下:
如何使用库插入具有祖先的实体?
插入祖先实体后,如何获取插入实体的标识?
如何使用
PathElement
?
我的英语水平很差,所以我不完全理解文档,所以请不要介意和原谅我。
如有不当或描述不当的地方,请告诉我。
不胜感激。
要创建具有祖先的实体,您只需确保您提供的键具有该祖先。如果您乐于指定新路径元素的名称部分,创建新键的最简单方法是在祖先键上调用 Key.WithElement
,但使用新路径元素。
如果您想生成一个密钥,目前有点难,因为在 Key
上没有像 WithElement
这样的方法来创建一个具有不完整最终元素的新密钥。不过,您可以轻松编写自己的扩展方法来做到这一点。
插入实体时,通过Insert
方法返回插入的键。这是一个完整的示例,首先创建一个 "shelf" 实体,然后创建三个 "book" 实体作为该实体的子实体:
- 密钥中具有指定名称的一个
- 一个不完整的密钥让服务器填写ID,通过扩展方法创建
- 一个
KeyFactory
生成的密钥不完整,以演示该选项。
代码:
using Google.Cloud.Datastore.V1;
using System;
using static Google.Cloud.Datastore.V1.Key.Types;
static class KeyExtensions
{
// Useful extension method to create an incomplete "child" key
public static Key WithIncompleteElement(this Key key, string kind)
{
Key ret = key.Clone();
ret.Path.Add(new PathElement { Kind = kind });
return ret;
}
}
class Program
{
static void Main(string[] args)
{
string projectId = "YOUR-PROJECT-ID-HERE";
DatastoreDb client = DatastoreDb.Create(projectId);
Entity shelf = new Entity
{
Key = client.CreateKeyFactory("shelf").CreateIncompleteKey(),
["genre"] = "fiction"
};
Key shelfKey = client.Insert(shelf);
// Insert a book specifying a complete key
Entity book1 = new Entity
{
Key = shelfKey.WithElement("book", "potter1"),
["title"] = "Harry Potter and the Philosopher's Stone"
};
Key book1Key = client.Insert(book1);
// Insert a book by creating an incomplete key with the extension method
Entity book2 = new Entity
{
Key = shelfKey.WithIncompleteElement("book"),
["title"] = "Harry Potter and the Chamber of Secrets"
};
Key book2Key = client.Insert(book2);
Console.WriteLine($"Inserted key: {book2Key}");
// Insert a book by creating an incomplete key with a KeyFactory
KeyFactory keyFactory = new KeyFactory(shelf, "book");
Entity book3 = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
["title"] = "Harry Potter and the Prisoner of Azkaban"
};
Key book3Key = client.Insert(book3);
Console.WriteLine($"Inserted key: {book3Key}");
Console.WriteLine();
// List all the books
var books = client.RunQuery(new Query { Kind = { new KindExpression { Name = "book" } } });
Console.WriteLine("All books:");
foreach (var book in books.Entities)
{
Console.WriteLine($"{(string) book["title"]}: Key={book.Key}");
}
}
}