Example #1
0
 @Test
 public void testInsertComplete() throws Exception {
   expect(bigquery.insertAll(INSERT_ALL_REQUEST_COMPLETE)).andReturn(EMPTY_INSERT_ALL_RESPONSE);
   replay(bigquery);
   InsertAllResponse response = table.insert(ROWS_TO_INSERT, true, true);
   assertSame(EMPTY_INSERT_ALL_RESPONSE, response);
 }
Example #2
0
 @Test
 public void testExists_False() throws Exception {
   BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()};
   expect(bigquery.getTable(TABLE_INFO.tableId(), expectedOptions)).andReturn(null);
   replay(bigquery);
   assertFalse(table.exists());
 }
Example #3
0
 @Test
 public void testGetFromStrings() throws Exception {
   expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(TABLE_INFO);
   replay(bigquery);
   Table loadedTable = Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table());
   assertNotNull(loadedTable);
   assertEquals(TABLE_INFO, loadedTable.info());
 }
Example #4
0
 @Test
 public void testLoadDataUris() throws Exception {
   expect(bigquery.create(LOAD_JOB_INFO)).andReturn(LOAD_JOB_INFO);
   replay(bigquery);
   Job job = table.load(FormatOptions.json(), ImmutableList.of("URI"));
   assertSame(bigquery, job.bigquery());
   assertEquals(LOAD_JOB_INFO, job.info());
 }
Example #5
0
 @Test
 public void testCopyFromId() throws Exception {
   expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO);
   replay(bigquery);
   Job job = table.copy(TABLE_ID2);
   assertSame(bigquery, job.bigquery());
   assertEquals(COPY_JOB_INFO, job.info());
 }
Example #6
0
 @Test
 public void testExtractDataUris() throws Exception {
   expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(EXTRACT_JOB_INFO);
   replay(bigquery);
   Job job = table.extract("CSV", ImmutableList.of("URI"));
   assertSame(bigquery, job.bigquery());
   assertEquals(EXTRACT_JOB_INFO, job.info());
 }
Example #7
0
 @Test
 public void testGetFromIdWithOptions() throws Exception {
   expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
       .andReturn(TABLE_INFO);
   replay(bigquery);
   Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId(), BigQuery.TableOption.fields());
   assertNotNull(loadedTable);
   assertEquals(TABLE_INFO, loadedTable.info());
 }
Example #8
0
 @Test
 public void testUpdateWithOptions() throws Exception {
   BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
   expect(bigquery.update(updatedInfo, BigQuery.TableOption.fields())).andReturn(updatedInfo);
   replay(bigquery);
   Table updatedTable = table.update(updatedInfo, BigQuery.TableOption.fields());
   assertSame(bigquery, updatedTable.bigquery());
   assertEquals(updatedInfo, updatedTable.info());
 }
Example #9
0
 /**
  * Updates the table's information. Dataset's and table's user-defined ids cannot be changed. A
  * new {@code Table} object is returned.
  *
  * @param tableInfo new table's information. Dataset's and table's user-defined ids must match the
  *     ones of the current table
  * @param options dataset options
  * @return a {@code Table} object with updated information
  * @throws BigQueryException upon failure
  */
 public Table update(BaseTableInfo tableInfo, BigQuery.TableOption... options) {
   checkArgument(
       Objects.equals(tableInfo.tableId().dataset(), info.tableId().dataset()),
       "Dataset's user-defined ids must match");
   checkArgument(
       Objects.equals(tableInfo.tableId().table(), info.tableId().table()),
       "Table's user-defined ids must match");
   return new Table(bigquery, bigquery.update(tableInfo, options));
 }
Example #10
0
 @Test
 public void testReload() throws Exception {
   TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
   expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(updatedInfo);
   replay(bigquery);
   Table updatedTable = table.reload();
   assertSame(bigquery, updatedTable.bigquery());
   assertEquals(updatedInfo, updatedTable.info());
 }
Example #11
0
 @Test
 public void testList() throws Exception {
   PageImpl<List<FieldValue>> tableDataPage = new PageImpl<>(null, "c", ROWS);
   expect(bigquery.listTableData(TABLE_ID1)).andReturn(tableDataPage);
   replay(bigquery);
   Page<List<FieldValue>> dataPage = table.list();
   Iterator<List<FieldValue>> tableDataIterator = tableDataPage.values().iterator();
   Iterator<List<FieldValue>> dataIterator = dataPage.values().iterator();
   assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator));
 }
Example #12
0
 /**
  * Insert rows into the table.
  *
  * @param rows rows to be inserted
  * @param skipInvalidRows whether to insert all valid rows, even if invalid rows exist. If not set
  *     the entire insert operation will fail if rows to be inserted contain an invalid row
  * @param ignoreUnknownValues whether to accept rows that contain values that do not match the
  *     schema. The unknown values are ignored. If not set, rows with unknown values are considered
  *     to be invalid
  * @throws BigQueryException upon failure
  */
 InsertAllResponse insert(
     Iterable<InsertAllRequest.RowToInsert> rows,
     boolean skipInvalidRows,
     boolean ignoreUnknownValues)
     throws BigQueryException {
   InsertAllRequest request =
       InsertAllRequest.builder(info.tableId(), rows)
           .skipInvalidRows(skipInvalidRows)
           .ignoreUnknownValues(ignoreUnknownValues)
           .build();
   return bigquery.insertAll(request);
 }
Example #13
0
 /**
  * Starts a BigQuery Job to load data into the current table from the provided source URIs.
  * Returns the started {@link Job} object.
  *
  * @param format the format of the exported data
  * @param sourceUris the fully-qualified Google Cloud Storage URIs (e.g. gs://bucket/path) from
  *     which to load the data
  * @param options job options
  * @throws BigQueryException upon failure
  */
 Job load(FormatOptions format, List<String> sourceUris, BigQuery.JobOption... options)
     throws BigQueryException {
   LoadJobConfiguration loadConfig = LoadJobConfiguration.of(info.tableId(), sourceUris, format);
   return new Job(bigquery, bigquery.create(JobInfo.of(loadConfig), options));
 }
Example #14
0
 /**
  * Starts a BigQuery Job to extract the current table to the provided destination URIs. Returns
  * the started {@link Job} object.
  *
  * @param format the format of the exported data
  * @param destinationUris the fully-qualified Google Cloud Storage URIs (e.g. gs://bucket/path)
  *     where the extracted table should be written
  * @param options job options
  * @throws BigQueryException upon failure
  */
 Job extract(String format, List<String> destinationUris, BigQuery.JobOption... options)
     throws BigQueryException {
   ExtractJobConfiguration extractConfiguration =
       ExtractJobConfiguration.of(info.tableId(), destinationUris, format);
   return new Job(bigquery, bigquery.create(JobInfo.of(extractConfiguration), options));
 }
Example #15
0
 /**
  * Starts a BigQuery Job to copy the current table to the provided destination table. Returns the
  * started {@link Job} object.
  *
  * @param destinationTable the destination table of the copy job
  * @param options job options
  * @throws BigQueryException upon failure
  */
 Job copy(TableId destinationTable, BigQuery.JobOption... options) throws BigQueryException {
   CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, info.tableId());
   return new Job(bigquery, bigquery.create(JobInfo.of(configuration), options));
 }
Example #16
0
 /**
  * Returns the paginated list rows in this table.
  *
  * @param options table data list options
  * @throws BigQueryException upon failure
  */
 Page<List<FieldValue>> list(BigQuery.TableDataListOption... options) throws BigQueryException {
   return bigquery.listTableData(info.tableId(), options);
 }
Example #17
0
 /**
  * Insert rows into the table.
  *
  * @param rows rows to be inserted
  * @throws BigQueryException upon failure
  */
 InsertAllResponse insert(Iterable<InsertAllRequest.RowToInsert> rows) throws BigQueryException {
   return bigquery.insertAll(InsertAllRequest.of(info.tableId(), rows));
 }
Example #18
0
 /**
  * Deletes this table.
  *
  * @return {@code true} if table was deleted, {@code false} if it was not found
  * @throws BigQueryException upon failure
  */
 public boolean delete() {
   return bigquery.delete(info.tableId());
 }
Example #19
0
 @Test
 public void testDelete() throws Exception {
   expect(bigquery.delete(TABLE_INFO.tableId())).andReturn(true);
   replay(bigquery);
   assertTrue(table.delete());
 }
Example #20
0
 /**
  * Creates a {@code Table} object for the provided table identity. Performs an RPC call to get the
  * latest table information.
  *
  * @param bigquery the BigQuery service used for issuing requests
  * @param table the table's identity
  * @param options table options
  * @return the {@code Table} object or {@code null} if not found
  * @throws BigQueryException upon failure
  */
 public static Table get(BigQuery bigquery, TableId table, BigQuery.TableOption... options) {
   BaseTableInfo info = bigquery.getTable(table, options);
   return info != null ? new Table(bigquery, info) : null;
 }
Example #21
0
 @Test
 public void testReloadNull() throws Exception {
   expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
   replay(bigquery);
   assertNull(table.reload());
 }
Example #22
0
 /**
  * Checks if this table exists.
  *
  * @return {@code true} if this table exists, {@code false} otherwise
  * @throws BigQueryException upon failure
  */
 public boolean exists() {
   return bigquery.getTable(info.tableId(), BigQuery.TableOption.fields()) != null;
 }
Example #23
0
 @Test
 public void testGetFromStringsNull() throws Exception {
   expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
   replay(bigquery);
   assertNull(Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table()));
 }