创建转移作业配置,并在转移作业运行成功或失败时向 Cloud Pub/Sub 发送通知。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
Java
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest;
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
import com.google.cloud.bigquery.datatransfer.v1.ProjectName;
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
// Sample to get run notification
public class RunNotification {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
final String projectId = "MY_PROJECT_ID";
final String datasetId = "MY_DATASET_ID";
final String pubsubTopicName = "MY_TOPIC_NAME";
final String query =
"SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, "
+ "@run_date as intended_run_date, 17 as some_integer";
Map<String, Value> params = new HashMap<>();
params.put("query", Value.newBuilder().setStringValue(query).build());
params.put(
"destination_table_name_template",
Value.newBuilder().setStringValue("my_destination_table_{run_date}").build());
params.put("write_disposition", Value.newBuilder().setStringValue("WRITE_TRUNCATE").build());
params.put("partitioning_field", Value.newBuilder().build());
TransferConfig transferConfig =
TransferConfig.newBuilder()
.setDestinationDatasetId(datasetId)
.setDisplayName("Your Scheduled Query Name")
.setDataSourceId("scheduled_query")
.setParams(Struct.newBuilder().putAllFields(params).build())
.setSchedule("every 24 hours")
.setNotificationPubsubTopic(pubsubTopicName)
.build();
runNotification(projectId, transferConfig);
}
public static void runNotification(String projectId, TransferConfig transferConfig)
throws IOException {
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
ProjectName parent = ProjectName.of(projectId);
CreateTransferConfigRequest request =
CreateTransferConfigRequest.newBuilder()
.setParent(parent.toString())
.setTransferConfig(transferConfig)
.build();
TransferConfig config = dataTransferServiceClient.createTransferConfig(request);
System.out.println(
"\nScheduled query with run notification created successfully :" + config.getName());
} catch (ApiException ex) {
System.out.print("\nScheduled query with run notification was not created." + ex.toString());
}
}
}