Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): add omni create external table #823

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions samples/install-without-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigqueryconnection</artifactId>
<version>0.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions samples/snapshot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigqueryconnection</artifactId>
<version>0.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions samples/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigqueryconnection</artifactId>
<version>0.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ public class CreateDatasetAws {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
// Note: As of now location only supports aws-us-east-1
String location = "aws-us-east-1";
createDatasetAws(datasetName, location);
createDatasetAws(projectId, datasetName, location);
}

public static void createDatasetAws(String datasetName, String location) {
public static void createDatasetAws(String projectId, String datasetName, String location) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).setLocation(location).build();
DatasetInfo datasetInfo =
DatasetInfo.newBuilder(projectId, datasetName).setLocation(location).build();

Dataset dataset = bigquery.create(datasetInfo);
System.out.println(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.CsvOptions;
import com.google.cloud.bigquery.ExternalTableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;

public class CreateExternalTableAws {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
// Create a aws connection
// projects/{project_id}/locations/{location_id}/connections/{connection_id}
String connectionId = "MY_CONNECTION_NAME";
String sourceUri = "s3://your-bucket-name/";
CsvOptions options = CsvOptions.newBuilder().setSkipLeadingRows(1).build();
ExternalTableDefinition externalTableDefinition =
ExternalTableDefinition.newBuilder(sourceUri, options)
.setConnectionId(connectionId)
.build();
createExternalTableAws(datasetName, tableName, externalTableDefinition);
}

public static void createExternalTableAws(
String datasetName, String tableName, ExternalTableDefinition externalTableDefinition) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

TableId tableId = TableId.of(datasetName, tableName);
TableInfo tableInfo = TableInfo.newBuilder(tableId, externalTableDefinition).build();

bigquery.create(tableInfo);
System.out.println("Aws external table created successfully");
} catch (BigQueryException e) {
System.out.println("Aws external was not created." + e.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CreateDatasetAwsIT {
private PrintStream out;
private PrintStream originalPrintStream;

private static final String GOOGLE_CLOUD_PROJECT = requireEnvVar("OMNI_PROJECT_ID");
private static final String PROJECT_ID = requireEnvVar("OMNI_PROJECT_ID");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
Expand All @@ -66,7 +66,7 @@ public void setUp() {
@After
public void tearDown() {
// Clean up
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
Expand All @@ -75,7 +75,7 @@ public void tearDown() {

@Test
public void testCreateDatasetAws() {
CreateDatasetAws.createDatasetAws(datasetName, LOCATION);
CreateDatasetAws.createDatasetAws(PROJECT_ID, datasetName, LOCATION);
assertThat(bout.toString()).contains("Aws dataset created successfully :");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.cloud.bigquery.CsvOptions;
import com.google.cloud.bigquery.ExternalTableDefinition;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.connection.v1.AwsCrossAccountRole;
import com.google.cloud.bigquery.connection.v1.AwsProperties;
import com.google.cloud.bigquery.connection.v1.Connection;
import com.google.cloud.bigquery.connection.v1.CreateConnectionRequest;
import com.google.cloud.bigquery.connection.v1.DeleteConnectionRequest;
import com.google.cloud.bigquery.connection.v1.LocationName;
import com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class CreateExternalTableAwsIT {

private static final String ID = UUID.randomUUID().toString().substring(0, 8);
private static final String LOCATION = "aws-us-east-1";
private final Logger log = Logger.getLogger(this.getClass().getName());
private String datasetName;
private String tableName;
private String connectionName;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String PROJECT_ID = requireEnvVar("OMNI_PROJECT_ID");
private static final String AWS_ACCOUNT_ID = requireEnvVar("AWS_ACCOUNT_ID");
private static final String AWS_ROLE_ID = requireEnvVar("AWS_ROLE_ID");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
assertNotNull(
"Environment variable " + varName + " is required to perform these tests.",
System.getenv(varName));
return value;
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("OMNI_PROJECT_ID");
requireEnvVar("AWS_ACCOUNT_ID");
requireEnvVar("AWS_ROLE_ID");
}

@Before
public void setUp() throws IOException {
datasetName = "CREATE_EXTERNAL_TABLE_AWS_TEST_" + ID;
tableName = "CREATE_EXTERNAL_TABLE_AWS_TEST_" + ID;
connectionName = "CREATE_EXTERNAL_TABLE_AWS_TEST_" + ID;
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
// create a temporary aws connection
try (ConnectionServiceClient client = ConnectionServiceClient.create()) {
LocationName parent = LocationName.of(PROJECT_ID, LOCATION);
String iamRoleId = String.format("arn:aws:iam::%s:role/%s", AWS_ACCOUNT_ID, AWS_ROLE_ID);
AwsCrossAccountRole role = AwsCrossAccountRole.newBuilder().setIamRoleId(iamRoleId).build();
AwsProperties awsProperties = AwsProperties.newBuilder().setCrossAccountRole(role).build();
Connection connection = Connection.newBuilder().setAws(awsProperties).build();
CreateConnectionRequest request =
CreateConnectionRequest.newBuilder()
.setParent(parent.toString())
.setConnection(connection)
.setConnectionId(connectionName)
.build();
connectionName = client.createConnection(request).getName();
}
// create a temporary dataset
CreateDatasetAws.createDatasetAws(PROJECT_ID, datasetName, LOCATION);
}

@After
public void tearDown() throws IOException {
// delete a temporary aws connection
try (ConnectionServiceClient client = ConnectionServiceClient.create()) {
DeleteConnectionRequest request =
DeleteConnectionRequest.newBuilder().setName(connectionName).build();
client.deleteConnection(request);
}
// Clean up
DeleteTable.deleteTable(datasetName, tableName);
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, bout.toString());
}

@Test
public void testCreateExternalTableAws() {
String sourceUri = "s3://cloud-samples-tests/us-states.csv";
Schema schema =
Schema.of(
Field.of("name", StandardSQLTypeName.STRING),
Field.of("post_abbr", StandardSQLTypeName.STRING));
CsvOptions options = CsvOptions.newBuilder().setSkipLeadingRows(1).build();
ExternalTableDefinition externalTable =
ExternalTableDefinition.newBuilder(sourceUri, options)
.setConnectionId(connectionName)
.setSchema(schema)
.build();
CreateExternalTableAws.createExternalTableAws(datasetName, tableName, externalTable);
assertThat(bout.toString()).contains("Aws external table created successfully");
}
}