Exemplo n.º 1
0
        // [END sync_query]

        // [START sync_query_legacy_sql]
        public BigqueryQueryJob LegacySqlSyncQuery(string projectId, string datasetId,
                                                   string tableId, string query, double timeoutMs, BigqueryClient client)
        {
            var         table = client.GetTable(projectId, datasetId, tableId);
            BigqueryJob job   = client.CreateQueryJob(query,
                                                      new CreateQueryJobOptions {
                UseLegacySql = true
            });
            // Get the query result, waiting for the timespan specified in milliseconds.
            BigqueryQueryJob result = client.GetQueryResults(job.Reference.JobId,
                                                             new GetQueryResultsOptions {
                Timeout = TimeSpan.FromMilliseconds(timeoutMs)
            });

            return(result);
        }
Exemplo n.º 2
0
        // [END sync_query_legacy_sql]

        // [START async_query]
        public BigqueryQueryJob AsyncQuery(string projectId, string datasetId, string tableId,
                                           string query, BigqueryClient client)
        {
            var         table = client.GetTable(projectId, datasetId, tableId);
            BigqueryJob job   = client.CreateQueryJob(query,
                                                      new CreateQueryJobOptions {
                UseQueryCache = false
            });

            // Wait for the job to complete.
            job.PollUntilCompleted();

            // Then we can fetch the results, either via the job or by accessing
            // the destination table.
            return(client.GetQueryResults(job.Reference.JobId));
        }
        public void CreateQueryJob()
        {
            var projectId      = _fixture.ProjectId;
            var datasetId      = _fixture.GameDatasetId;
            var historyTableId = _fixture.HistoryTableId;
            var queryTableId   = Guid.NewGuid().ToString().Replace('-', '_');

            // Snippet: CreateQueryJob(*,*)
            BigqueryClient client      = BigqueryClient.Create(projectId);
            BigqueryTable  table       = client.GetTable(datasetId, historyTableId);
            TableReference destination = client.GetTableReference(datasetId, queryTableId);
            // If the destination table is not specified, the results will be stored in
            // a temporary table.
            BigqueryJob job = client.CreateQueryJob(
                $@"SELECT player, MAX(score) AS score
                   FROM {table}
                   GROUP BY player
                   ORDER BY score DESC",
                new CreateQueryJobOptions {
                DestinationTable = destination
            });

            // Wait for the job to complete.
            job.Poll();

            // Then we can fetch the results, either via the job or by accessing
            // the destination table.
            BigqueryResult result = client.GetQueryResults(job.Reference);

            foreach (var row in result.Rows)
            {
                Console.WriteLine($"{row["player"]}: {row["score"]}");
            }
            // End snippet

            var players = result.Rows.Select(r => (string)r["player"]).ToList();

            Assert.Contains("Ben", players);
            Assert.Contains("Nadia", players);
            Assert.Contains("Tim", players);
        }