Java中的数据存储区导出逻辑

时间:2018-11-15 03:26:50

标签: google-app-engine google-cloud-platform google-cloud-datastore database-backups

感谢Google宣布了Cloud Datastore的导出逻辑。我想在我的平台上设置schedule-export。但是,它不是Python,而是Java。因此,我需要使用cron.xml和Java逻辑来设计此逻辑。

在Java中是否有参考设计数据存储导出逻辑(cloud_datastore_admin.py)?特别是,我需要用Java转换这部分

app = webapp2.WSGIApplication(
[
    ('/cloud-datastore-export', Export),
], debug=True)

https://cloud.google.com/datastore/docs/schedule-export

3 个答案:

答案 0 :(得分:2)

您可以通过following these instructions为App Egnine创建骨架。

有了骨架后,添加类似以下内容以处理导出请求:

CloudDatastoreExport.java

package com.google.example.datastore;

import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import com.google.apphosting.api.ApiProxy;
import com.google.common.io.CharStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

@WebServlet(name = "CloudDatastoreExport", value = "/cloud-datastore-export")
public class CloudDatastoreExport extends HttpServlet {

  private static final Logger log = Logger.getLogger(CloudDatastoreExport.class.getName());

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // Verify outputURL parameter
    String outputUrlPrefix = request.getParameter("output_url_prefix");

    if (outputUrlPrefix == null || !outputUrlPrefix.matches("^gs://.*")) {
      response.setStatus(HttpServletResponse.SC_CONFLICT);
      response.setContentType("text/plain");
      response.getWriter().println("Error: Must provide a valid output_url_prefix.");

    } else {

      // Get project ID
      String projectId = ApiProxy.getCurrentEnvironment().getAppId();
      // Remove partition information to get plain app ID
      String appId = projectId.replaceFirst("(.*~)", "");

      // Get access token
      ArrayList<String> scopes = new ArrayList<String>();
      scopes.add("https://www.googleapis.com/auth/datastore");
      final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
      final AppIdentityService.GetAccessTokenResult accessToken =
          appIdentity.getAccessToken(scopes);

      // 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");

      // Build export request
      JSONObject exportRequest = new JSONObject();
      exportRequest.put("output_url_prefix", outputUrlPrefix);

      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);

      URL url = new URL("https://datastore.googleapis.com/v1/projects/" + appId + ":export");
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setDoOutput(true);
      connection.setRequestMethod("POST");
      connection.addRequestProperty("Content-Type", "application/json");
      connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());

      OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
      exportRequest.write(writer);
      writer.close();

      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

        JSONTokener exportResponseTokens = new JSONTokener(connection.getInputStream());
        JSONObject exportResponse = new JSONObject(exportResponseTokens);

        response.setContentType("text/plain");
        response.getWriter().println("Export started:\n" + exportResponse.toString(4));

      } else {
        InputStream s = connection.getErrorStream();
        InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8);
        String errorMessage =
            String.format(
                "got error (%d) response %s from %s",
                connection.getResponseCode(), CharStreams.toString(r), connection.toString());
        log.warning(errorMessage);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.setContentType("text/plain");
        response.getWriter().println("Failed to initiate export.");
      }
    }
  }
}

您可以在文档中使用相同的cron.yaml

cron:
- description: "Daily Cloud Datastore Export"
  url: /cloud-datastore-export?namespace_id=&output_url_prefix=gs://BUCKET_NAME[/NAMESPACE_PATH]
  target: cloud-datastore-admin
  schedule: every 24 hours

使用gcloud部署cron作业:

gcloud app deploy cron.yaml

确保您完成此部分,以授予GAE导出和存储桶权限,否则 您会收到权限被拒绝的错误:

https://cloud.google.com/datastore/docs/schedule-export#before_you_begin

答案 1 :(得分:1)

您显示的代码片段只是特定于第一代标准环境python应用程序的典型GAE应用程序框架的一部分。您可以在python快速入门main.pyHello World code review部分中轻松识别它。

代码初始化function getTimeRemaining(endtime) { var deadline = document.getElementById('myDate').value; var t = Date.parse(endtime).getTime() - (new Date()).getTime(); var seconds = Math.floor( (t/1000) % 60 ); var minutes = Math.floor( (t/1000/60) % 60); var hours = Math.floor( (t/(1000*60*60)) % 24); var days = Math.floor( t/(1000*60*60*24) ); return { 'total': t, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': seconds }; function initializeClock(id, endtime) { var clock = document.getElementById(id); var timeinterval = setInterval(function(){ var t = getTimeRemaining(endtime); clock.innerHTML = 'days: ' + t.days + '<br>' + 'hours: ' + t.hours + '<br>' + 'minutes: ' + t.minutes + '<br>' + 'seconds: ' + t.seconds; if(t.total<=0){ clearInterval(timeinterval); } },1000); } initializeClock('clockdiv', deadline); } 变量(来自app python模块,即main文件),该变量在main.py处理程序配置中被引用为{{1} }。

相应的Java应用程序框架明显不同,请参见Java快速入门Hello World code review。但是不用担心,您不必专门转换该代码段,只需构建Java应用程序框架并关注应用程序处理程序的实际工作-向数据存储区发出这些app.yaml请求。抱歉,我无济于事,但我不是Java用户。

答案 2 :(得分:0)

我真正意识到的是app.yaml就像Java中的Web.xml 而cloud-datastore-export是一个与gae通信以导出数据的servlet,但我不能做更多的事情