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

feat: add samples #44

Merged
merged 6 commits into from Dec 18, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions samples/src/main/java/com/example/bigquery/CreateTable.java
@@ -0,0 +1,46 @@
package com.example.bigquery;

// [START bigquery_create_table]
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;

public class CreateTable {

public static void runCreateTable() {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "my-dataset-name";
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
String tableName = "my_table_name";
Schema schema =
Schema.of(
// LegacySQLTypeName will be updated to StandardSQLTypeName once release rolls out
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
Field.of("stringField", LegacySQLTypeName.STRING),
Field.of("booleanField", LegacySQLTypeName.BOOLEAN));
createTable(datasetName, tableName, schema);
}

public static void createTable(String datasetName, String tableName, Schema schema) {
// 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);
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();

try {
bigquery.create(tableInfo);
System.out.println("Table created successfully");
} catch (BigQueryException e) {
System.out.println("Table was not created. \n" + e.toString());
}
}
}
// [END bigquery_create_table]
29 changes: 29 additions & 0 deletions samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java
@@ -0,0 +1,29 @@
package com.example.bigquery;

// [START bigquery_extract_table]
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.Table;

public class ExtractTableToJSON {

public static void runExtractTableToJSON() {
// TODO(developer): Replace these variables before running the sample.
Table table = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this null? We should be showing the user how to initialize this value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thought is user should be able to pass in a Table object of their choice to extract into GCS as CSV - but I think what you said makes sense too. We can offer an example of what this Table object could be.

String format = "CSV";
String bucketName = "my-bucket";
String gcsFileName = "gs://" + bucketName + "/extractTest.csv";
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
extractTableToJSON(table, format, gcsFileName);
}

// Exports my-dataset-name:my_table to gcs://my-bucket/my-file as raw CSV
public static void extractTableToJSON(Table table, String format, String gcsFileName) {

stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
try {
table.extract(format, gcsFileName);
System.out.println("Table extraction job completed successfully");
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
} catch (BigQueryException e) {
System.out.println("Table extraction job was interrupted. \n" + e.toString());
}
}
}
// [END bigquery_extract_table]
48 changes: 48 additions & 0 deletions samples/src/test/java/com/example/bigquery/CreateTableIT.java
@@ -0,0 +1,48 @@
package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CreateTableIT {
private ByteArrayOutputStream bout;
private PrintStream out;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testCreateTable() {
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();

// Create a new dataset to create a table in
CreateDataset.createDataset(generatedDatasetName);

// Create an empty table with specific schema in the dataset just created
String tableName = "my_table_name";
Schema schema =
Schema.of(
Field.of("stringField", LegacySQLTypeName.STRING),
Field.of("booleanField", LegacySQLTypeName.BOOLEAN));
CreateTable.createTable(generatedDatasetName, tableName, schema);

assertThat(bout.toString()).contains("Table created successfully");
}
}
@@ -0,0 +1,76 @@
package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ExtractTableToJSONIT {
private ByteArrayOutputStream bout;
private PrintStream out;

@Before
public void setUp() throws Exception {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testExtractTableToJSON() {
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();

// Create a new dataset to create a new table in
CreateDataset.createDataset(generatedDatasetName);

// Create a new table to extract to GCS for
String tableName = "my_table_name";
Schema schema =
Schema.of(
Field.of("stringField", LegacySQLTypeName.STRING),
Field.of("booleanField", LegacySQLTypeName.BOOLEAN));
Table table = createTableHelper(generatedDatasetName, tableName, schema);

// Extract table content to GCS in CSV format
ExtractTableToJSON.extractTableToJSON(table, "CSV", "gs://my-bucket/extractTest.csv");
assertThat(bout.toString()).contains("Table extraction job completed successfully");
}

private static Table createTableHelper(String datasetName, String tableName, Schema schema) {
// 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);
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();

try {
Table table = bigquery.create(tableInfo);
return table;
} catch (BigQueryException e) {
System.out.println("Table was not created. \n" + e.toString());
return null;
}
}
}