带有.NET文档/示例的Google BigQuery

时间:2012-09-16 01:56:17

标签: .net google-bigquery

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

2 个答案:

答案 0 :(得分:5)

这是一份工作样本,部分基于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

我们很快会在那里获得更多样品,但希望在此期间有所帮助。

答案 1 :(得分:0)

我们还没有任何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);
        }
    }
}