|
22 | 22 | import com.google.cloud.ServiceOptions;
|
23 | 23 | import com.google.cloud.asset.v1.AssetServiceClient;
|
24 | 24 | import com.google.cloud.asset.v1.BigQueryDestination;
|
| 25 | +import com.google.cloud.asset.v1.ContentType; |
25 | 26 | import com.google.cloud.asset.v1.ExportAssetsRequest;
|
26 | 27 | import com.google.cloud.asset.v1.ExportAssetsResponse;
|
27 | 28 | import com.google.cloud.asset.v1.OutputConfig;
|
| 29 | +import com.google.cloud.asset.v1.PartitionSpec; |
28 | 30 | import com.google.cloud.asset.v1.ProjectName;
|
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ExecutionException; |
29 | 33 |
|
30 | 34 | public class ExportAssetsBigqueryExample {
|
31 | 35 |
|
32 | 36 | // Use the default project Id.
|
33 | 37 | private static final String projectId = ServiceOptions.getDefaultProjectId();
|
34 | 38 |
|
35 |
| - // Export assets for a project. |
36 |
| - // @param args path where the results will be exported to. |
37 |
| - public static void exportBigQuery(String bigqueryDataset, String bigqueryTable) throws Exception { |
| 39 | + // Export assets to BigQuery for a project. |
| 40 | + public static void exportBigQuery( |
| 41 | + String bigqueryDataset, String bigqueryTable, ContentType contentType, boolean isPerType) |
| 42 | + throws IOException, IllegalArgumentException, InterruptedException, ExecutionException { |
38 | 43 | try (AssetServiceClient client = AssetServiceClient.create()) {
|
39 | 44 | ProjectName parent = ProjectName.of(projectId);
|
40 |
| - OutputConfig outputConfig = |
41 |
| - OutputConfig.newBuilder() |
42 |
| - .setBigqueryDestination( |
43 |
| - BigQueryDestination.newBuilder() |
44 |
| - .setDataset(bigqueryDataset) |
45 |
| - .setTable(bigqueryTable) |
46 |
| - .setForce(true) |
47 |
| - .build()) |
48 |
| - .build(); |
| 45 | + OutputConfig outputConfig; |
| 46 | + // Outputs to per-type BigQuery table. |
| 47 | + if (isPerType) { |
| 48 | + outputConfig = |
| 49 | + OutputConfig.newBuilder() |
| 50 | + .setBigqueryDestination( |
| 51 | + BigQueryDestination.newBuilder() |
| 52 | + .setDataset(bigqueryDataset) |
| 53 | + .setTable(bigqueryTable) |
| 54 | + .setForce(true) |
| 55 | + .setSeparateTablesPerAssetType(true) |
| 56 | + .setPartitionSpec( |
| 57 | + PartitionSpec.newBuilder() |
| 58 | + .setPartitionKey(PartitionSpec.PartitionKey.READ_TIME) |
| 59 | + .build()) |
| 60 | + .build()) |
| 61 | + .build(); |
| 62 | + } else { |
| 63 | + outputConfig = |
| 64 | + OutputConfig.newBuilder() |
| 65 | + .setBigqueryDestination( |
| 66 | + BigQueryDestination.newBuilder() |
| 67 | + .setDataset(bigqueryDataset) |
| 68 | + .setTable(bigqueryTable) |
| 69 | + .setForce(true) |
| 70 | + .build()) |
| 71 | + .build(); |
| 72 | + } |
49 | 73 | ExportAssetsRequest request =
|
50 | 74 | ExportAssetsRequest.newBuilder()
|
51 | 75 | .setParent(parent.toString())
|
| 76 | + .setContentType(contentType) |
52 | 77 | .setOutputConfig(outputConfig)
|
53 | 78 | .build();
|
54 | 79 | ExportAssetsResponse response = client.exportAssetsAsync(request).get();
|
|
0 commit comments