Ejemplo n.º 1
0
 @Test
 public void testInsertAllWithSuffix() throws InterruptedException {
   String tableName = "test_insert_all_with_suffix_table";
   StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
   TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
   assertNotNull(bigquery.create(tableInfo));
   InsertAllRequest request =
       InsertAllRequest.builder(tableInfo.tableId())
           .addRow(
               ImmutableMap.<String, Object>of(
                   "TimestampField",
                   "2014-08-19 07:41:35.220 -05:00",
                   "StringField",
                   "stringValue",
                   "IntegerField",
                   ImmutableList.of(0, 1),
                   "BooleanField",
                   false,
                   "RecordField",
                   ImmutableMap.of(
                       "TimestampField",
                       "1969-07-20 20:18:04 UTC",
                       "IntegerField",
                       ImmutableList.of(1, 0),
                       "BooleanField",
                       true)))
           .addRow(
               ImmutableMap.<String, Object>of(
                   "TimestampField",
                   "2014-08-19 07:41:35.220 -05:00",
                   "StringField",
                   "stringValue",
                   "IntegerField",
                   ImmutableList.of(0, 1),
                   "BooleanField",
                   false,
                   "RecordField",
                   ImmutableMap.of(
                       "TimestampField",
                       "1969-07-20 20:18:04 UTC",
                       "IntegerField",
                       ImmutableList.of(1, 0),
                       "BooleanField",
                       true)))
           .templateSuffix("_suffix")
           .build();
   InsertAllResponse response = bigquery.insertAll(request);
   assertFalse(response.hasErrors());
   assertEquals(0, response.insertErrors().size());
   String newTableName = tableName + "_suffix";
   Table suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
   // wait until the new table is created. If the table is never created the test will time-out
   while (suffixTable == null) {
     Thread.sleep(1000L);
     suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
   }
   assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
   assertTrue(suffixTable.delete());
 }
Ejemplo n.º 2
0
 @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());
 }
Ejemplo n.º 3
0
 @Test
 public void testCreateViewTable() throws InterruptedException {
   String tableName = "test_create_view_table";
   TableId tableId = TableId.of(DATASET, tableName);
   ViewDefinition viewDefinition =
       ViewDefinition.of(
           "SELECT TimestampField, StringField, BooleanField FROM "
               + DATASET
               + "."
               + TABLE_ID.table());
   TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
   Table createdTable = bigquery.create(tableInfo);
   assertNotNull(createdTable);
   assertEquals(DATASET, createdTable.tableId().dataset());
   assertEquals(tableName, createdTable.tableId().table());
   Table remoteTable = bigquery.getTable(DATASET, tableName);
   assertNotNull(remoteTable);
   assertEquals(createdTable.tableId(), remoteTable.tableId());
   assertTrue(remoteTable.definition() instanceof ViewDefinition);
   Schema expectedSchema =
       Schema.builder()
           .addField(
               Field.builder("TimestampField", Field.Type.timestamp())
                   .mode(Field.Mode.NULLABLE)
                   .build())
           .addField(
               Field.builder("StringField", Field.Type.string()).mode(Field.Mode.NULLABLE).build())
           .addField(
               Field.builder("BooleanField", Field.Type.bool()).mode(Field.Mode.NULLABLE).build())
           .build();
   assertEquals(expectedSchema, remoteTable.definition().schema());
   QueryRequest request =
       QueryRequest.builder("SELECT * FROM " + tableName)
           .defaultDataset(DatasetId.of(DATASET))
           .maxWaitTime(60000L)
           .maxResults(1000L)
           .build();
   QueryResponse response = bigquery.query(request);
   while (!response.jobCompleted()) {
     response = bigquery.getQueryResults(response.jobId());
     Thread.sleep(1000);
   }
   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(remoteTable.delete());
 }
Ejemplo n.º 4
0
 @Test
 public void testCreateExternalTable() throws InterruptedException {
   String tableName = "test_create_external_table";
   TableId tableId = TableId.of(DATASET, tableName);
   ExternalTableDefinition externalTableDefinition =
       ExternalTableDefinition.of(
           "gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json());
   TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
   Table createdTable = bigquery.create(tableInfo);
   assertNotNull(createdTable);
   assertEquals(DATASET, createdTable.tableId().dataset());
   assertEquals(tableName, createdTable.tableId().table());
   Table remoteTable = bigquery.getTable(DATASET, tableName);
   assertNotNull(remoteTable);
   assertTrue(remoteTable.definition() instanceof ExternalTableDefinition);
   assertEquals(createdTable.tableId(), remoteTable.tableId());
   assertEquals(TABLE_SCHEMA, remoteTable.definition().schema());
   QueryRequest request =
       QueryRequest.builder(
               "SELECT TimestampField, StringField, IntegerField, BooleanField FROM "
                   + DATASET
                   + "."
                   + tableName)
           .defaultDataset(DatasetId.of(DATASET))
           .maxWaitTime(60000L)
           .maxResults(1000L)
           .build();
   QueryResponse response = bigquery.query(request);
   while (!response.jobCompleted()) {
     response = bigquery.getQueryResults(response.jobId());
     Thread.sleep(1000);
   }
   long integerValue = 0;
   int rowCount = 0;
   for (List<FieldValue> row : response.result().values()) {
     FieldValue timestampCell = row.get(0);
     FieldValue stringCell = row.get(1);
     FieldValue integerCell = row.get(2);
     FieldValue booleanCell = row.get(3);
     assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute());
     assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
     assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.attribute());
     assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
     assertEquals(1408452095220000L, timestampCell.timestampValue());
     assertEquals("stringValue", stringCell.stringValue());
     assertEquals(integerValue, integerCell.longValue());
     assertEquals(false, booleanCell.booleanValue());
     integerValue = ~integerValue & 0x1;
     rowCount++;
   }
   assertEquals(4, rowCount);
   assertTrue(remoteTable.delete());
 }
Ejemplo n.º 5
0
 @Test
 public void testInsertFromFile() throws InterruptedException {
   String destinationTableName = "test_insert_from_file_table";
   TableId tableId = TableId.of(DATASET, destinationTableName);
   WriteChannelConfiguration configuration =
       WriteChannelConfiguration.builder(tableId)
           .formatOptions(FormatOptions.json())
           .createDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED)
           .schema(TABLE_SCHEMA)
           .build();
   try (WriteChannel channel = bigquery.writer(configuration)) {
     channel.write(ByteBuffer.wrap(JSON_CONTENT.getBytes(StandardCharsets.UTF_8)));
   } catch (IOException e) {
     fail("IOException was not expected");
   }
   // wait until the new table is created. If the table is never created the test will time-out
   while (bigquery.getTable(tableId) == null) {
     Thread.sleep(1000L);
   }
   Page<List<FieldValue>> rows = bigquery.listTableData(tableId);
   int rowCount = 0;
   for (List<FieldValue> row : rows.values()) {
     FieldValue timestampCell = row.get(0);
     FieldValue stringCell = row.get(1);
     FieldValue integerCell = row.get(2);
     FieldValue booleanCell = row.get(3);
     FieldValue recordCell = row.get(4);
     assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute());
     assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
     assertEquals(FieldValue.Attribute.REPEATED, integerCell.attribute());
     assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
     assertEquals(FieldValue.Attribute.RECORD, recordCell.attribute());
     assertEquals(1408452095220000L, timestampCell.timestampValue());
     assertEquals("stringValue", stringCell.stringValue());
     assertEquals(0, integerCell.repeatedValue().get(0).longValue());
     assertEquals(1, integerCell.repeatedValue().get(1).longValue());
     assertEquals(false, booleanCell.booleanValue());
     assertEquals(-14182916000000L, recordCell.recordValue().get(0).timestampValue());
     assertTrue(recordCell.recordValue().get(1).isNull());
     assertEquals(1, recordCell.recordValue().get(2).repeatedValue().get(0).longValue());
     assertEquals(0, recordCell.recordValue().get(2).repeatedValue().get(1).longValue());
     assertEquals(true, recordCell.recordValue().get(3).booleanValue());
     rowCount++;
   }
   assertEquals(2, rowCount);
   assertTrue(bigquery.delete(DATASET, destinationTableName));
 }
Ejemplo n.º 6
0
 @Test
 public void testCreateAndGetTable() {
   String tableName = "test_create_and_get_table";
   TableId tableId = TableId.of(DATASET, tableName);
   StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
   Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
   assertNotNull(createdTable);
   assertEquals(DATASET, createdTable.tableId().dataset());
   assertEquals(tableName, createdTable.tableId().table());
   Table remoteTable = bigquery.getTable(DATASET, tableName);
   assertNotNull(remoteTable);
   assertTrue(remoteTable.definition() instanceof StandardTableDefinition);
   assertEquals(createdTable.tableId(), remoteTable.tableId());
   assertEquals(TableDefinition.Type.TABLE, remoteTable.definition().type());
   assertEquals(TABLE_SCHEMA, remoteTable.definition().schema());
   assertNotNull(remoteTable.creationTime());
   assertNotNull(remoteTable.lastModifiedTime());
   assertNotNull(remoteTable.<StandardTableDefinition>definition().numBytes());
   assertNotNull(remoteTable.<StandardTableDefinition>definition().numRows());
   assertTrue(remoteTable.delete());
 }
Ejemplo n.º 7
0
 @Test
 public void testGetNonExistingTable() {
   assertNull(bigquery.getTable(DATASET, "test_get_non_existing_table"));
 }