Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ private DatasetListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}

/**
* Returns an option to specify a label filter. See
* https://cloud.google.com/bigquery/docs/adding-using-labels#filtering_datasets_using_labels
*/
public static DatasetListOption labelFilter(String labelFilter) {
return new DatasetListOption(BigQueryRpc.Option.LABEL_FILTER, labelFilter);
}

/** Returns an option to specify the maximum number of datasets returned per page. */
public static DatasetListOption pageSize(long pageSize) {
return new DatasetListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -387,6 +395,16 @@ public String apply(JobStatus.State state) {
return new JobListOption(BigQueryRpc.Option.STATE_FILTER, stringFilters);
}

/** Returns an option to filter out jobs before the given minimum creation time. */
public static JobListOption minCreationTime(long minCreationTime) {
return new JobListOption(BigQueryRpc.Option.MIN_CREATION_TIME, minCreationTime);
}

/** Returns an option to filter out jobs after the given maximum creation time. */
public static JobListOption maxCreationTime(long maxCreationTime) {
return new JobListOption(BigQueryRpc.Option.MAX_CREATION_TIME, maxCreationTime);
}

/** Returns an option to specify the maximum number of jobs returned per page. */
public static JobListOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ enum Option {
DELETE_CONTENTS("deleteContents"),
ALL_DATASETS("all"),
ALL_USERS("allUsers"),
LABEL_FILTER("filter"),
MIN_CREATION_TIME("minCreationTime"),
MAX_CREATION_TIME("maxCreationTime"),
MAX_RESULTS("maxResults"),
PAGE_TOKEN("pageToken"),
START_INDEX("startIndex"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public Tuple<String, Iterable<Dataset>> listDatasets(String projectId, Map<Optio
.datasets()
.list(projectId)
.setAll(Option.ALL_DATASETS.getBoolean(options))
.setFilter(Option.LABEL_FILTER.getString(options))
.setMaxResults(Option.MAX_RESULTS.getLong(options))
.setPageToken(Option.PAGE_TOKEN.getString(options))
.setPageToken(Option.PAGE_TOKEN.getString(options))
Expand Down Expand Up @@ -418,7 +419,7 @@ public Job getJob(String projectId, String jobId, String location, Map<Option, ?
@Override
public Tuple<String, Iterable<Job>> listJobs(String projectId, Map<Option, ?> options) {
try {
JobList jobsList =
Bigquery.Jobs.List request =
bigquery
.jobs()
.list(projectId)
Expand All @@ -427,8 +428,15 @@ public Tuple<String, Iterable<Job>> listJobs(String projectId, Map<Option, ?> op
.setStateFilter(Option.STATE_FILTER.<List<String>>get(options))
.setMaxResults(Option.MAX_RESULTS.getLong(options))
.setPageToken(Option.PAGE_TOKEN.getString(options))
.setProjection(DEFAULT_PROJECTION)
.execute();
.setProjection(DEFAULT_PROJECTION);
if (Option.MIN_CREATION_TIME.getLong(options) != null) {
request.setMinCreationTime(BigInteger.valueOf(Option.MIN_CREATION_TIME.getLong(options)));
}
if (Option.MAX_CREATION_TIME.getLong(options) != null) {
request.setMaxCreationTime(BigInteger.valueOf(Option.MAX_CREATION_TIME.getLong(options)));
}
JobList jobsList = request.execute();

Iterable<JobList.Jobs> jobs = jobsList.getJobs();
return Tuple.of(
jobsList.getNextPageToken(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.cloud.bigquery.JobStatus.State.DONE;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.System.currentTimeMillis;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -32,6 +33,7 @@
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQuery.DatasetField;
import com.google.cloud.bigquery.BigQuery.DatasetListOption;
import com.google.cloud.bigquery.BigQuery.DatasetOption;
import com.google.cloud.bigquery.BigQuery.JobField;
import com.google.cloud.bigquery.BigQuery.JobListOption;
Expand Down Expand Up @@ -324,6 +326,22 @@ public void testListDatasets() {
}
}

@Test
public void testListDatasetsWithFilter() {
String labelFilter = "labels.example-label1:example-value1";
Page<Dataset> datasets = bigquery.listDatasets(DatasetListOption.labelFilter(labelFilter));
int count = 0;
for (Dataset dataset : datasets.getValues()) {
assertTrue(
"failed to find label key in dataset", dataset.getLabels().containsKey("example-label1"));
assertTrue(
"failed to find label value in dataset",
dataset.getLabels().get("example-label1").equals("example-value1"));
count++;
}
assertTrue(count > 0);
}

@Test
public void testGetDataset() {
Dataset dataset = bigquery.getDataset(DATASET);
Expand Down Expand Up @@ -1243,6 +1261,29 @@ public void testListJobsWithSelectedFields() {
}
}

@Test
public void testListJobsWithCreationBounding() {
long currentMillis = currentTimeMillis();
long lowerBound = currentMillis - 3600 * 1000;
long upperBound = currentMillis;
Page<Job> jobs =
bigquery.listJobs(
JobListOption.minCreationTime(lowerBound), JobListOption.maxCreationTime(upperBound));
long foundMin = upperBound;
long foundMax = lowerBound;
long jobCount = 0;
for (Job job : jobs.getValues()) {
jobCount++;
foundMin = Math.min(job.getStatistics().getCreationTime(), foundMin);
foundMax = Math.max(job.getStatistics().getCreationTime(), foundMax);
}
assertTrue(
"Found min job time " + foundMin + " earlier than " + lowerBound, foundMin >= lowerBound);
assertTrue(
"Found max job time " + foundMax + " later than " + upperBound, foundMax <= upperBound);
assertTrue("no jobs listed", jobCount > 0);
}

@Test
public void testCreateAndGetJob() throws InterruptedException, TimeoutException {
String sourceTableName = "test_create_and_get_job_source_table";
Expand Down