Skip to content

Commit 09a0753

Browse files
authored
docs: Add create Azure Blob Storage transfer code example (GoogleCloudPlatform#8885)
* docs: Add create Azure Blob Storage transfer code example * Address code review comments * Address more comments * Make AzureBlobStorage transfer test to manage test resources in setup/teardown * Rename Env vars because they are global across all tests * Add "DTS_" prefix to test env vars to avoid collisions * Update header to 2024
1 parent d89c606 commit 09a0753

3 files changed

Lines changed: 231 additions & 4 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquerydatatransfer;
18+
19+
// [START bigquerydatatransfer_create_azureblobstorage_transfer]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest;
23+
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
24+
import com.google.cloud.bigquery.datatransfer.v1.ProjectName;
25+
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
26+
import com.google.protobuf.Struct;
27+
import com.google.protobuf.Value;
28+
import java.io.IOException;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
// Sample to create azure blob storage transfer config.
33+
public class CreateAzureBlobStorageTransfer {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
final String projectId = "MY_PROJECT_ID";
38+
final String displayName = "MY_TRANSFER_DISPLAY_NAME";
39+
final String datasetId = "MY_DATASET_ID";
40+
String tableId = "MY_TABLE_ID";
41+
String storageAccount = "MY_AZURE_STORAGE_ACCOUNT_NAME";
42+
String containerName = "MY_AZURE_CONTAINER_NAME";
43+
String dataPath = "MY_AZURE_FILE_NAME_OR_PREFIX";
44+
String sasToken = "MY_AZURE_SAS_TOKEN";
45+
String fileFormat = "CSV";
46+
String fieldDelimiter = ",";
47+
String skipLeadingRows = "1";
48+
Map<String, Value> params = new HashMap<>();
49+
params.put(
50+
"destination_table_name_template", Value.newBuilder().setStringValue(tableId).build());
51+
params.put("storage_account", Value.newBuilder().setStringValue(storageAccount).build());
52+
params.put("container", Value.newBuilder().setStringValue(containerName).build());
53+
params.put("data_path", Value.newBuilder().setStringValue(dataPath).build());
54+
params.put("sas_token", Value.newBuilder().setStringValue(sasToken).build());
55+
params.put("file_format", Value.newBuilder().setStringValue(fileFormat).build());
56+
params.put("field_delimiter", Value.newBuilder().setStringValue(fieldDelimiter).build());
57+
params.put("skip_leading_rows", Value.newBuilder().setStringValue(skipLeadingRows).build());
58+
createAzureBlobStorageTransfer(projectId, displayName, datasetId, params);
59+
}
60+
61+
public static void createAzureBlobStorageTransfer(
62+
String projectId, String displayName, String datasetId, Map<String, Value> params)
63+
throws IOException {
64+
TransferConfig transferConfig =
65+
TransferConfig.newBuilder()
66+
.setDestinationDatasetId(datasetId)
67+
.setDisplayName(displayName)
68+
.setDataSourceId("azure_blob_storage")
69+
.setParams(Struct.newBuilder().putAllFields(params).build())
70+
.setSchedule("every 24 hours")
71+
.build();
72+
// Initialize client that will be used to send requests. This client only needs to be created
73+
// once, and can be reused for multiple requests.
74+
try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
75+
ProjectName parent = ProjectName.of(projectId);
76+
CreateTransferConfigRequest request =
77+
CreateTransferConfigRequest.newBuilder()
78+
.setParent(parent.toString())
79+
.setTransferConfig(transferConfig)
80+
.build();
81+
TransferConfig config = client.createTransferConfig(request);
82+
System.out.println("Azure Blob Storage transfer created successfully: " + config.getName());
83+
} catch (ApiException ex) {
84+
System.out.print("Azure Blob Storage transfer was not created." + ex.toString());
85+
}
86+
}
87+
}
88+
// [END bigquerydatatransfer_create_azureblobstorage_transfer]

bigquery/bigquerydatatransfer/snippets/src/test/java/com/example/bigquerydatatransfer/CreateAmazonS3TransferIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.example.bigquerydatatransfer;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static junit.framework.TestCase.assertNotNull;
20+
import static com.google.common.truth.Truth.assertWithMessage;
2121

2222
import com.google.cloud.bigquery.BigQuery;
2323
import com.google.cloud.bigquery.BigQueryOptions;
@@ -65,9 +65,9 @@ public class CreateAmazonS3TransferIT {
6565

6666
private static String requireEnvVar(String varName) {
6767
String value = System.getenv(varName);
68-
assertNotNull(
69-
"Environment variable " + varName + " is required to perform these tests.",
70-
System.getenv(varName));
68+
assertWithMessage("Environment variable %s is required to perform these tests.", varName)
69+
.that(value)
70+
.isNotEmpty();
7171
return value;
7272
}
7373

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquerydatatransfer;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
21+
22+
import com.google.cloud.bigquery.BigQuery;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
import com.google.cloud.bigquery.Field;
26+
import com.google.cloud.bigquery.Schema;
27+
import com.google.cloud.bigquery.StandardSQLTypeName;
28+
import com.google.cloud.bigquery.StandardTableDefinition;
29+
import com.google.cloud.bigquery.TableDefinition;
30+
import com.google.cloud.bigquery.TableId;
31+
import com.google.cloud.bigquery.TableInfo;
32+
import com.google.protobuf.Value;
33+
import java.io.ByteArrayOutputStream;
34+
import java.io.IOException;
35+
import java.io.PrintStream;
36+
import java.util.HashMap;
37+
import java.util.Map;
38+
import java.util.UUID;
39+
import java.util.logging.Level;
40+
import java.util.logging.Logger;
41+
import org.junit.After;
42+
import org.junit.Before;
43+
import org.junit.BeforeClass;
44+
import org.junit.Test;
45+
46+
public class CreateAzureBlobStorageTransferIT {
47+
48+
private static final Logger LOG =
49+
Logger.getLogger(CreateAzureBlobStorageTransferIT.class.getName());
50+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
51+
private BigQuery bigquery;
52+
private String name;
53+
private String displayName;
54+
private String datasetName;
55+
private String tableName;
56+
private ByteArrayOutputStream bout;
57+
private PrintStream out;
58+
private PrintStream originalPrintStream;
59+
60+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
61+
private static final String DTS_AZURE_STORAGE_ACCOUNT =
62+
requireEnvVar("DTS_AZURE_STORAGE_ACCOUNT");
63+
private static final String DTS_AZURE_BLOB_CONTAINER = requireEnvVar("DTS_AZURE_BLOB_CONTAINER");
64+
private static final String DTS_AZURE_SAS_TOKEN = requireEnvVar("DTS_AZURE_SAS_TOKEN");
65+
66+
private static String requireEnvVar(String varName) {
67+
String value = System.getenv(varName);
68+
assertWithMessage("Environment variable %s is required to perform these tests.", varName)
69+
.that(value)
70+
.isNotEmpty();
71+
return value;
72+
}
73+
74+
@BeforeClass
75+
public static void checkRequirements() {
76+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
77+
requireEnvVar("DTS_AZURE_STORAGE_ACCOUNT");
78+
requireEnvVar("DTS_AZURE_BLOB_CONTAINER");
79+
requireEnvVar("DTS_AZURE_SAS_TOKEN");
80+
}
81+
82+
@Before
83+
public void setUp() {
84+
displayName = "MY_TRANSFER_NAME_TEST_" + ID;
85+
datasetName = "MY_DATASET_NAME_TEST_" + ID;
86+
tableName = "MY_TABLE_NAME_TEST_" + ID;
87+
// create a temporary dataset
88+
bigquery = BigQueryOptions.getDefaultInstance().getService();
89+
bigquery.create(DatasetInfo.of(datasetName));
90+
// create a temporary table
91+
Schema schema =
92+
Schema.of(
93+
Field.of("name", StandardSQLTypeName.STRING),
94+
Field.of("post_abbr", StandardSQLTypeName.STRING));
95+
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
96+
TableInfo tableInfo = TableInfo.of(TableId.of(datasetName, tableName), tableDefinition);
97+
bigquery.create(tableInfo);
98+
99+
bout = new ByteArrayOutputStream();
100+
out = new PrintStream(bout);
101+
originalPrintStream = System.out;
102+
System.setOut(out);
103+
}
104+
105+
@After
106+
public void tearDown() throws IOException {
107+
// Clean up
108+
DeleteScheduledQuery.deleteScheduledQuery(name);
109+
// delete a temporary table
110+
bigquery.delete(TableId.of(datasetName, tableName));
111+
// delete a temporary dataset
112+
bigquery.delete(datasetName, BigQuery.DatasetDeleteOption.deleteContents());
113+
// restores print statements in the original method
114+
System.out.flush();
115+
System.setOut(originalPrintStream);
116+
LOG.log(Level.INFO, bout.toString());
117+
}
118+
119+
@Test
120+
public void testCreateAzureBlobStorageTransfer() throws IOException {
121+
Map<String, Value> params = new HashMap<>();
122+
params.put(
123+
"destination_table_name_template", Value.newBuilder().setStringValue(tableName).build());
124+
params.put(
125+
"storage_account", Value.newBuilder().setStringValue(DTS_AZURE_STORAGE_ACCOUNT).build());
126+
params.put("container", Value.newBuilder().setStringValue(DTS_AZURE_BLOB_CONTAINER).build());
127+
params.put("data_path", Value.newBuilder().setStringValue("*").build());
128+
params.put("sas_token", Value.newBuilder().setStringValue(DTS_AZURE_SAS_TOKEN).build());
129+
params.put("file_format", Value.newBuilder().setStringValue("CSV").build());
130+
params.put("field_delimiter", Value.newBuilder().setStringValue(",").build());
131+
params.put("skip_leading_rows", Value.newBuilder().setStringValue("1").build());
132+
133+
CreateAzureBlobStorageTransfer.createAzureBlobStorageTransfer(
134+
PROJECT_ID, displayName, datasetName, params);
135+
String result = bout.toString();
136+
name = result.substring(result.indexOf(":") + 1, result.length() - 1);
137+
assertThat(result).contains("Azure Blob Storage transfer created successfully: ");
138+
}
139+
}

0 commit comments

Comments
 (0)