简体   繁体   English

Appengine之上的键值

[英]Key-Value on top of Appengine

Although appengine already is schema-less, there still need to define the entities that needed to be stored into the Datastore through the Datanucleus persistence layer. 尽管appengine已经没有模式,但是仍然需要定义需要通过Datanucleus持久层存储到DatastoreDatanucleus So I am thinking of a way to get around this; 所以我正在想办法解决这个问题。 by having a layer that will store Key-value at runtime, instead of compile-time Entities. 通过具有将在运行时存储键值而不是编译时实体的层。

The way this is done with Redis is by creating a key like this: 使用Redis的方法是通过创建如下键:

private static final String USER_ID_FORMAT = "user:id:%s";
private static final String USER_NAME_FORMAT = "user:name:%s";

From the docs Redis types are: String , Linked-list , Set , Sorted set . 从文档中,Redis类型为: StringLinked-listSetSorted set I am not sure if there's more. 我不确定是否还有更多。

As for the GAE datastore is concerned a String "Key" and a "Value" have to be the entity that will be stored. 至于GAE数据存储区,必须将字符串“键”和“值”作为要存储的实体。

Like: 喜欢:

public class KeyValue {
 private String key;
 private Value value; // value can be a String, Linked-list, Set or Sorted set etc.
 // Code omitted
}

The justification of this scheme is rooted to the Restful access to the datastore (that is provided by Datanucleus-api-rest) 此方案的理由源于对数据存储的Restful访问(由Datanucleus-api-rest提供)

Using this rest api, to persist a object or entity: 使用此rest api来持久化对象或实体:

POST http://datanucleus.appspot.com/dn/guestbook.Greeting
{"author":null,
  "class":"guestbook.Greeting",
  "content":"test insert",
  "date":1239213923232}

The problem with this approach is that in order to persist a Entity the actual class needs to be defined at compile time; 这种方法的问题在于,为了持久化Entity,需要在编译时定义实际的类; unlike with the idea of having a key-value store mechanism we can simplify the method call: 与具有键值存储机制的想法不同,我们可以简化方法调用:

POST http://datanucleus.appspot.com/dn/org.myframework.KeyValue
{ "class":"org.myframework.KeyValue"
  "key":"user:id:johnsmith;followers",
  "value":"the_list",
}

Passing a single string as "value" is fairly easy, I can use JSON array for list, set or sorted list. 将单个字符串作为“值”传递是相当容易的,我可以将JSON数组用于列表,设置或排序列表。 The real question would be how to actually persist different types of data passed into the interface. 真正的问题是如何真正持久化传递给接口的不同类型的数据。 Should there be multiple KeyValue entities each representing the basic types it support: KeyValueString? 是否应该有多个KeyValue实体,每个实体代表其支持的基本类型:KeyValueString? KeyValueList? KeyValueList? etc. 等等

看起来您正在使用基于JSON的REST API,那么为什么不只将Value存储为JSON字符串呢?

You do not need to use the Datanucleus layer, or any of the other fine ORM layers (like Twig or Objectify). 您无需使用Datanucleus层或任何其他精细的ORM层(例如Twig或Objectify)。 Those are optional, and are all based on the low-level API. 这些是可选的,并且都基于低级API。 If I interpret what you are saying properly, perhaps it already has the functionality that you want. 如果我能正确解释您的意思,也许它已经具有您想要的功能。 See: https://developers.google.com/appengine/docs/java/datastore/entities 请参阅: https : //developers.google.com/appengine/docs/java/datastore/entities

Datanucleus is a specific framework that runs on top of GAE. Datanucleus是在GAE之上运行的特定框架。 You can however access the database at a lower, less structured, more key/value-like level - the low-level API. 但是,您可以在较低的,结构较少的,更像键/值的级别(低级API)中访问数据库。 That's the lowest level you can access directly. 这是您可以直接访问的最低级别。 BTW, the low-level-"GAE datastore" internally runs on 6 global Google Megastore tables, which in turn are hosted on the Google Big Table database system. 顺便说一句,底层的“ GAE数据存储”在内部运行在6个全局Google Megastore表上,这些表又托管在Google Big Table数据库系统上。 Saving JSON as a String works fine. 将JSON保存为字符串可以正常工作。 But you will need ways to retrieve your objects other than by ID. 但是,除了ID之外,您还需要其他方法来检索对象。 That is, you need a way to index your data to support any kind of useful query on it. 也就是说,您需要一种对数据进行索引的方法,以支持对其进行任何有用的查询。

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

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