You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将非默认Datastore数据库中的Kind导出至Google Cloud Storage

解决Datastore导出非默认数据库Kind的问题

你遇到的问题是因为旧代码未适配Datastore的多数据库支持,要指定目标数据库(对应你说的datastoreId),需要在导出请求的顶层JSON体中添加databaseId字段,具体修改步骤如下:

1. 添加databaseId参数获取逻辑

在读取namespaces和kinds参数的位置,新增获取databaseId的代码:

String[] namespaces = request.getParameterValues("namespace_id");
String[] kinds = request.getParameterValues("kind");
// 新增:获取databaseId参数,默认使用(default)
String databaseId = request.getParameter("database_id");
if (databaseId == null) {
    databaseId = "(default)";
}

2. 在导出请求体中添加databaseId字段

构建exportRequest时,将databaseId加入顶层JSON:

// Build export request
JSONObject exportRequest = new JSONObject();
exportRequest.put("output_url_prefix", outputUrlPrefix);
// 新增:指定要导出的数据库ID
exportRequest.put("databaseId", databaseId);

JSONObject entityFilter = new JSONObject();
// ... 原有代码不变

修改后的核心代码片段

// Read export parameters
// If output prefix does not end with slash, add a timestamp
if (!outputUrlPrefix.substring(outputUrlPrefix.length() - 1).contentEquals("/")) {
    String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    outputUrlPrefix = outputUrlPrefix + "/" + timeStamp + "/";
}

String[] namespaces = request.getParameterValues("namespace_id");
String[] kinds = request.getParameterValues("kind");
// 新增获取databaseId
String databaseId = request.getParameter("database_id");
if (databaseId == null) {
    databaseId = "(default)";
}

// Build export request
JSONObject exportRequest = new JSONObject();
exportRequest.put("output_url_prefix", outputUrlPrefix);
// 新增添加databaseId到请求体
exportRequest.put("databaseId", databaseId);

JSONObject entityFilter = new JSONObject();

if (kinds != null) {
    JSONArray kindsJSON = new JSONArray(kinds);
    entityFilter.put("kinds", kinds);
}

if (namespaces != null) {
    JSONArray namespacesJSON = new JSONArray(namespaces);
    entityFilter.put("namespaceIds", namespacesJSON);
}

exportRequest.put("entityFilter", entityFilter);

补充说明

  • databaseId是Datastore Admin API为多数据库支持新增的字段,默认值为(default),对应你之前导出的默认数据库。
  • 调用时只需在请求参数中传入database_id=你的数据库ID,即可导出对应数据库中的Kind。

内容的提问来源于stack exchange,提问作者Sebastian Filip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 09:20:10