private void onTableGet(Table table) throws IOException { when(mockClient.tables()).thenReturn(mockTables); when(mockTables.get(anyString(), anyString(), anyString())).thenReturn(mockTablesGet); when(mockTablesGet.execute()).thenReturn(table); }
public void deleteTable(String projectId, String datasetId, String tableName) { try { bqClient.tables().delete(projectId, datasetId, tableName).execute(); LOG.info("Successfully deleted table: " + tableName); } catch (Exception e) { LOG.debug("Exception caught when deleting table: " + e.getMessage()); } }
public void createNewTable(String projectId, String datasetId, Table newTable) { try { this.bqClient.tables().insert(projectId, datasetId, newTable).execute(); LOG.info("Successfully created new table: " + newTable.getId()); } catch (Exception e) { LOG.debug("Exceptions caught when creating new table: " + e.getMessage()); } }
return client .tables() .insert( table.getTableReference().getProjectId(), table.getTableReference().getDatasetId(),
table.setExternalDataConfiguration(externalConf); service.tables().insert(projectId, tableRef.getDatasetId(), table).execute();
Table table = executeNullIfNotFound(tableService.get(projectId, datasetId, tableId)); if (table == null) { Table newTable = .setDatasetId(datasetId) .setTableId(tableId)); tableService.insert(projectId, datasetId, newTable).execute(); } else if (!table.getSchema().equals(schema)) { throw new RuntimeException(
bigquery .tables() .list(projectId, datasetId) .setMaxResults(Option.MAX_RESULTS.getLong(options)) .setPageToken(Option.PAGE_TOKEN.getString(options))
@Override public Tuple<String, Iterable<Table>> listTables(String datasetId, Map<Option, ?> options) { try { TableList tableList = bigquery.tables() .list(this.options.projectId(), datasetId) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) .execute(); Iterable<TableList.Tables> tables = tableList.getTables(); return Tuple.of(tableList.getNextPageToken(), Iterables.transform(tables != null ? tables : ImmutableList.<TableList.Tables>of(), new Function<TableList.Tables, Table>() { @Override public Table apply(TableList.Tables tablePb) { return new Table() .setFriendlyName(tablePb.getFriendlyName()) .setId(tablePb.getId()) .setKind(tablePb.getKind()) .setTableReference(tablePb.getTableReference()) .setType(tablePb.getType()); } })); } catch (IOException ex) { throw translate(ex); } }
@Override public Tuple<String, Iterable<Table>> listTables(String datasetId, Map<Option, ?> options) { try { TableList tableList = bigquery.tables() .list(this.options.projectId(), datasetId) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) .execute(); Iterable<TableList.Tables> tables = tableList.getTables(); return Tuple.of(tableList.getNextPageToken(), Iterables.transform(tables != null ? tables : ImmutableList.<TableList.Tables>of(), new Function<TableList.Tables, Table>() { @Override public Table apply(TableList.Tables tablePb) { return new Table() .setFriendlyName(tablePb.getFriendlyName()) .setId(tablePb.getId()) .setKind(tablePb.getKind()) .setTableReference(tablePb.getTableReference()) .setType(tablePb.getType()); } })); } catch (IOException ex) { throw translate(ex); } }
@Override public Table patchTableDescription( TableReference tableReference, @Nullable String tableDescription) throws IOException, InterruptedException { Table table = new Table(); table.setDescription(tableDescription); return executeWithRetries( client .tables() .patch( tableReference.getProjectId(), tableReference.getDatasetId(), tableReference.getTableId(), table), String.format( "Unable to patch table description: %s, aborting after %d retries.", tableReference, MAX_RPC_RETRIES), Sleeper.DEFAULT, createDefaultBackoff(), ALWAYS_RETRY); } }
/** * {@inheritDoc} * * <p>Tries executing the RPC for at most {@code MAX_RPC_RETRIES} times until it succeeds. * * @throws IOException if it exceeds {@code MAX_RPC_RETRIES} attempts. */ @Override public void deleteTable(TableReference tableRef) throws IOException, InterruptedException { executeWithRetries( client .tables() .delete(tableRef.getProjectId(), tableRef.getDatasetId(), tableRef.getTableId()), String.format( "Unable to delete table: %s, aborting after %d retries.", tableRef.getTableId(), MAX_RPC_RETRIES), Sleeper.DEFAULT, createDefaultBackoff(), ALWAYS_RETRY); }
@Test public void testExecuteWithRetries() throws IOException, InterruptedException { Table testTable = new Table(); when(response.getContentType()).thenReturn(Json.MEDIA_TYPE); when(response.getStatusCode()).thenReturn(200); when(response.getContent()).thenReturn(toStream(testTable)); Table table = BigQueryServicesImpl.executeWithRetries( bigquery.tables().get("projectId", "datasetId", "tableId"), "Failed to get table.", Sleeper.DEFAULT, BackOff.STOP_BACKOFF, BigQueryServicesImpl.ALWAYS_RETRY); assertEquals(testTable, table); verify(response, times(1)).getStatusCode(); verify(response, times(1)).getContent(); verify(response, times(1)).getContentType(); }
@VisibleForTesting @Nullable Table getTable(TableReference ref, BackOff backoff, Sleeper sleeper) throws IOException, InterruptedException { try { return executeWithRetries( client.tables().get(ref.getProjectId(), ref.getDatasetId(), ref.getTableId()), String.format( "Unable to get table: %s, aborting after %d retries.", ref.getTableId(), MAX_RPC_RETRIES), sleeper, backoff, DONT_RETRY_NOT_FOUND); } catch (IOException e) { if (errorExtractor.itemNotFound(e)) { return null; } throw e; } }
/** * Returns true if the table exists, or false if not. */ public boolean tableExists(TableReference tableRef) throws IOException { try { Table fetchedTable = service.tables().get( tableRef.getProjectId(), tableRef.getDatasetId(), tableRef.getTableId()).execute(); logger.atFine().log( "Successfully fetched table '%s' for tableRef '%s'", fetchedTable, tableRef); return true; } catch (IOException ioe) { if (errorExtractor.itemNotFound(ioe)) { return false; } else { // Unhandled exceptions should just propagate up. throw ioe; } } }
public static Table createTable(String projectId, String datasetId, String tableId, TableSchema schema, Bigquery bigquery) throws IOException { Table table = new Table(); TableReference tableRef = new TableReference(); tableRef.setDatasetId(datasetId); tableRef.setProjectId(projectId); tableRef.setTableId(tableId); table.setTableReference(tableRef); table.setFriendlyName(tableId); table.setSchema(schema); try { return bigquery.tables().insert(projectId, datasetId, table).execute(); } catch (GoogleJsonResponseException e) { return null; //table already exists } } }
@Override public Table getTable( String projectId, String datasetId, String tableId, Map<Option, ?> options) { try { return bigquery .tables() .get(projectId, datasetId, tableId) .setFields(Option.FIELDS.getString(options)) .execute(); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { return null; } throw serviceException; } }
public void deleteDataset(String projectId, String datasetId) { try { TableList tables = bqClient.tables().list(projectId, datasetId).execute(); for (Tables table : tables.getTables()) { this.deleteTable(projectId, datasetId, table.getTableReference().getTableId()); } } catch (Exception e) { LOG.debug("Exceptions caught when listing all tables: " + e.getMessage()); } try { bqClient.datasets().delete(projectId, datasetId).execute(); LOG.info("Successfully deleted dataset: " + datasetId); } catch (Exception e) { LOG.debug("Exceptions caught when deleting dataset: " + e.getMessage()); } }
void createTable(String projectId, Table table) throws IOException { String datasetId = table.getTableReference().getDatasetId(); try { client.tables().insert(projectId, datasetId, table) .execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_CONFLICT) { logger.debug("Table already exists: {}:{}.{}", projectId, datasetId, table.getTableReference().getTableId()); } else { throw e; } } }
private void deleteTables(String projectId, String datasetId) throws IOException { Bigquery.Tables.List list = client.tables().list(projectId, datasetId); TableList tableList; do { tableList = list.execute(); List<TableList.Tables> tables = tableList.getTables(); if (tables != null) { for (TableList.Tables table : tables) { deleteTable(projectId, datasetId, table.getTableReference().getTableId()); } } } while (tableList.getNextPageToken() != null); }