要使用Google Apps Script修改BigQuery数据,您需要执行以下步骤:
function getService() {
var jsonKey = JSON.parse(PropertiesService.getScriptProperties().getProperty('jsonKey'));
var privateKey = jsonKey.private_key;
var serviceAccountEmail = jsonKey.client_email;
var config = {
'type': 'service_account',
'project_id': jsonKey.project_id,
'private_key_id': jsonKey.private_key_id,
'private_key': privateKey,
'client_email': serviceAccountEmail,
'client_id': jsonKey.client_id,
'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
'token_uri': 'https://oauth2.googleapis.com/token',
'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
'client_x509_cert_url': jsonKey.client_x509_cert_url
};
var service = OAuth2.createService('BigQuery')
.setAuthorizationBaseUrl(config.auth_uri)
.setTokenUrl(config.token_uri)
.setClientId(config.client_id)
.setClientSecret(null)
.setScope('https://www.googleapis.com/auth/bigquery')
.setPrivateKey(privateKey)
.setIssuer(serviceAccountEmail)
.setSubject(null)
.setPropertyStore(PropertiesService.getScriptProperties());
if (!service.hasAccess()) {
Logger.log(service.getAuthorizationUrl());
return null;
}
return service;
}
function updateBigQueryData() {
var service = getService();
if (!service) {
Logger.log('Authentication failed.');
return;
}
var projectId = 'your-project-id';
var datasetId = 'your-dataset-id';
var tableId = 'your-table-id';
var sqlQuery = 'UPDATE `' + projectId + '.' + datasetId + '.' + tableId + '` SET column1 = "newValue" WHERE condition';
var request = {
'query': sqlQuery,
'useLegacySql': false
};
var response = BigQuery.Jobs.query(request, projectId);
Logger.log('Query executed successfully: ' + JSON.stringify(response));
}
请确保将your-project-id
、your-dataset-id
和your-table-id
替换为您的实际项目、数据集和表ID。同时,根据您的需求修改SQL查询。
updateBigNameQueryData
函数以执行查询并修改BigQuery数据。领取专属 10元无门槛券
手把手带您无忧上云