@Test public void testCopyJob() throws InterruptedException { String sourceTableName = "test_copy_job_source_table"; String destinationTableName = "test_copy_job_destination_table"; TableId sourceTable = TableId.of(DATASET, sourceTableName); StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); Table createdTable = bigquery.create(tableInfo); assertNotNull(createdTable); assertEquals(DATASET, createdTable.tableId().dataset()); assertEquals(sourceTableName, createdTable.tableId().table()); TableId destinationTable = TableId.of(DATASET, destinationTableName); CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, sourceTable); Job remoteJob = bigquery.create(JobInfo.of(configuration)); while (!remoteJob.isDone()) { Thread.sleep(1000); } assertNull(remoteJob.status().error()); Table remoteTable = bigquery.getTable(DATASET, destinationTableName); assertNotNull(remoteTable); assertEquals(destinationTable.dataset(), remoteTable.tableId().dataset()); assertEquals(destinationTableName, remoteTable.tableId().table()); assertEquals(TABLE_SCHEMA, remoteTable.definition().schema()); assertTrue(createdTable.delete()); assertTrue(remoteTable.delete()); }
@BeforeClass public static void beforeClass() throws InterruptedException { RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create(); RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(); bigquery = bigqueryHelper.options().service(); storage = gcsHelper.options().service(); storage.create(BucketInfo.of(BUCKET)); storage.create( BlobInfo.builder(BUCKET, LOAD_FILE).contentType("text/plain").build(), CSV_CONTENT.getBytes(StandardCharsets.UTF_8)); storage.create( BlobInfo.builder(BUCKET, JSON_LOAD_FILE).contentType("application/json").build(), JSON_CONTENT.getBytes(StandardCharsets.UTF_8)); DatasetInfo info = DatasetInfo.builder(DATASET).description(DESCRIPTION).build(); bigquery.create(info); LoadJobConfiguration configuration = LoadJobConfiguration.builder( TABLE_ID, "gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json()) .createDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED) .schema(TABLE_SCHEMA) .build(); Job job = bigquery.create(JobInfo.of(configuration)); while (!job.isDone()) { Thread.sleep(1000); } assertNull(job.status().error()); }
@Test public void testCancelJob() throws InterruptedException { String destinationTableName = "test_cancel_query_job_table"; String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.table(); TableId destinationTable = TableId.of(DATASET, destinationTableName); QueryJobConfiguration configuration = QueryJobConfiguration.builder(query) .defaultDataset(DatasetId.of(DATASET)) .destinationTable(destinationTable) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); assertTrue(remoteJob.cancel()); while (!remoteJob.isDone()) { Thread.sleep(1000); } assertNull(remoteJob.status().error()); }
@Test public void testExtractJob() throws InterruptedException { String tableName = "test_export_job_table"; TableId destinationTable = TableId.of(DATASET, tableName); LoadJobConfiguration configuration = LoadJobConfiguration.builder(destinationTable, "gs://" + BUCKET + "/" + LOAD_FILE) .schema(SIMPLE_SCHEMA) .build(); Job remoteLoadJob = bigquery.create(JobInfo.of(configuration)); while (!remoteLoadJob.isDone()) { Thread.sleep(1000); } assertNull(remoteLoadJob.status().error()); ExtractJobConfiguration extractConfiguration = ExtractJobConfiguration.builder(destinationTable, "gs://" + BUCKET + "/" + EXTRACT_FILE) .printHeader(false) .build(); Job remoteExtractJob = bigquery.create(JobInfo.of(extractConfiguration)); while (!remoteExtractJob.isDone()) { Thread.sleep(1000); } assertNull(remoteExtractJob.status().error()); assertEquals( CSV_CONTENT, new String(storage.readAllBytes(BUCKET, EXTRACT_FILE), StandardCharsets.UTF_8)); assertTrue(bigquery.delete(DATASET, tableName)); }
@Test public void testQuery() throws InterruptedException { String query = new StringBuilder() .append("SELECT TimestampField, StringField, BooleanField FROM ") .append(TABLE_ID.table()) .toString(); QueryRequest request = QueryRequest.builder(query) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) .maxResults(1000L) .build(); QueryResponse response = bigquery.query(request); while (!response.jobCompleted()) { Thread.sleep(1000); response = bigquery.getQueryResults(response.jobId()); } assertEquals(QUERY_RESULT_SCHEMA, response.result().schema()); int rowCount = 0; for (List<FieldValue> row : response.result().values()) { FieldValue timestampCell = row.get(0); FieldValue stringCell = row.get(1); FieldValue booleanCell = row.get(2); assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute()); assertEquals(1408452095220000L, timestampCell.timestampValue()); assertEquals("stringValue", stringCell.stringValue()); assertEquals(false, booleanCell.booleanValue()); rowCount++; } assertEquals(2, rowCount); Job queryJob = bigquery.getJob(response.jobId()); JobStatistics.QueryStatistics statistics = queryJob.statistics(); assertNotNull(statistics.queryPlan()); }
@Test public void testListJobsWithSelectedFields() { Page<Job> jobs = bigquery.listJobs(JobListOption.fields(JobField.USER_EMAIL)); for (Job job : jobs.values()) { assertNotNull(job.jobId()); assertNotNull(job.status()); assertNotNull(job.userEmail()); assertNull(job.statistics()); assertNull(job.id()); } }
@Test public void testListJobs() { Page<Job> jobs = bigquery.listJobs(); for (Job job : jobs.values()) { assertNotNull(job.jobId()); assertNotNull(job.statistics()); assertNotNull(job.status()); assertNotNull(job.userEmail()); assertNotNull(job.id()); } }
@Test public void testQueryJob() throws InterruptedException { String tableName = "test_query_job_table"; String query = new StringBuilder() .append("SELECT TimestampField, StringField, BooleanField FROM ") .append(TABLE_ID.table()) .toString(); TableId destinationTable = TableId.of(DATASET, tableName); QueryJobConfiguration configuration = QueryJobConfiguration.builder(query) .defaultDataset(DatasetId.of(DATASET)) .destinationTable(destinationTable) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); while (!remoteJob.isDone()) { Thread.sleep(1000); } assertNull(remoteJob.status().error()); QueryResponse response = bigquery.getQueryResults(remoteJob.jobId()); while (!response.jobCompleted()) { Thread.sleep(1000); response = bigquery.getQueryResults(response.jobId()); } assertFalse(response.hasErrors()); assertEquals(QUERY_RESULT_SCHEMA, response.result().schema()); int rowCount = 0; for (List<FieldValue> row : response.result().values()) { FieldValue timestampCell = row.get(0); FieldValue stringCell = row.get(1); FieldValue booleanCell = row.get(2); assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute()); assertEquals(1408452095220000L, timestampCell.timestampValue()); assertEquals("stringValue", stringCell.stringValue()); assertEquals(false, booleanCell.booleanValue()); rowCount++; } assertEquals(2, rowCount); assertTrue(bigquery.delete(DATASET, tableName)); Job queryJob = bigquery.getJob(remoteJob.jobId()); JobStatistics.QueryStatistics statistics = queryJob.statistics(); assertNotNull(statistics.queryPlan()); }
@Test public void testCreateAndGetJobWithSelectedFields() { String sourceTableName = "test_create_and_get_job_with_selected_fields_source_table"; String destinationTableName = "test_create_and_get_job_with_selected_fields_destination_table"; TableId sourceTable = TableId.of(DATASET, sourceTableName); StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); Table createdTable = bigquery.create(tableInfo); assertNotNull(createdTable); assertEquals(DATASET, createdTable.tableId().dataset()); assertEquals(sourceTableName, createdTable.tableId().table()); TableId destinationTable = TableId.of(DATASET, destinationTableName); CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, sourceTable); Job createdJob = bigquery.create(JobInfo.of(configuration), JobOption.fields(JobField.ETAG)); CopyJobConfiguration createdConfiguration = createdJob.configuration(); assertNotNull(createdJob.jobId()); assertNotNull(createdConfiguration.sourceTables()); assertNotNull(createdConfiguration.destinationTable()); assertNotNull(createdJob.etag()); assertNull(createdJob.statistics()); assertNull(createdJob.status()); assertNull(createdJob.selfLink()); assertNull(createdJob.userEmail()); Job remoteJob = bigquery.getJob(createdJob.jobId(), JobOption.fields(JobField.ETAG)); CopyJobConfiguration remoteConfiguration = remoteJob.configuration(); assertEquals(createdJob.jobId(), remoteJob.jobId()); assertEquals(createdConfiguration.sourceTables(), remoteConfiguration.sourceTables()); assertEquals(createdConfiguration.destinationTable(), remoteConfiguration.destinationTable()); assertEquals(createdConfiguration.createDisposition(), remoteConfiguration.createDisposition()); assertEquals(createdConfiguration.writeDisposition(), remoteConfiguration.writeDisposition()); assertNotNull(remoteJob.etag()); assertNull(remoteJob.statistics()); assertNull(remoteJob.status()); assertNull(remoteJob.selfLink()); assertNull(remoteJob.userEmail()); assertTrue(createdTable.delete()); assertTrue(bigquery.delete(DATASET, destinationTableName)); }