2

我在 Java 语言中使用 Fusion Table SQL API,应用程序托管在 Google App 引擎中。应用程序正在通过 Java 语言在融合表中添加和删除记录。但是 Fusion Tables SQL API 已于 2012 年 6 月 26 日正式弃用,并将于 2012 年 12 月 26 日关闭。因此,我必须使用新 API 迁移 Fusion Tables SQL API。

我的问题是如何使用 Java 中的新 API 迁移 Fusion Tables SQL API?

我当前的脚本是

import com.google.gdata.client.ClientLoginAccountType;
    import com.google.gdata.client.GoogleService;
    import com.google.gdata.client.Service.GDataRequest;
    import com.google.gdata.client.Service.GDataRequest.RequestType;

    import com.google.gdata.util.AuthenticationException;
    import com.google.gdata.util.ContentType;
    import com.google.gdata.util.ServiceException;

    import java.io.IOException;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.Scanner;
    import java.util.regex.MatchResult;
    import java.util.regex.Pattern;

    public class SampleProgram {

      private static final String SERVICE_URL = "https://www.google.com/fusiontables/api/query";

      private static final Pattern CSV_VALUE_PATTERN = Pattern.compile("([^,\\r\\n\"]*|\"(([^\"]*\"\")*[^\"]*)\")(,|\\r?\\n)");

      private GoogleService service;


      public SampleProgram(String email, String password) throws AuthenticationException 
      {
        service = new GoogleService("fusiontables", "fusiontables.ApiExample");
        service.setUserCredentials(email, password, ClientLoginAccountType.GOOGLE);
      }

      public String GetrunSelectQuery(String selectQuery) throws IOException,ServiceException 
      {
          URL url = new URL(SERVICE_URL + "?sql=" + URLEncoder.encode(selectQuery, "UTF-8"));
            GDataRequest request = service.getRequestFactory().getRequest(RequestType.QUERY, url, ContentType.TEXT_PLAIN);

            request.execute();
            String decoded="";
            Scanner scanner = new Scanner(request.getResponseStream(),"UTF-8");
            while (scanner.hasNextLine()) {
              scanner.findWithinHorizon(CSV_VALUE_PATTERN, 0);
              MatchResult match = scanner.match();

              String quotedString = match.group(2);
              decoded = quotedString == null ? match.group(1): quotedString.replaceAll("\"\"", "\"");

            }
            return decoded;
      }



      public static void main(String[] args) throws ServiceException, IOException 
      {

        String tableEncryptedID="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        String sEmailId="XXXX@XXXXX.com";
        String sPassword="XXXXXXXXX";
        SampleProgram obj = new SampleProgram(sEmailId, sPassword);

        System.out.println(obj.GetrunSelectQuery("select name from "+tableEncryptedID+" limit 1"));

      }
    }
4

0 回答 0