Write Cron Job in Java on GAE to Run BigQuery

2019-07-18 15:19发布

I want to run a cron job on GAE that internally calls BigQuery.

I am currently able to run BigQuery but I need to log in with my credentials. But I would like to run the cron job for BigQuery without any login.

Any help would be highly appreciated.

2条回答
倾城 Initia
2楼-- · 2019-07-18 15:55

I am successfully able to implement it in java.Before performing it we need to generate client id for service account.

private static final HttpTransport TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static Bigquery bigquery;




AppIdentityCredential credential = new AppIdentityCredential(Collections.singleton(BigqueryScopes.BIGQUERY));
bigquery = new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName("BigQuery-Service-Accounts/0.1")
            .setHttpRequestInitializer(credential).build();
查看更多
爷的心禁止访问
3楼-- · 2019-07-18 16:12

I know it's not the java you're expecting. The secret is to use AppAssertionCredentials. Here is the python sample:

from apiclient.discovery import build
from oauth2client.appengine import AppAssertionCredentials
import httplib2
from google.appengine.api import memcache

scope = 'https://www.googleapis.com/auth/bigquery'

credentials = AppAssertionCredentials(scope=scope)

http = credentials.authorize(httplib2.Http(memcache))

return build("bigquery", "v2", http=http)
查看更多
登录 后发表回答