如何将 Google Bigquery 与 C# 控制台应用程序集成

How to integrate Google Bigquery with c# console application

是否可以将 Google 大查询与 C# 控制台应用程序集成?

如果是,我们该怎么做,我在网上搜索过,找不到合适的答案。

我想要连接字符串格式?我已经从 Google 开发人员控制台创建了客户端 ID 身份验证是如何完成的?是一次性配置或者每次都需要登录google账号进行认证。

如果有任何示例应用程序可以连接示例数据,那将会很有帮助。

谢谢, 塞尔瓦库玛 S

这是一个基于 Whosebug 中 another question 的工作示例:

using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;

using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;

using Google.Apis.Util;
using System;
using System.Diagnostics;
using System.Collections.Generic;

namespace BigQueryConsole
{
    public class BigQueryConsole
    {
        // Put your client ID and secret here (from https://developers.google.com/console)
        // Use the installed app flow here.
        // Client ID looks like "9999999.apps.googleusercontent.com"
        static string clientId = "YOURCLIENTID";  
        static string clientSecret = "YOURSECRET";

        // Project ID is in the URL of your project on the APIs Console
        // Project ID looks like "999999";
        static string projectId = "YOURPROJECTID";

        // Query in SQL-like form
        static string query = "SELECT state, count(*) from [publicdata:samples.natality] GROUP BY state ORDER BY state ASC";

        public static void Main(string[] args)
        {
            // Register an authenticator.
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);

            provider.ClientIdentifier = clientId;
            provider.ClientSecret = clientSecret;

            // Initiate an OAuth 2.0 flow to get an access token

            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

            // Create the service.
            var service = new BigqueryService(auth);
            JobsResource j = service.Jobs;
            QueryRequest qr = new QueryRequest();
            qr.Query = query;

            QueryResponse response = j.Query(qr, projectId).Fetch();
            foreach (TableRow row in response.Rows)
            {
                List<string> list = new List<string>();
                foreach (TableRow.FData field in row.F)
                {
                    list.Add(field.V);
                }
                Console.WriteLine(String.Join("\t", list));
            }
            Console.WriteLine("\nPress enter to exit");
            Console.ReadLine();
        }

        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] {  BigqueryService.Scopes.Bigquery.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }
    }
}

在你的回答中我无法添加命名空间 "using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;"

但我设法使用以下代码从 BigQuery 检索结果,您需要更新项目名称、项目 ID 和查询。

下载客户端 ID(我正在使用已安装的应用程序 - 其他类别)生成 JSON 文件并添加到您的调试文件夹中。

using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Threading;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
using System.Data;
using Google.Apis.Services;
using System;

namespace GoogleBigQuery
{
    public class Class1
    {
        private static void Main()
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open,
                                            FileAccess.Read))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                   GoogleClientSecrets.Load(stream).Secrets,
                   new[] { BigqueryService.Scope.Bigquery },
                   "user", CancellationToken.None).Result;
            }

            //  Create and initialize the Bigquery service. Use the Project Name value
            //  from the New Project window for the ApplicationName variable.

            BigqueryService Service = new BigqueryService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "PROJECT NAME"
            });

            string query = "YOUR QUERY";

            JobsResource j = Service.Jobs;
            QueryRequest qr = new QueryRequest();
            qr.Query = query;

            DataTable DT = new DataTable();
            int i = 0;

            QueryResponse response = j.Query(qr, "PROJECT ID").Execute();

            if (response != null)
            {
                int colCount = response.Schema.Fields.Count;

                foreach (var Column in response.Schema.Fields)
                {
                    DT.Columns.Add(Column.Name);
                }

                foreach (TableRow row in response.Rows)
                {
                    DataRow dr = DT.NewRow();

                    for (i = 0; i < colCount; i++)
                    {
                        dr[i] = row.F[i].V;
                    }

                    DT.Rows.Add(dr);
                }
            }
            else
            {
                Console.WriteLine("Response is null");
            }
        }
    }
}

谢谢。