Skip to content

Commit 5da46e8

Browse files
committed
Merge pull request #527 from mziccard/add-format-to-of
Add format parameter to ExtractJobInfo and LoadJobInfo factory methods
2 parents 8d37c32 + c3e808d commit 5da46e8

File tree

6 files changed

+117
-10
lines changed

6 files changed

+117
-10
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExtractJobInfo.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ public static ExtractJobInfo of(TableId sourceTable, List<String> destinationUri
263263
return builder(sourceTable, destinationUris).build();
264264
}
265265

266+
/**
267+
* Returns a BigQuery Extract Job for the given source table, format and destination URI. Job's id
268+
* is chosen by the service.
269+
*/
270+
public static ExtractJobInfo of(TableId sourceTable, String format, String destinationUri) {
271+
return builder(sourceTable, destinationUri).format(format).build();
272+
}
273+
274+
/**
275+
* Returns a BigQuery Extract Job for the given source table, format and destination URIs. Job's
276+
* id is chosen by the service.
277+
*/
278+
public static ExtractJobInfo of(TableId sourceTable, String format,
279+
List<String> destinationUris) {
280+
return builder(sourceTable, destinationUris).format(format).build();
281+
}
282+
266283
/**
267284
* Returns a BigQuery Extract Job for the given source table and destination URI. Job's id is set
268285
* to the provided value.
@@ -279,6 +296,24 @@ public static ExtractJobInfo of(JobId jobId, TableId sourceTable, List<String> d
279296
return builder(sourceTable, destinationUris).jobId(jobId).build();
280297
}
281298

299+
/**
300+
* Returns a BigQuery Extract Job for the given source table, format and destination URI. Job's id
301+
* is set to the provided value.
302+
*/
303+
public static ExtractJobInfo of(JobId jobId, TableId sourceTable, String format,
304+
String destinationUri) {
305+
return builder(sourceTable, destinationUri).format(format).jobId(jobId).build();
306+
}
307+
308+
/**
309+
* Returns a BigQuery Extract Job for the given source table, format and destination URIs. Job's
310+
* id is set to the provided value.
311+
*/
312+
public static ExtractJobInfo of(JobId jobId, TableId sourceTable, String format,
313+
List<String> destinationUris) {
314+
return builder(sourceTable, destinationUris).format(format).jobId(jobId).build();
315+
}
316+
282317
@SuppressWarnings("unchecked")
283318
static ExtractJobInfo fromPb(Job jobPb) {
284319
return new Builder(jobPb).build();

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/LoadJobInfo.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,23 @@ public static LoadJobInfo of(TableId destinationTable, List<String> sourceUris)
407407
return builder(destinationTable, sourceUris).build();
408408
}
409409

410+
/**
411+
* Returns a BigQuery Load Job for the given destination table, format and source URI. Job's id is
412+
* chosen by the service.
413+
*/
414+
public static LoadJobInfo of(TableId destinationTable, FormatOptions format, String sourceUri) {
415+
return builder(destinationTable, sourceUri).formatOptions(format).build();
416+
}
417+
418+
/**
419+
* Returns a BigQuery Load Job for the given destination table, format and source URIs. Job's id
420+
* is chosen by the service.
421+
*/
422+
public static LoadJobInfo of(TableId destinationTable, FormatOptions format,
423+
List<String> sourceUris) {
424+
return builder(destinationTable, sourceUris).formatOptions(format).build();
425+
}
426+
410427
/**
411428
* Returns a BigQuery Load Job for the given destination table and source URI. Job's id is set to
412429
* the provided value.
@@ -423,6 +440,24 @@ public static LoadJobInfo of(JobId jobId, TableId destinationTable, List<String>
423440
return builder(destinationTable, sourceUris).jobId(jobId).build();
424441
}
425442

443+
/**
444+
* Returns a BigQuery Load Job for the given destination table, format, and source URI. Job's id
445+
* is set to the provided value.
446+
*/
447+
public static LoadJobInfo of(JobId jobId, TableId destinationTable, FormatOptions format,
448+
String sourceUri) {
449+
return builder(destinationTable, sourceUri).formatOptions(format).jobId(jobId).build();
450+
}
451+
452+
/**
453+
* Returns a BigQuery Load Job for the given destination table, format and source URIs. Job's id
454+
* is set to the provided value.
455+
*/
456+
public static LoadJobInfo of(JobId jobId, TableId destinationTable, FormatOptions format,
457+
List<String> sourceUris) {
458+
return builder(destinationTable, sourceUris).formatOptions(format).jobId(jobId).build();
459+
}
460+
426461
@SuppressWarnings("unchecked")
427462
static LoadJobInfo fromPb(Job jobPb) {
428463
return new Builder(jobPb).build();

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryJobInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public enum Priority {
5353

5454
/**
5555
* Query is queued and started as soon as idle resources are available, usually within a few
56-
* minutes. If a {@link Priority#BATCH} query hasn't started within 3 hours, its priority is
57-
* changed to {@link Priority#INTERACTIVE}.
56+
* minutes. If the query hasn't started within 3 hours, its priority is changed to
57+
* {@link Priority#INTERACTIVE}.
5858
*/
5959
BATCH
6060
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,8 @@ Job extract(String format, String destinationUri, BigQuery.JobOption... options)
231231
*/
232232
Job extract(String format, List<String> destinationUris, BigQuery.JobOption... options)
233233
throws BigQueryException {
234-
ExtractJobInfo job = ExtractJobInfo.builder(info.tableId(), destinationUris)
235-
.format(format)
236-
.build();
237-
return new Job(bigquery, bigquery.create(job, options));
234+
return new Job(bigquery,
235+
bigquery.create(ExtractJobInfo.of(info.tableId(), format, destinationUris), options));
238236
}
239237

240238
/**
@@ -264,10 +262,8 @@ Job load(FormatOptions format, String sourceUri, BigQuery.JobOption... options)
264262
*/
265263
Job load(FormatOptions format, List<String> sourceUris, BigQuery.JobOption... options)
266264
throws BigQueryException {
267-
LoadJobInfo job = LoadJobInfo.builder(info.tableId(), sourceUris)
268-
.formatOptions(format)
269-
.build();
270-
return new Job(bigquery, bigquery.create(job, options));
265+
return new Job(bigquery, bigquery.create(LoadJobInfo.of(info.tableId(), format, sourceUris),
266+
options));
271267
}
272268

273269
/**

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExtractJobInfoTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class ExtractJobInfoTest {
3838
private static final TableId TABLE_ID = TableId.of("dataset", "table");
3939
private static final String FIELD_DELIMITER = ",";
4040
private static final String FORMAT = "CSV";
41+
private static final String JSON_FORMAT = "NEWLINE_DELIMITED_JSON";
4142
private static final Boolean PRINT_HEADER = true;
4243
private static final String COMPRESSION = "GZIP";
4344
private static final JobId JOB_ID = JobId.of("job");
@@ -95,6 +96,14 @@ public void testOf() {
9596
job = ExtractJobInfo.of(TABLE_ID, DESTINATION_URI);
9697
assertEquals(TABLE_ID, job.sourceTable());
9798
assertEquals(ImmutableList.of(DESTINATION_URI), job.destinationUris());
99+
job = ExtractJobInfo.of(TABLE_ID, JSON_FORMAT, DESTINATION_URIS);
100+
assertEquals(TABLE_ID, job.sourceTable());
101+
assertEquals(DESTINATION_URIS, job.destinationUris());
102+
assertEquals(JSON_FORMAT, job.format());
103+
job = ExtractJobInfo.of(TABLE_ID, JSON_FORMAT, DESTINATION_URI);
104+
assertEquals(TABLE_ID, job.sourceTable());
105+
assertEquals(ImmutableList.of(DESTINATION_URI), job.destinationUris());
106+
assertEquals(JSON_FORMAT, job.format());
98107
job = ExtractJobInfo.of(JOB_ID, TABLE_ID, DESTINATION_URIS);
99108
assertEquals(JOB_ID, job.jobId());
100109
assertEquals(TABLE_ID, job.sourceTable());
@@ -103,6 +112,16 @@ public void testOf() {
103112
assertEquals(JOB_ID, job.jobId());
104113
assertEquals(TABLE_ID, job.sourceTable());
105114
assertEquals(ImmutableList.of(DESTINATION_URI), job.destinationUris());
115+
job = ExtractJobInfo.of(JOB_ID, TABLE_ID, JSON_FORMAT, DESTINATION_URIS);
116+
assertEquals(JOB_ID, job.jobId());
117+
assertEquals(TABLE_ID, job.sourceTable());
118+
assertEquals(DESTINATION_URIS, job.destinationUris());
119+
assertEquals(JSON_FORMAT, job.format());
120+
job = ExtractJobInfo.of(JOB_ID, TABLE_ID, JSON_FORMAT, DESTINATION_URI);
121+
assertEquals(JOB_ID, job.jobId());
122+
assertEquals(TABLE_ID, job.sourceTable());
123+
assertEquals(ImmutableList.of(DESTINATION_URI), job.destinationUris());
124+
assertEquals(JSON_FORMAT, job.format());
106125
}
107126

108127
@Test

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/LoadJobInfoTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ public void testOf() {
102102
job = LoadJobInfo.of(TABLE_ID, SOURCE_URI);
103103
assertEquals(TABLE_ID, job.destinationTable());
104104
assertEquals(ImmutableList.of(SOURCE_URI), job.sourceUris());
105+
job = LoadJobInfo.of(TABLE_ID, CSV_OPTIONS, SOURCE_URIS);
106+
assertEquals(TABLE_ID, job.destinationTable());
107+
assertEquals(SOURCE_URIS, job.sourceUris());
108+
assertEquals(FORMAT, job.format());
109+
assertEquals(CSV_OPTIONS, job.csvOptions());
110+
job = LoadJobInfo.of(TABLE_ID, CSV_OPTIONS, SOURCE_URI);
111+
assertEquals(TABLE_ID, job.destinationTable());
112+
assertEquals(ImmutableList.of(SOURCE_URI), job.sourceUris());
113+
assertEquals(FORMAT, job.format());
114+
assertEquals(CSV_OPTIONS, job.csvOptions());
105115
job = LoadJobInfo.of(JOB_ID, TABLE_ID, SOURCE_URIS);
106116
assertEquals(JOB_ID, job.jobId());
107117
assertEquals(TABLE_ID, job.destinationTable());
@@ -110,6 +120,18 @@ public void testOf() {
110120
assertEquals(JOB_ID, job.jobId());
111121
assertEquals(TABLE_ID, job.destinationTable());
112122
assertEquals(ImmutableList.of(SOURCE_URI), job.sourceUris());
123+
job = LoadJobInfo.of(JOB_ID, TABLE_ID, CSV_OPTIONS, SOURCE_URIS);
124+
assertEquals(JOB_ID, job.jobId());
125+
assertEquals(TABLE_ID, job.destinationTable());
126+
assertEquals(SOURCE_URIS, job.sourceUris());
127+
assertEquals(FORMAT, job.format());
128+
assertEquals(CSV_OPTIONS, job.csvOptions());
129+
job = LoadJobInfo.of(JOB_ID, TABLE_ID, CSV_OPTIONS, SOURCE_URI);
130+
assertEquals(JOB_ID, job.jobId());
131+
assertEquals(TABLE_ID, job.destinationTable());
132+
assertEquals(ImmutableList.of(SOURCE_URI), job.sourceUris());
133+
assertEquals(FORMAT, job.format());
134+
assertEquals(CSV_OPTIONS, job.csvOptions());
113135
}
114136

115137
@Test

0 commit comments

Comments
 (0)