예제 #1
0
        public void ListJobs_FilterByLabels()
        {
            string bucketName = _fixture.StorageBucketName;
            string objectName = _fixture.GenerateStorageObjectName();

            string projectId = _fixture.ProjectId;
            string datasetId = _fixture.GameDatasetId;
            string tableId   = _fixture.HistoryTableId;

            // Snippet: Labels
            IDictionary <string, string> labels = new Dictionary <string, string>()
            {
                { "label-key", "label-value" }
            };

            BigQueryClient client         = BigQueryClient.Create(projectId);
            BigQueryTable  table          = client.GetTable(projectId, datasetId, tableId);
            string         destinationUri = $"gs://{bucketName}/{objectName}";

            // Just a couple examples of jobs marked with labels:
            // (These jobs will most certainly be created somewhere else.)
            // Running a query on a given table.
            BigQueryJob oneLabeledJob = client.CreateQueryJob(
                $"SELECT * FROM {table}", null,
                new QueryOptions {
                Labels = labels
            });
            // Extracting data from a table to GCS.
            BigQueryJob anotherLabeledJob = client.CreateExtractJob(
                projectId, datasetId, tableId, destinationUri,
                new CreateExtractJobOptions {
                Labels = labels
            });

            // Find jobs marked with a certain label.
            KeyValuePair <string, string> labelToBeFound = labels.First();
            // Specify full projection to make sure that
            // label information, if it exists, is returned for listed jobs.
            ListJobsOptions options = new ListJobsOptions {
                Projection = ProjectionEnum.Full
            };
            List <BigQueryJob> jobs = client
                                      .ListJobs(options)
                                      .Where(job => job.Resource.Configuration.Labels?.Contains(labelToBeFound) ?? false)
                                      .Take(2)
                                      .ToList();

            foreach (BigQueryJob job in jobs)
            {
                Console.WriteLine(job.Reference.JobId);
            }
            // End snippet

            // This test added two jobs with such labels, other tests might have
            // added more.
            Assert.True(jobs.Count >= 2);
        }