【问题标题】:How to integrate Google Bigquery with c# console application如何将 Google Bigquery 与 c# 控制台应用程序集成
【发布时间】:2015-06-15 16:04:12
【问题描述】:

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

如果是,我们该怎么做,我在互联网上搜索我找不到正确的答案。

我想要连接字符串格式?我已经从 Google Developer 控制台创建了客户端 ID,身份验证是如何完成的?这是一次配置或每次我们需要登录谷歌帐户进行身份验证。

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

谢谢, Selvakumar S

【问题讨论】:

    标签: c# integration google-bigquery


    【解决方案1】:

    这是一个基于 StackOverflow 中 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);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      在您的回答中,我无法添加命名空间 "使用 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");
                  }
              }
          }
      }
      

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-24
        • 2011-01-03
        • 2021-11-26
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-07
        相关资源
        最近更新 更多