コード例 #1
0
ファイル: TableTest.java プロジェクト: mderka/gcloud-java
 @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());
 }
コード例 #2
0
ファイル: TableTest.java プロジェクト: mderka/gcloud-java
 @Test
 public void testCopyFromString() throws Exception {
   expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO);
   replay(bigquery);
   Job job = table.copy(TABLE_ID2.dataset(), TABLE_ID2.table());
   assertSame(bigquery, job.bigquery());
   assertEquals(COPY_JOB_INFO, job.info());
 }
コード例 #3
0
  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    OFFlowStatsEntryVer10 other = (OFFlowStatsEntryVer10) obj;

    if (tableId == null) {
      if (other.tableId != null) return false;
    } else if (!tableId.equals(other.tableId)) return false;
    if (match == null) {
      if (other.match != null) return false;
    } else if (!match.equals(other.match)) return false;
    if (durationSec != other.durationSec) return false;
    if (durationNsec != other.durationNsec) return false;
    if (priority != other.priority) return false;
    if (idleTimeout != other.idleTimeout) return false;
    if (hardTimeout != other.hardTimeout) return false;
    if (cookie == null) {
      if (other.cookie != null) return false;
    } else if (!cookie.equals(other.cookie)) return false;
    if (packetCount == null) {
      if (other.packetCount != null) return false;
    } else if (!packetCount.equals(other.packetCount)) return false;
    if (byteCount == null) {
      if (other.byteCount != null) return false;
    } else if (!byteCount.equals(other.byteCount)) return false;
    if (actions == null) {
      if (other.actions != null) return false;
    } else if (!actions.equals(other.actions)) return false;
    return true;
  }
コード例 #4
0
 @Test
 public void testToBuilder() {
   compareLoadConfiguration(LOAD_CONFIGURATION, LOAD_CONFIGURATION.toBuilder().build());
   WriteChannelConfiguration configuration =
       LOAD_CONFIGURATION.toBuilder().destinationTable(TableId.of("dataset", "newTable")).build();
   assertEquals("newTable", configuration.destinationTable().table());
   configuration = configuration.toBuilder().destinationTable(TABLE_ID).build();
   compareLoadConfiguration(LOAD_CONFIGURATION, configuration);
 }
コード例 #5
0
ファイル: TableTest.java プロジェクト: mderka/gcloud-java
 @Test
 public void testUpdateWithDifferentDatasetId() throws Exception {
   TableInfo updatedInfo =
       TABLE_INFO
           .toBuilder()
           .tableId(TableId.of("dataset1", "table1"))
           .description("Description")
           .build();
   replay(bigquery);
   thrown.expect(IllegalArgumentException.class);
   table.update(updatedInfo);
 }
コード例 #6
0
    @Override
    public OFFlowStatsEntry readFrom(ByteBuf bb) throws OFParseError {
      int start = bb.readerIndex();
      int length = U16.f(bb.readShort());
      if (length < MINIMUM_LENGTH)
        throw new OFParseError(
            "Wrong length: Expected to be >= " + MINIMUM_LENGTH + ", was: " + length);
      if (bb.readableBytes() + (bb.readerIndex() - start) < length) {
        // Buffer does not have all data yet
        bb.readerIndex(start);
        return null;
      }
      if (logger.isTraceEnabled()) logger.trace("readFrom - length={}", length);
      TableId tableId = TableId.readByte(bb);
      // pad: 1 bytes
      bb.skipBytes(1);
      long durationSec = U32.f(bb.readInt());
      long durationNsec = U32.f(bb.readInt());
      int priority = U16.f(bb.readShort());
      int idleTimeout = U16.f(bb.readShort());
      int hardTimeout = U16.f(bb.readShort());
      Set<OFFlowModFlags> flags = OFFlowModFlagsSerializerVer14.readFrom(bb);
      int importance = U16.f(bb.readShort());
      // pad: 2 bytes
      bb.skipBytes(2);
      U64 cookie = U64.ofRaw(bb.readLong());
      U64 packetCount = U64.ofRaw(bb.readLong());
      U64 byteCount = U64.ofRaw(bb.readLong());
      Match match = ChannelUtilsVer14.readOFMatch(bb);
      List<OFInstruction> instructions =
          ChannelUtils.readList(bb, length - (bb.readerIndex() - start), OFInstructionVer14.READER);

      OFFlowStatsEntryVer14 flowStatsEntryVer14 =
          new OFFlowStatsEntryVer14(
              tableId,
              durationSec,
              durationNsec,
              priority,
              idleTimeout,
              hardTimeout,
              flags,
              importance,
              cookie,
              packetCount,
              byteCount,
              match,
              instructions);
      if (logger.isTraceEnabled()) logger.trace("readFrom - read={}", flowStatsEntryVer14);
      return flowStatsEntryVer14;
    }
コード例 #7
0
 /**
  * Blocks until the table has finished loading and returns the result. If any errors were
  * encountered while loading the table an IncompleteTable will be returned.
  */
 public Table get() {
   Table tbl;
   try {
     tbl = tblTask_.get();
   } catch (Exception e) {
     tbl =
         IncompleteTable.createFailedMetadataLoadTable(
             TableId.createInvalidId(),
             catalog_.getDb(tblName_.getDb_name()),
             tblName_.getTable_name(),
             new TableLoadingException(e.getMessage(), e));
   }
   Preconditions.checkState(tbl.isLoaded());
   return tbl;
 }
コード例 #8
0
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;

    result = prime * result + ((tableId == null) ? 0 : tableId.hashCode());
    result = prime * result + ((match == null) ? 0 : match.hashCode());
    result = prime * (int) (durationSec ^ (durationSec >>> 32));
    result = prime * (int) (durationNsec ^ (durationNsec >>> 32));
    result = prime * result + priority;
    result = prime * result + idleTimeout;
    result = prime * result + hardTimeout;
    result = prime * result + ((cookie == null) ? 0 : cookie.hashCode());
    result = prime * result + ((packetCount == null) ? 0 : packetCount.hashCode());
    result = prime * result + ((byteCount == null) ? 0 : byteCount.hashCode());
    result = prime * result + ((actions == null) ? 0 : actions.hashCode());
    return result;
  }
コード例 #9
0
 @Override
 com.google.api.services.bigquery.model.JobConfiguration toPb() {
   com.google.api.services.bigquery.model.JobConfiguration configurationPb =
       new com.google.api.services.bigquery.model.JobConfiguration();
   JobConfigurationQuery queryConfigurationPb = new JobConfigurationQuery();
   queryConfigurationPb.setQuery(query);
   configurationPb.setDryRun(dryRun());
   if (allowLargeResults != null) {
     queryConfigurationPb.setAllowLargeResults(allowLargeResults);
   }
   if (createDisposition != null) {
     queryConfigurationPb.setCreateDisposition(createDisposition.toString());
   }
   if (destinationTable != null) {
     queryConfigurationPb.setDestinationTable(destinationTable.toPb());
   }
   if (defaultDataset != null) {
     queryConfigurationPb.setDefaultDataset(defaultDataset.toPb());
   }
   if (flattenResults != null) {
     queryConfigurationPb.setFlattenResults(flattenResults);
   }
   if (priority != null) {
     queryConfigurationPb.setPriority(priority.toString());
   }
   if (tableDefinitions != null) {
     queryConfigurationPb.setTableDefinitions(
         Maps.transformValues(
             tableDefinitions, ExternalTableDefinition.TO_EXTERNAL_DATA_FUNCTION));
   }
   if (useQueryCache != null) {
     queryConfigurationPb.setUseQueryCache(useQueryCache);
   }
   if (userDefinedFunctions != null) {
     queryConfigurationPb.setUserDefinedFunctionResources(
         Lists.transform(userDefinedFunctions, UserDefinedFunction.TO_PB_FUNCTION));
   }
   if (writeDisposition != null) {
     queryConfigurationPb.setWriteDisposition(writeDisposition.toString());
   }
   return configurationPb.setQuery(queryConfigurationPb);
 }
コード例 #10
0
 private Builder(com.google.api.services.bigquery.model.JobConfiguration configurationPb) {
   this();
   JobConfigurationQuery queryConfigurationPb = configurationPb.getQuery();
   this.query = queryConfigurationPb.getQuery();
   allowLargeResults = queryConfigurationPb.getAllowLargeResults();
   useQueryCache = queryConfigurationPb.getUseQueryCache();
   flattenResults = queryConfigurationPb.getFlattenResults();
   dryRun = configurationPb.getDryRun();
   if (queryConfigurationPb.getDestinationTable() != null) {
     destinationTable = TableId.fromPb(queryConfigurationPb.getDestinationTable());
   }
   if (queryConfigurationPb.getDefaultDataset() != null) {
     defaultDataset = DatasetId.fromPb(queryConfigurationPb.getDefaultDataset());
   }
   if (queryConfigurationPb.getPriority() != null) {
     priority = Priority.valueOf(queryConfigurationPb.getPriority());
   }
   if (queryConfigurationPb.getTableDefinitions() != null) {
     tableDefinitions =
         Maps.transformValues(
             queryConfigurationPb.getTableDefinitions(),
             ExternalTableDefinition.FROM_EXTERNAL_DATA_FUNCTION);
   }
   if (queryConfigurationPb.getUserDefinedFunctionResources() != null) {
     userDefinedFunctions =
         Lists.transform(
             queryConfigurationPb.getUserDefinedFunctionResources(),
             UserDefinedFunction.FROM_PB_FUNCTION);
   }
   if (queryConfigurationPb.getCreateDisposition() != null) {
     createDisposition = CreateDisposition.valueOf(queryConfigurationPb.getCreateDisposition());
   }
   if (queryConfigurationPb.getWriteDisposition() != null) {
     writeDisposition = WriteDisposition.valueOf(queryConfigurationPb.getWriteDisposition());
   }
 }
コード例 #11
0
 @Test
 public void testOf() {
   InsertAllRequest request = InsertAllRequest.of(TABLE_ID, ROWS);
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
   request = InsertAllRequest.of(TABLE_INFO, ROWS);
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
   request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS);
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
   request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS);
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
   request = InsertAllRequest.of(TABLE_ID, ROWS.get(0), ROWS.get(1));
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
   request = InsertAllRequest.of(TABLE_INFO, ROWS.get(0), ROWS.get(1));
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
   request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS.get(0), ROWS.get(1));
   assertEquals(TABLE_ID, request.table());
   assertEquals(ROWS, request.rows());
 }
コード例 #12
0
public class InsertAllRequestTest {

  private static final Map<String, Object> CONTENT1 =
      ImmutableMap.<String, Object>of("key", "val1");
  private static final Map<String, Object> CONTENT2 =
      ImmutableMap.<String, Object>of("key", "val2");
  private static final List<InsertAllRequest.RowToInsert> ROWS =
      ImmutableList.of(
          InsertAllRequest.RowToInsert.of(CONTENT1), InsertAllRequest.RowToInsert.of(CONTENT2));
  private static final List<InsertAllRequest.RowToInsert> ROWS_WITH_ID =
      ImmutableList.of(
          InsertAllRequest.RowToInsert.of("id1", CONTENT1),
          InsertAllRequest.RowToInsert.of("id2", CONTENT2));
  private static final TableId TABLE_ID = TableId.of("dataset", "table");
  private static final Schema TABLE_SCHEMA = Schema.of();
  private static final BaseTableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_SCHEMA);
  private static final boolean SKIP_INVALID_ROWS = true;
  private static final boolean IGNORE_UNKNOWN_VALUES = false;
  private static final String TEMPLATE_SUFFIX = "templateSuffix";
  private static final InsertAllRequest INSERT_ALL_REQUEST1 =
      InsertAllRequest.builder(TABLE_ID)
          .addRow(CONTENT1)
          .addRow(CONTENT2)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST2 =
      InsertAllRequest.builder(TABLE_ID)
          .rows(ROWS)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST3 =
      InsertAllRequest.builder(TABLE_ID.dataset(), TABLE_ID.table())
          .rows(ROWS_WITH_ID)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST4 =
      InsertAllRequest.builder(TABLE_ID, ROWS)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST5 =
      InsertAllRequest.builder(TABLE_ID.dataset(), TABLE_ID.table(), ROWS_WITH_ID)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST6 =
      InsertAllRequest.builder(TABLE_ID, ROWS.get(0), ROWS.get(1))
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST7 =
      InsertAllRequest.builder(
              TABLE_ID.dataset(), TABLE_ID.table(), ROWS_WITH_ID.get(0), ROWS_WITH_ID.get(1))
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST8 =
      InsertAllRequest.builder(TABLE_ID.dataset(), TABLE_ID.table())
          .addRow("id1", CONTENT1)
          .addRow("id2", CONTENT2)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST9 =
      InsertAllRequest.builder(TABLE_INFO)
          .addRow("id1", CONTENT1)
          .addRow("id2", CONTENT2)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .skipInvalidRows(SKIP_INVALID_ROWS)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST10 =
      InsertAllRequest.builder(TABLE_INFO)
          .addRow("id1", CONTENT1)
          .addRow("id2", CONTENT2)
          .ignoreUnknownValues(true)
          .skipInvalidRows(false)
          .build();
  private static final InsertAllRequest INSERT_ALL_REQUEST11 =
      InsertAllRequest.builder(TABLE_INFO)
          .addRow("id1", CONTENT1)
          .addRow("id2", CONTENT2)
          .ignoreUnknownValues(true)
          .skipInvalidRows(false)
          .templateSuffix(TEMPLATE_SUFFIX)
          .build();

  @Test
  public void testBuilder() {
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST1.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST2.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST3.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST4.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST5.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST6.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST7.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST8.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST9.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST10.table());
    assertEquals(TABLE_ID, INSERT_ALL_REQUEST11.table());
    assertEquals(ROWS, INSERT_ALL_REQUEST1.rows());
    assertEquals(ROWS, INSERT_ALL_REQUEST2.rows());
    assertEquals(ROWS, INSERT_ALL_REQUEST4.rows());
    assertEquals(ROWS, INSERT_ALL_REQUEST6.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST3.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST5.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST7.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST8.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST9.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST10.rows());
    assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST11.rows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST1.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST2.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST3.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST4.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST5.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST6.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST7.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST8.skipInvalidRows());
    assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST9.skipInvalidRows());
    assertFalse(INSERT_ALL_REQUEST10.skipInvalidRows());
    assertFalse(INSERT_ALL_REQUEST11.skipInvalidRows());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST1.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST2.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST3.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST4.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST5.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST6.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST7.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST8.ignoreUnknownValues());
    assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST9.ignoreUnknownValues());
    assertTrue(INSERT_ALL_REQUEST10.ignoreUnknownValues());
    assertTrue(INSERT_ALL_REQUEST11.ignoreUnknownValues());
    assertNull(INSERT_ALL_REQUEST1.templateSuffix());
    assertNull(INSERT_ALL_REQUEST2.templateSuffix());
    assertNull(INSERT_ALL_REQUEST3.templateSuffix());
    assertNull(INSERT_ALL_REQUEST4.templateSuffix());
    assertNull(INSERT_ALL_REQUEST5.templateSuffix());
    assertNull(INSERT_ALL_REQUEST6.templateSuffix());
    assertNull(INSERT_ALL_REQUEST7.templateSuffix());
    assertNull(INSERT_ALL_REQUEST8.templateSuffix());
    assertNull(INSERT_ALL_REQUEST9.templateSuffix());
    assertNull(INSERT_ALL_REQUEST10.templateSuffix());
    assertEquals(TEMPLATE_SUFFIX, INSERT_ALL_REQUEST11.templateSuffix());
  }

  @Test
  public void testOf() {
    InsertAllRequest request = InsertAllRequest.of(TABLE_ID, ROWS);
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
    request = InsertAllRequest.of(TABLE_INFO, ROWS);
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
    request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS);
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
    request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS);
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
    request = InsertAllRequest.of(TABLE_ID, ROWS.get(0), ROWS.get(1));
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
    request = InsertAllRequest.of(TABLE_INFO, ROWS.get(0), ROWS.get(1));
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
    request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS.get(0), ROWS.get(1));
    assertEquals(TABLE_ID, request.table());
    assertEquals(ROWS, request.rows());
  }

  @Test
  public void testEquals() {
    compareInsertAllRequest(INSERT_ALL_REQUEST1, INSERT_ALL_REQUEST2);
    compareInsertAllRequest(INSERT_ALL_REQUEST2, INSERT_ALL_REQUEST4);
    compareInsertAllRequest(INSERT_ALL_REQUEST3, INSERT_ALL_REQUEST5);
    compareInsertAllRequest(INSERT_ALL_REQUEST4, INSERT_ALL_REQUEST6);
    compareInsertAllRequest(INSERT_ALL_REQUEST5, INSERT_ALL_REQUEST7);
    compareInsertAllRequest(INSERT_ALL_REQUEST7, INSERT_ALL_REQUEST8);
    compareInsertAllRequest(INSERT_ALL_REQUEST8, INSERT_ALL_REQUEST9);
    compareInsertAllRequest(INSERT_ALL_REQUEST10, INSERT_ALL_REQUEST10);
    compareInsertAllRequest(INSERT_ALL_REQUEST11, INSERT_ALL_REQUEST11);
  }

  private void compareInsertAllRequest(InsertAllRequest expected, InsertAllRequest value) {
    assertEquals(expected, value);
    assertEquals(expected.toString(), value.toString());
    assertEquals(expected.hashCode(), value.hashCode());
    assertEquals(expected.table(), value.table());
    assertEquals(expected.rows(), value.rows());
    assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues());
    assertEquals(expected.skipInvalidRows(), value.skipInvalidRows());
    assertEquals(expected.templateSuffix(), value.templateSuffix());
  }
}
コード例 #13
0
ファイル: TableTest.java プロジェクト: mderka/gcloud-java
 @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()));
 }
コード例 #14
0
ファイル: TableTest.java プロジェクト: mderka/gcloud-java
public class TableTest {

  private static final TableId TABLE_ID1 = TableId.of("dataset", "table1");
  private static final TableId TABLE_ID2 = TableId.of("dataset", "table2");
  private static final JobInfo COPY_JOB_INFO = CopyJobInfo.of(TABLE_ID2, TABLE_ID1);
  private static final JobInfo LOAD_JOB_INFO =
      LoadJobInfo.builder(
              LoadConfiguration.builder(TABLE_ID1).formatOptions(FormatOptions.json()).build(),
              ImmutableList.of("URI"))
          .build();
  private static final JobInfo EXTRACT_JOB_INFO =
      ExtractJobInfo.builder(TABLE_ID1, ImmutableList.of("URI")).format("CSV").build();
  private static final Field FIELD = Field.of("FieldName", Field.Type.integer());
  private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, Schema.of(FIELD));
  private static final List<RowToInsert> ROWS_TO_INSERT =
      ImmutableList.of(
          RowToInsert.of("id1", ImmutableMap.<String, Object>of("key", "val1")),
          RowToInsert.of("id2", ImmutableMap.<String, Object>of("key", "val2")));
  private static final InsertAllRequest INSERT_ALL_REQUEST =
      InsertAllRequest.of(TABLE_ID1, ROWS_TO_INSERT);
  private static final InsertAllRequest INSERT_ALL_REQUEST_COMPLETE =
      InsertAllRequest.builder(TABLE_ID1, ROWS_TO_INSERT)
          .skipInvalidRows(true)
          .ignoreUnknownValues(true)
          .build();
  private static final InsertAllResponse EMPTY_INSERT_ALL_RESPONSE =
      new InsertAllResponse(ImmutableMap.<Long, List<BigQueryError>>of());
  private static final FieldValue FIELD_VALUE1 =
      new FieldValue(FieldValue.Attribute.PRIMITIVE, "val1");
  private static final FieldValue FIELD_VALUE2 =
      new FieldValue(FieldValue.Attribute.PRIMITIVE, "val1");
  private static final Iterable<List<FieldValue>> ROWS =
      ImmutableList.of(
          (List<FieldValue>) ImmutableList.of(FIELD_VALUE1), ImmutableList.of(FIELD_VALUE2));

  @Rule public ExpectedException thrown = ExpectedException.none();
  private BigQuery bigquery;
  private Table table;

  @Before
  public void setUp() throws Exception {
    bigquery = createStrictMock(BigQuery.class);
    table = new Table(bigquery, TABLE_INFO);
  }

  @After
  public void tearDown() throws Exception {
    verify(bigquery);
  }

  @Test
  public void testInfo() throws Exception {
    assertEquals(TABLE_INFO, table.info());
    replay(bigquery);
  }

  @Test
  public void testBigQuery() throws Exception {
    assertSame(bigquery, table.bigquery());
    replay(bigquery);
  }

  @Test
  public void testExists_True() throws Exception {
    BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()};
    expect(bigquery.getTable(TABLE_INFO.tableId(), expectedOptions)).andReturn(TABLE_INFO);
    replay(bigquery);
    assertTrue(table.exists());
  }

  @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());
  }

  @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());
  }

  @Test
  public void testReloadNull() throws Exception {
    expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
    replay(bigquery);
    assertNull(table.reload());
  }

  @Test
  public void testReloadWithOptions() throws Exception {
    TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
    expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
        .andReturn(updatedInfo);
    replay(bigquery);
    Table updatedTable = table.reload(BigQuery.TableOption.fields());
    assertSame(bigquery, updatedTable.bigquery());
    assertEquals(updatedInfo, updatedTable.info());
  }

  @Test
  public void testUpdate() throws Exception {
    BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
    expect(bigquery.update(updatedInfo)).andReturn(updatedInfo);
    replay(bigquery);
    Table updatedTable = table.update(updatedInfo);
    assertSame(bigquery, updatedTable.bigquery());
    assertEquals(updatedInfo, updatedTable.info());
  }

  @Test
  public void testUpdateWithDifferentId() throws Exception {
    TableInfo updatedInfo =
        TABLE_INFO
            .toBuilder()
            .tableId(TableId.of("dataset", "table3"))
            .description("Description")
            .build();
    replay(bigquery);
    thrown.expect(IllegalArgumentException.class);
    table.update(updatedInfo);
  }

  @Test
  public void testUpdateWithDifferentDatasetId() throws Exception {
    TableInfo updatedInfo =
        TABLE_INFO
            .toBuilder()
            .tableId(TableId.of("dataset1", "table1"))
            .description("Description")
            .build();
    replay(bigquery);
    thrown.expect(IllegalArgumentException.class);
    table.update(updatedInfo);
  }

  @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());
  }

  @Test
  public void testDelete() throws Exception {
    expect(bigquery.delete(TABLE_INFO.tableId())).andReturn(true);
    replay(bigquery);
    assertTrue(table.delete());
  }

  @Test
  public void testInsert() throws Exception {
    expect(bigquery.insertAll(INSERT_ALL_REQUEST)).andReturn(EMPTY_INSERT_ALL_RESPONSE);
    replay(bigquery);
    InsertAllResponse response = table.insert(ROWS_TO_INSERT);
    assertSame(EMPTY_INSERT_ALL_RESPONSE, response);
  }

  @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);
  }

  @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));
  }

  @Test
  public void testListWithOptions() throws Exception {
    PageImpl<List<FieldValue>> tableDataPage = new PageImpl<>(null, "c", ROWS);
    expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.maxResults(10L)))
        .andReturn(tableDataPage);
    replay(bigquery);
    Page<List<FieldValue>> dataPage = table.list(BigQuery.TableDataListOption.maxResults(10L));
    Iterator<List<FieldValue>> tableDataIterator = tableDataPage.values().iterator();
    Iterator<List<FieldValue>> dataIterator = dataPage.values().iterator();
    assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator));
  }

  @Test
  public void testCopyFromString() throws Exception {
    expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO);
    replay(bigquery);
    Job job = table.copy(TABLE_ID2.dataset(), TABLE_ID2.table());
    assertSame(bigquery, job.bigquery());
    assertEquals(COPY_JOB_INFO, job.info());
  }

  @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());
  }

  @Test
  public void testLoadDataUri() throws Exception {
    expect(bigquery.create(LOAD_JOB_INFO)).andReturn(LOAD_JOB_INFO);
    replay(bigquery);
    Job job = table.load(FormatOptions.json(), "URI");
    assertSame(bigquery, job.bigquery());
    assertEquals(LOAD_JOB_INFO, job.info());
  }

  @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());
  }

  @Test
  public void testExtractDataUri() throws Exception {
    expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(EXTRACT_JOB_INFO);
    replay(bigquery);
    Job job = table.extract("CSV", "URI");
    assertSame(bigquery, job.bigquery());
    assertEquals(EXTRACT_JOB_INFO, job.info());
  }

  @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());
  }

  @Test
  public void testGetFromId() throws Exception {
    expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(TABLE_INFO);
    replay(bigquery);
    Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId());
    assertNotNull(loadedTable);
    assertEquals(TABLE_INFO, loadedTable.info());
  }

  @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());
  }

  @Test
  public void testGetFromIdNull() throws Exception {
    expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
    replay(bigquery);
    assertNull(Table.get(bigquery, TABLE_INFO.tableId()));
  }

  @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()));
  }

  @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());
  }

  @Test
  public void testGetFromStringsWithOptions() throws Exception {
    expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
        .andReturn(TABLE_INFO);
    replay(bigquery);
    Table loadedTable =
        Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table(), BigQuery.TableOption.fields());
    assertNotNull(loadedTable);
    assertEquals(TABLE_INFO, loadedTable.info());
  }
}
コード例 #15
0
ファイル: Table.java プロジェクト: vad-babushkin/gcloud-java
 /**
  * Starts a BigQuery Job to copy the current table to the provided destination table. Returns the
  * started {@link Job} object.
  *
  * @param destinationDataset the user-defined id of the destination dataset
  * @param destinationTable the user-defined id of the destination table
  * @param options job options
  * @throws BigQueryException upon failure
  */
 Job copy(String destinationDataset, String destinationTable, BigQuery.JobOption... options)
     throws BigQueryException {
   return copy(TableId.of(destinationDataset, destinationTable), options);
 }
コード例 #16
0
public class WriteChannelConfigurationTest {

  private static final CsvOptions CSV_OPTIONS =
      CsvOptions.builder()
          .allowJaggedRows(true)
          .allowQuotedNewLines(false)
          .encoding(StandardCharsets.UTF_8)
          .build();
  private static final TableId TABLE_ID = TableId.of("dataset", "table");
  private static final CreateDisposition CREATE_DISPOSITION = CreateDisposition.CREATE_IF_NEEDED;
  private static final WriteDisposition WRITE_DISPOSITION = WriteDisposition.WRITE_APPEND;
  private static final Integer MAX_BAD_RECORDS = 42;
  private static final String FORMAT = "CSV";
  private static final Boolean IGNORE_UNKNOWN_VALUES = true;
  private static final List<String> PROJECTION_FIELDS = ImmutableList.of("field1", "field2");
  private static final Field FIELD_SCHEMA =
      Field.builder("IntegerField", Field.Type.integer())
          .mode(Field.Mode.REQUIRED)
          .description("FieldDescription")
          .build();
  private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA);
  private static final WriteChannelConfiguration LOAD_CONFIGURATION =
      WriteChannelConfiguration.builder(TABLE_ID)
          .createDisposition(CREATE_DISPOSITION)
          .writeDisposition(WRITE_DISPOSITION)
          .formatOptions(CSV_OPTIONS)
          .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
          .maxBadRecords(MAX_BAD_RECORDS)
          .projectionFields(PROJECTION_FIELDS)
          .schema(TABLE_SCHEMA)
          .build();

  @Test
  public void testToBuilder() {
    compareLoadConfiguration(LOAD_CONFIGURATION, LOAD_CONFIGURATION.toBuilder().build());
    WriteChannelConfiguration configuration =
        LOAD_CONFIGURATION.toBuilder().destinationTable(TableId.of("dataset", "newTable")).build();
    assertEquals("newTable", configuration.destinationTable().table());
    configuration = configuration.toBuilder().destinationTable(TABLE_ID).build();
    compareLoadConfiguration(LOAD_CONFIGURATION, configuration);
  }

  @Test
  public void testOf() {
    WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID);
    assertEquals(TABLE_ID, configuration.destinationTable());
    configuration = WriteChannelConfiguration.of(TABLE_ID, CSV_OPTIONS);
    assertEquals(TABLE_ID, configuration.destinationTable());
    assertEquals(FORMAT, configuration.format());
    assertEquals(CSV_OPTIONS, configuration.csvOptions());
  }

  @Test
  public void testToBuilderIncomplete() {
    WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID);
    compareLoadConfiguration(configuration, configuration.toBuilder().build());
  }

  @Test
  public void testBuilder() {
    assertEquals(TABLE_ID, LOAD_CONFIGURATION.destinationTable());
    assertEquals(CREATE_DISPOSITION, LOAD_CONFIGURATION.createDisposition());
    assertEquals(WRITE_DISPOSITION, LOAD_CONFIGURATION.writeDisposition());
    assertEquals(CSV_OPTIONS, LOAD_CONFIGURATION.csvOptions());
    assertEquals(FORMAT, LOAD_CONFIGURATION.format());
    assertEquals(IGNORE_UNKNOWN_VALUES, LOAD_CONFIGURATION.ignoreUnknownValues());
    assertEquals(MAX_BAD_RECORDS, LOAD_CONFIGURATION.maxBadRecords());
    assertEquals(PROJECTION_FIELDS, LOAD_CONFIGURATION.projectionFields());
    assertEquals(TABLE_SCHEMA, LOAD_CONFIGURATION.schema());
  }

  @Test
  public void testToPbAndFromPb() {
    assertNull(LOAD_CONFIGURATION.toPb().getLoad().getSourceUris());
    compareLoadConfiguration(
        LOAD_CONFIGURATION, WriteChannelConfiguration.fromPb(LOAD_CONFIGURATION.toPb()));
    WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID);
    compareLoadConfiguration(configuration, WriteChannelConfiguration.fromPb(configuration.toPb()));
  }

  private void compareLoadConfiguration(
      WriteChannelConfiguration expected, WriteChannelConfiguration value) {
    assertEquals(expected, value);
    assertEquals(expected.hashCode(), value.hashCode());
    assertEquals(expected.toString(), value.toString());
    assertEquals(expected.destinationTable(), value.destinationTable());
    assertEquals(expected.createDisposition(), value.createDisposition());
    assertEquals(expected.writeDisposition(), value.writeDisposition());
    assertEquals(expected.csvOptions(), value.csvOptions());
    assertEquals(expected.format(), value.format());
    assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues());
    assertEquals(expected.maxBadRecords(), value.maxBadRecords());
    assertEquals(expected.projectionFields(), value.projectionFields());
    assertEquals(expected.schema(), value.schema());
  }
}
コード例 #17
0
ファイル: Table.java プロジェクト: vad-babushkin/gcloud-java
 /**
  * Creates a {@code Table} object for the provided dataset and table's user-defined ids. Performs
  * an RPC call to get the latest table information.
  *
  * @param bigquery the BigQuery service used for issuing requests
  * @param dataset the dataset's user-defined id
  * @param table the table's user-defined id
  * @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, String dataset, String table, BigQuery.TableOption... options) {
   return get(bigquery, TableId.of(dataset, table), options);
 }