10

Google BigQuery APIを使用してデータをクエリする必要があります。しかし、.NET サンプルを見つけるのに苦労しており、バイナリ(Google.Apis.Bigquery.dll)にドキュメントが含まれていませんでした。.NET の使用例を教えてくれる人はいますか?

4

2 に答える 2

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

すぐにより多くのサンプルを入手できますが、それまでの間、これが役立つことを願っています.

于 2012-09-17T01:59:46.730 に答える
0

BigQuery C# のサンプルはまだありませんが、Google .NET ライブラリには他の Google API のさまざまなサンプルが付属しており、コードは類似しています。例については、このページを参照してください

コードは以下のようになります (注: まだ実際にテストする機会がなかったので、編集は大歓迎です... )。ところで、私たちは 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);
        }
    }
}
于 2012-09-16T05:17:20.383 に答える