@Test public void testToPbAndFromPb() { assertNotNull(EXTRACT_CONFIGURATION.toPb().getExtract()); assertNull(EXTRACT_CONFIGURATION.toPb().getCopy()); assertNull(EXTRACT_CONFIGURATION.toPb().getLoad()); assertNull(EXTRACT_CONFIGURATION.toPb().getQuery()); compareExtractJobConfiguration( EXTRACT_CONFIGURATION, ExtractJobConfiguration.fromPb(EXTRACT_CONFIGURATION.toPb())); compareExtractJobConfiguration( EXTRACT_CONFIGURATION_ONE_URI, ExtractJobConfiguration.fromPb(EXTRACT_CONFIGURATION_ONE_URI.toPb())); ExtractJobConfiguration job = ExtractJobConfiguration.of(TABLE_ID, DESTINATION_URIS); compareExtractJobConfiguration(job, ExtractJobConfiguration.fromPb(job.toPb())); }
@Override ExtractJobConfiguration setProjectId(String projectId) { if (Strings.isNullOrEmpty(getSourceTable().getProject())) { return toBuilder().setSourceTable(getSourceTable().setProjectId(projectId)).build(); } return this; }
public ExtractJobConfiguration build() { return new ExtractJobConfiguration(this); } }
private void compareExtractJobConfiguration( ExtractJobConfiguration expected, ExtractJobConfiguration value) { assertEquals(expected, value); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); assertEquals(expected.getSourceTable(), value.getSourceTable()); assertEquals(expected.getDestinationUris(), value.getDestinationUris()); assertEquals(expected.getCompression(), value.getCompression()); assertEquals(expected.printHeader(), value.printHeader()); assertEquals(expected.getFieldDelimiter(), value.getFieldDelimiter()); assertEquals(expected.getFormat(), value.getFormat()); } }
@Test public void testOf() { ExtractJobConfiguration job = ExtractJobConfiguration.of(TABLE_ID, DESTINATION_URIS); assertEquals(TABLE_ID, job.getSourceTable()); assertEquals(DESTINATION_URIS, job.getDestinationUris()); job = ExtractJobConfiguration.of(TABLE_ID, DESTINATION_URI); assertEquals(TABLE_ID, job.getSourceTable()); assertEquals(ImmutableList.of(DESTINATION_URI), job.getDestinationUris()); job = ExtractJobConfiguration.of(TABLE_ID, DESTINATION_URIS, JSON_FORMAT); assertEquals(TABLE_ID, job.getSourceTable()); assertEquals(DESTINATION_URIS, job.getDestinationUris()); assertEquals(JSON_FORMAT, job.getFormat()); job = ExtractJobConfiguration.of(TABLE_ID, DESTINATION_URI, JSON_FORMAT); assertEquals(TABLE_ID, job.getSourceTable()); assertEquals(ImmutableList.of(DESTINATION_URI), job.getDestinationUris()); assertEquals(JSON_FORMAT, job.getFormat()); }
@Test public void testBuilder() { assertEquals(TABLE_ID, EXTRACT_CONFIGURATION.getSourceTable()); assertEquals(DESTINATION_URIS, EXTRACT_CONFIGURATION.getDestinationUris()); assertEquals(FIELD_DELIMITER, EXTRACT_CONFIGURATION.getFieldDelimiter()); assertEquals(COMPRESSION, EXTRACT_CONFIGURATION.getCompression()); assertEquals(PRINT_HEADER, EXTRACT_CONFIGURATION.printHeader()); assertEquals(FORMAT, EXTRACT_CONFIGURATION.getFormat()); assertEquals(TABLE_ID, EXTRACT_CONFIGURATION_ONE_URI.getSourceTable()); assertEquals( ImmutableList.of(DESTINATION_URI), EXTRACT_CONFIGURATION_ONE_URI.getDestinationUris()); assertEquals(FIELD_DELIMITER, EXTRACT_CONFIGURATION_ONE_URI.getFieldDelimiter()); assertEquals(COMPRESSION, EXTRACT_CONFIGURATION_ONE_URI.getCompression()); assertEquals(PRINT_HEADER, EXTRACT_CONFIGURATION_ONE_URI.printHeader()); assertEquals(FORMAT, EXTRACT_CONFIGURATION_ONE_URI.getFormat()); }
@Test public void testSetProjectIdDoNotOverride() { ExtractJobConfiguration configuration = EXTRACT_CONFIGURATION .toBuilder() .setSourceTable(TABLE_ID.setProjectId(TEST_PROJECT_ID)) .build() .setProjectId("do-not-update"); assertEquals(TEST_PROJECT_ID, configuration.getSourceTable().getProject()); }
/** * Creates a builder for a BigQuery Extract Job configuration given source table and destination * URI. */ public static Builder newBuilder(TableId sourceTable, String destinationUri) { return newBuilder(sourceTable, ImmutableList.of(checkNotNull(destinationUri))); }
@Override JobInfo parse(String... args) throws Exception { if (args.length >= 4) { String dataset = args[0]; String table = args[1]; String format = args[2]; TableId tableId = TableId.of(dataset, table); ExtractJobConfiguration configuration = ExtractJobConfiguration.of( tableId, Arrays.asList(args).subList(3, args.length), format); return JobInfo.of(configuration); } throw new IllegalArgumentException("Missing required arguments."); }
@SuppressWarnings("unchecked") static <T extends JobConfiguration> T fromPb( com.google.api.services.bigquery.model.JobConfiguration configurationPb) { if (configurationPb.getCopy() != null) { return (T) CopyJobConfiguration.fromPb(configurationPb); } else if (configurationPb.getExtract() != null) { return (T) ExtractJobConfiguration.fromPb(configurationPb); } else if (configurationPb.getLoad() != null) { return (T) LoadJobConfiguration.fromPb(configurationPb); } else if (configurationPb.getQuery() != null) { return (T) QueryJobConfiguration.fromPb(configurationPb); } else { // never reached throw new IllegalArgumentException("Job configuration is not supported"); } } }
@Override ExtractJobConfiguration setProjectId(String projectId) { return toBuilder().sourceTable(sourceTable().setProjectId(projectId)).build(); }
@Test public void testSetProjectId() { ExtractJobConfiguration configuration = EXTRACT_CONFIGURATION.setProjectId(TEST_PROJECT_ID); assertEquals(TEST_PROJECT_ID, configuration.getSourceTable().getProject()); }
@Override public int hashCode() { return Objects.hash( baseHashCode(), sourceTable, destinationUris, printHeader, fieldDelimiter, format, compression); }
@Override public boolean equals(Object obj) { return obj == this || obj instanceof ExtractJobConfiguration && baseEquals((ExtractJobConfiguration) obj); }
/** * Creates a builder for a BigQuery Extract Job configuration given source table and destination * URI. */ public static Builder builder(TableId sourceTable, String destinationUri) { return builder(sourceTable, ImmutableList.of(checkNotNull(destinationUri))); }
@Test public void testSetProjectId() { JobInfo jobInfo = COPY_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); CopyJobConfiguration copyConfiguration = jobInfo.getConfiguration(); assertEquals("p", copyConfiguration.getDestinationTable().getProject()); for (TableId sourceTable : copyConfiguration.getSourceTables()) { assertEquals("p", sourceTable.getProject()); } jobInfo = EXTRACT_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); ExtractJobConfiguration extractConfiguration = jobInfo.getConfiguration(); assertEquals("p", extractConfiguration.getSourceTable().getProject()); jobInfo = LOAD_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); LoadJobConfiguration loadConfiguration = jobInfo.getConfiguration(); assertEquals("p", loadConfiguration.getDestinationTable().getProject()); jobInfo = QUERY_JOB.setProjectId("p"); assertEquals("p", jobInfo.getJobId().getProject()); QueryJobConfiguration queryConfiguration = jobInfo.getConfiguration(); assertEquals("p", queryConfiguration.getDefaultDataset().getProject()); assertEquals("p", queryConfiguration.getDestinationTable().getProject()); }
/** * Returns a BigQuery Extract Job configuration for the given source table and destination URIs. */ public static ExtractJobConfiguration of(TableId sourceTable, List<String> destinationUris) { return newBuilder(sourceTable, destinationUris).build(); }
throws BigQueryException { ExtractJobConfiguration extractConfiguration = ExtractJobConfiguration.of(getTableId(), destinationUris, format); return bigquery.create(JobInfo.of(extractConfiguration), options);
@SuppressWarnings("unchecked") static <T extends JobConfiguration> T fromPb( com.google.api.services.bigquery.model.JobConfiguration configurationPb) { if (configurationPb.getCopy() != null) { return (T) CopyJobConfiguration.fromPb(configurationPb); } else if (configurationPb.getExtract() != null) { return (T) ExtractJobConfiguration.fromPb(configurationPb); } else if (configurationPb.getLoad() != null) { return (T) LoadJobConfiguration.fromPb(configurationPb); } else if (configurationPb.getQuery() != null) { return (T) QueryJobConfiguration.fromPb(configurationPb); } else { // never reached throw new IllegalArgumentException("Job configuration is not supported"); } } }