如何使用Java中的新API迁移Fusion Tables SQL API

时间:2012-10-30 10:50:15

标签: java google-fusion-tables

我在Java语言中使用Fusion Table SQL API,而Application在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"));

      }
    }

0 个答案:

没有答案