繁体   English   中英

GAE-JAVA-JDO如何构建解码的实体密钥面包屑

[英]GAE-JAVA-JDO How to build a decoded entity key breadcrumb

我想知道他们是如何在数据存储区查看器的编辑页面中完成的,任何帮助都会非常感激。 看起来很简单,但无法弄清楚。 这是一个截图,准确显示我的意思。 解码的实体密钥

Key类有一个kind(名称或id),还有一个parent ,它将是null或另一个键。

从实体的键开始,您可以打印种类和ID,然后查找父项,打印其类型和ID,然后查找其父项,打印种类和ID等。

请参阅https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Key

字符串编码的Key确实包含其自身及其所有祖先的种类和ID。 在父字段上循环服务器端没有问题,直到它为空(它基本上是字符串+对象操作,不涉及对数据存储区的查询),以创建面包屑。

我不知道它是否在JS中已经完成了客​​户端,但它应该是可能的,因为它基本上是一个base64编码。 有关算法,请参阅https://github.com/golang/appengine/blob/master/datastore/key.go中的函数Encode()

此在线工具对密钥进行解码和编码: http//datastore-key.appspot.com 它也可以作为具有JSON输出的服务。 Go代码服务器端不会发出数据存储区查询。

Java答案可能是这样的:

https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/KeyFactory#stringToKey(java.lang.String)

这为您提供了一个编码表示的Key ,然后您可以查询kind,namespace,id,parent和appId,它允许类似的处理,而不是上面评论中提到的python答案。

作为示例,以下代码片段重新映射具有一个父级并且最初来自另一个GAE应用程序的密钥:

  String key = "sflkjsadfliasjflkhsa"; // replace this by a real key

  // a not very elegant way to get the current app id
  String appId = KeyFactory.createKey("dummy", 1).getAppId();

  // here, the key is converted from an encoded String to a key object
  Key keyObj = KeyFactory.stringToKey(key);
  String oldAppId = keyObj.getAppId();

  // if the app id is different, we have to convert the key
  if (!appId.equals(oldAppId)) {
      // creeate a new key with parent having the correct app id
      Key parentKey = keyObj.getParent();
      Key newParentKey = KeyFactory.createKey(parentKey.getKind(), parentKey.getId());
      Key newKeyObj = KeyFactory.createKey(newParentKey, keyObj.getKind(), keyObj.getId());

      // convert the key back to a String replacing the original one
      String newKey = KeyFactory.keyToString(newKeyObj);

      // replace this by a call to your logger
      Log.warn(getClass(), "remapped key: appId: " + oldAppId + " -> " + appId + " oldKey: " + key + " -> " + newKey);

      key = newKey;
  }

暂无
暂无

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

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