简体   繁体   English

如何读取和写入谷歌云数据存储?

[英]How do I read and write to the google cloud datastore?

The documentation in google cloud's datastore section is very confusing to me.谷歌云数据存储部分的文档让我很困惑。

I was under the belief datastore may be used as a key-value storage.我相信数据存储可以用作键值存储。 (Similar to mongoDB) But I guess I misunderstood how its keys work, since I can't use string keys outright, but I need to transform series of strings to a (list) key via some list => dataStore.key(list) transformation. (类似于 mongoDB)但我想我误解了它的键是如何工作的,因为我不能完全使用字符串键,但我需要通过一些列表将一系列字符串转换为(列表)键 => dataStore.key(list)转型。

That's weird, and I don't understand why use a list instead of a string, and I don't understand why I don't use the list outright, and need to use datastore.key first, but I can do that.这很奇怪,我不明白为什么要使用列表而不是字符串,我也不明白为什么我不直接使用列表,而是需要先使用 datastore.key,但我可以这样做。 However, after playing with it a little bit, I discovered that the return value of datastore.key(list) would get me different values for the same list if I run it repeatedly!然而,在稍微玩了一下之后,我发现如果我重复运行它,datastore.key(list) 的返回值会为同一个列表得到不同的值!

So, now I need to somehow remember this key somewhere, but the reason I wanted to use datastore was that I was running in a service with no persistent memory to begin with.所以,现在我需要以某种方式在某个地方记住这个密钥,但我想使用数据存储的原因是我在一个没有持久性 memory 的服务中运行。

Am I using datastore wrong?我使用的数据存储有误吗? Can I use it at all for simple persistent key-value storage?我可以将它用于简单的持久键值存储吗?

It appears that the issue was that I used a list that was too short.看来问题是我使用的列表太短了。

The datastore expects collections, which contain documents, each of which is a key-value mapping.数据存储期望 collections,其中包含文档,每个文档都是一个键值映射。 So instead of having my key point at a mapping, I needed to set a collection, and in it have keys mapped to mappings.因此,我需要设置一个集合,而不是将我的关键点放在映射上,并且在其中将键映射到映射。 (In other words, I needed to have one more element in the keys list) (换句话说,我需要在键列表中多一个元素)

  results = await this.dataStore.get(this.dataStore.key([collectionId, documentId]));

AND

  await this.dataStore.save({ key: this.dataStore.key([collectionId, documentId]), data: someKeyValueObject });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM