Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
    public void ExtractTable(
        string projectId  = "your-project-id",
        string bucketName = "your-bucket-name")
    {
        BigQueryClient client = BigQueryClient.Create(projectId);
        // Define a destination URI. Use a single wildcard URI if you think
        // your exported data will be larger than the 1 GB maximum value.
        string      destinationUri = $"gs://{bucketName}/shakespeare-*.csv";
        BigQueryJob job            = client.CreateExtractJob(
            projectId: "bigquery-public-data",
            datasetId: "samples",
            tableId: "shakespeare",
            destinationUri: destinationUri
            );

        job.PollUntilCompleted();  // Waits for the job to complete.
        Console.Write($"Exported table to {destinationUri}.");
    }
Ejemplo n.º 3
0
        public static async Task ExportBigQueryTableToStorageAsync(BigQueryClient bigQueryClient, string destinationUri, BigQueryResults results)
        {
            CreateExtractJobOptions jobOptions = new CreateExtractJobOptions()
            {
                DestinationFormat = FileFormat.Csv,
                Compression       = CompressionType.Gzip
            };

            BigQueryJob job = bigQueryClient.CreateExtractJob(
                projectId: results.TableReference.ProjectId,
                datasetId: results.TableReference.DatasetId,
                tableId: results.TableReference.TableId,
                destinationUri: destinationUri,
                options: jobOptions
                );

            await job.PollUntilCompletedAsync();
        }
    public void ExtractTableJson(
        string projectId  = "your-project-id",
        string bucketName = "your-bucket-name")
    {
        BigQueryClient client         = BigQueryClient.Create(projectId);
        string         destinationUri = $"gs://{bucketName}/shakespeare.json";
        var            jobOptions     = new CreateExtractJobOptions()
        {
            DestinationFormat = FileFormat.NewlineDelimitedJson
        };
        BigQueryJob job = client.CreateExtractJob(
            projectId: "bigquery-public-data",
            datasetId: "samples",
            tableId: "shakespeare",
            destinationUri: destinationUri,
            options: jobOptions
            );

        job = job.PollUntilCompleted().ThrowOnAnyError();  // Waits for the job to complete.
        Console.Write($"Exported table to {destinationUri}.");
    }