Skip to content

Commit 477a8e3

Browse files
author
Praful Makani
authored
docs(samples): add omni create aws dataset (#821)
1 parent 43cf389 commit 477a8e3

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2020 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.bigquery;
18+
19+
// [START bigquery_omni_create_dataset]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Dataset;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
26+
// Sample to create a aws dataset
27+
public class CreateDatasetAws {
28+
29+
public static void main(String[] args) {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
// Note: As of now location only supports aws-us-east-1
33+
String location = "aws-us-east-1";
34+
createDatasetAws(datasetName, location);
35+
}
36+
37+
public static void createDatasetAws(String datasetName, String location) {
38+
try {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests.
41+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
42+
43+
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).setLocation(location).build();
44+
45+
Dataset dataset = bigquery.create(datasetInfo);
46+
System.out.println(
47+
"Aws dataset created successfully :" + dataset.getDatasetId().getDataset());
48+
} catch (BigQueryException e) {
49+
System.out.println("Aws dataset was not created. \n" + e.toString());
50+
}
51+
}
52+
}
53+
// [END bigquery_omni_create_dataset]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2020 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.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class CreateDatasetAwsIT {
33+
34+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
35+
private static final String LOCATION = "aws-us-east-1";
36+
private final Logger log = Logger.getLogger(this.getClass().getName());
37+
private String datasetName;
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
private PrintStream originalPrintStream;
41+
42+
private static final String GOOGLE_CLOUD_PROJECT = requireEnvVar("OMNI_PROJECT_ID");
43+
44+
private static String requireEnvVar(String varName) {
45+
String value = System.getenv(varName);
46+
assertNotNull(
47+
"Environment variable " + varName + " is required to perform these tests.",
48+
System.getenv(varName));
49+
return value;
50+
}
51+
52+
@BeforeClass
53+
public static void checkRequirements() {
54+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
55+
}
56+
57+
@Before
58+
public void setUp() {
59+
datasetName = "CREATE_DATASET_AWS_TEST_" + ID;
60+
bout = new ByteArrayOutputStream();
61+
out = new PrintStream(bout);
62+
originalPrintStream = System.out;
63+
System.setOut(out);
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
// Clean up
69+
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
70+
// restores print statements in the original method
71+
System.out.flush();
72+
System.setOut(originalPrintStream);
73+
log.log(Level.INFO, bout.toString());
74+
}
75+
76+
@Test
77+
public void testCreateDatasetAws() {
78+
CreateDatasetAws.createDatasetAws(datasetName, LOCATION);
79+
assertThat(bout.toString()).contains("Aws dataset created successfully :");
80+
}
81+
}

0 commit comments

Comments
 (0)