GAEデータストアのKind一覧を取得

EclipseでDatastore Viewer Pluginを作るときなどに使えそうな記事がありました。

404 shin1のつぶやき ないわー Not Found: #appengine java でローカル環境のデータファイルをほげってみる

public class MyKindList {

  static final String GAE_DIR = ".";
  static final String ApplicationID = "myappid";
  static final String VersionID = "1";
  static final String DATASTORE_V3 = "datastore_v3";

  public static void main(String[] args) {

    ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
    ApiProxyLocalImpl proxy = new ApiProxyLocalImpl(new File(GAE_DIR)) {};
    ApiProxy.setDelegate(proxy);

    System.out.println(getKindList());

    proxy.getService(DATASTORE_V3).stop();
    ApiProxy.setDelegate(null);
    ApiProxy.setEnvironmentForCurrentThread(null);

  }

  public static List getKindList() {
    Environment environment = ApiProxy.getCurrentEnvironment();
    ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
    LocalDatastoreService datastoreService =
      (LocalDatastoreService) (proxy).getService(DATASTORE_V3);
    Schema schema = datastoreService
      .getSchema(null, (new DatastorePb.GetSchemaRequest())
      .setApp(environment.getAppId()));
    List entityProtoList = schema.kinds();
    List kindList = new ArrayList(entityProtoList.size());
    for (EntityProto entityProto : entityProtoList) {
      List path = entityProto.getKey().getPath().elements();
      Element element = (Element) path.get(path.size() - 1);
      kindList.add(element.getType());
    }
    return kindList;
  }

}
class TestEnvironment implements ApiProxy.Environment {
  public String getAppId() { return MyKindList.ApplicationID; }
  public String getVersionId() { return MyKindList.VersionID; }
  public void setDefaultNamespace(String s) { }
  public String getRequestNamespace() { return null; }
  public String getDefaultNamespace() { return null; }
  public String getAuthDomain() { return null; }
  public boolean isLoggedIn() { return false; }
  public String getEmail() { return null; }
  public boolean isAdmin() { return false; }
  public Map getAttributes() {
    Map map = new HashMap();
    map.put(NamespaceManager.class.getName()
      + ".default_api_namespace_key", "");
    return map;
  }

}

実行には以下のライブラリにパスを通す必要があります。

appengine-java-sdk-x.x.x/lib/impl/appengine-api-stubs.jar
appengine-java-sdk-x.x.x/lib/impl/appengine-local-runtime.jar

おすすめ

2件のフィードバック

  1. shin1ogawa より:

    GetSchemaはProduction環境では使えないので、Production環境ではDatastoreStatisticsを使う必要があります。
    http://code.google.com/appengine/docs/java/datastore/stats.html
    …とはいえstatisticsはリアルタイムな状況が反映されていない、という問題に注意が必要なんですけどね。

    • petit より:

      コメントありがとうございます。
      shin1ogawaさんの仰るmakeSyncCallと組み合わせれば、local環境とProduction環境、両方のデータベースに対して自由にアクセスできそうです。以下のコードでProduction環境でもkind一覧を取得することができることを確認しました。

      DatastoreService dataservice =
        DatastoreServiceFactory.getDatastoreService();
      
      ArrayList kindlist = new ArrayList();
      
      Query query = new Query("__Stat_Kind__");
      Iterator it = dataservice.prepare(query).asIterator();
      while (it.hasNext()) {
        Entity entity = (Entity) it.next();
        Object property = entity.getProperty("kind_name");
        if(property.toString().indexOf("__")==0){
          continue;
        }else{
          String kindname = property.toString();
          kindlist.add(kindname);
        }
      }
      
      System.out.println(kindlist);

shin1ogawa へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です