Skip to content

Commit 9232370

Browse files
author
Praful Makani
authored
docs(samples): add create table without schema (#488)
1 parent fdea80e commit 9232370

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_create_table_without_schema]
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.Schema;
24+
import com.google.cloud.bigquery.StandardTableDefinition;
25+
import com.google.cloud.bigquery.TableDefinition;
26+
import com.google.cloud.bigquery.TableId;
27+
import com.google.cloud.bigquery.TableInfo;
28+
29+
// Sample to create a table without schema
30+
public class CreateTableWithoutSchema {
31+
32+
public static void runCreateTableWithoutSchema() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String datasetName = "MY_DATASET_NAME";
35+
String tableName = "MY_TABLE_NAME";
36+
createTableWithoutSchema(datasetName, tableName);
37+
}
38+
39+
public static void createTableWithoutSchema(String datasetName, String tableName) {
40+
try {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
44+
45+
TableId tableId = TableId.of(datasetName, tableName);
46+
TableDefinition tableDefinition = StandardTableDefinition.of(Schema.of());
47+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
48+
49+
bigquery.create(tableInfo);
50+
System.out.println("Table created successfully");
51+
} catch (BigQueryException e) {
52+
System.out.println("Table was not created. \n" + e.toString());
53+
}
54+
}
55+
}
56+
// [END bigquery_create_table_without_schema]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class CreateTableWithoutSchemaIT {
31+
32+
private String tableName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
tableName = "MY_TABLE_TEST" + UUID.randomUUID().toString().replace('-', '_');
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
// Clean up
62+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
63+
System.setOut(null);
64+
}
65+
66+
@Test
67+
public void testCreateTableWithoutSchema() {
68+
CreateTableWithoutSchema.createTableWithoutSchema(BIGQUERY_DATASET_NAME, tableName);
69+
assertThat(bout.toString()).contains("Table created successfully");
70+
}
71+
}

0 commit comments

Comments
 (0)