【问题标题】:Google BigQuery with .NET documentation/ samples带有 .NET 文档/示例的 Google BigQuery
【发布时间】:2012-09-16 01:56:16
【问题描述】:

我需要使用Google BigQuery API 查询数据。但我很难找到 .NET 示例,并且二进制 (Google.Apis.Bigquery.dll) 中没有包含文档。任何人都可以为我提供 .NET 的示例用法吗?

【问题讨论】:

  • 请看下面的答案——他们有帮助吗?
  • 如果您需要更多帮助,请告诉我们。如果以下答案有效,请投票/接受。谢谢!
  • 最近的一篇,见bitvectors.blogspot.de/2014/05/…

标签: .net google-bigquery


【解决方案1】:

这是一个工作示例,部分基于 Michael 的回复:

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);
        }
    }
}

这使用同步查询。对于异步查询,代码会略有不同。

您需要同时引用 BigQuery 服务 dll(在二进制下载的“Services”目录下)以及“Lib”目录中的其他 dll。二进制版本在这里:http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release

.NET 代码将与 Java 库代码非常相似(它们是根据相同的 API 描述生成的): https://developers.google.com/bigquery/docs/developers_guide#batchqueries

我们很快就会得到更多的样品,但希望在此期间能有所帮助。

【讨论】:

  • 最近的一篇,见bitvectors.blogspot.de/2014/05/…
  • 您链接的库需要 Windows 8.1 并且需要解析其所有 NuGet 包。我在 YouTube 上看到了一些关于 Big Query 的视频。上面代码 sn-p 中使用的 using 语句不适用于其余代码。如果 Java 库更稳定,请告诉我,以便我们切换到那个。
【解决方案2】:

我们还没有任何 BigQuery C# 示例,但 Google .NET 库提供了其他 Google API 的各种示例,并且代码类似。 See this page for an example

代码如下所示(注意:我还没有机会实际测试它,所以欢迎编辑...)。顺便说一句,我们正在为 BigQuery 进行大量文档更新,我们希望尽快发布一些 C# 示例(但最有可能在下个月)。

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

using Google.Apis.Bigquery.v2;
using Google.Apis.Util;

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

            // Put your client id and secret here (from https://developers.google.com/console)
            // Use the installed app flow here.
            provider.ClientIdentifier = "<client id>";
            provider.ClientSecret = "<client secret>";

            // 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);

            // Do something with the BigQuery service here
            // Such as... service.[some BigQuery method].Fetch();
        }

        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);
        }
    }
}

【讨论】:

  • 这些样本或文档制作完成了吗?我似乎仍然无法追踪它们,我找到了很好的 API 文档,但并没有真正给出一个好的起点!
  • 什么是 Google.Apis.Util、GoogleAuthenticationServer、NativeApplicationClient 请对依赖项进行一些解释。另外,你有样品吗?
  • 当我通过 NuGet nuget.org/packages/Google.Apis 下载最新的 Google Api 库时,它无法识别 Google.Apis.Authentication。这是您上面粘贴的代码的工作版本吗?如果 Java 库不那么麻烦,请告诉我们,以便我们切换到该库。
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 2020-07-04
  • 2012-04-20
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多