2012-09-16 20 views
10

Verileri Google BigQuery API kullanarak sorgulamam gerekiyor. Ama .NET Örnekleri bulmak için mücadele ediyorum ve (Google.Apis.Bigquery.dll) ikili belge ile hiçbir belge yoktu. Herkes .NET için örnek kullanım sağlayabilir miyim?Google BigQuery with .NET belgeleri/örnekleri

+0

Lütfen aşağıdaki cevaplara bakın - yardımcı oldu mu? –

+0

Daha fazla yardıma ihtiyacınız varsa bize bildirin. Aşağıdaki cevaplar işe yararsa, lütfen oylayın/kabul edin. Teşekkürler! –

+0

Son zamanlarda, bkz. Http://bitvectors.blogspot.de/2014/05/front-end-google-bigquery-with-aspnet_27.html –

cevap

0

Henüz herhangi bir BigQuery C# örneğimiz yok, ancak Google .NET kütüphanesi diğer Google API'leri için çeşitli örneklerle geliyor ve kod benzer. See this page for an example.

Kod aşağıdaki gibi görünecek (not: Bunu henüz test etme şansım olmadı, bu yüzden düzenlemeler hoş geldiniz ...). Bu arada, BigQuery için çok sayıda dokümantasyon güncellemesi yapmaktayız ve bazı C# örneklerini mümkün olan en kısa sürede (ancak büyük ihtimalle gelecek ay) göndermeyi umuyoruz.

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

Bu örneklerin veya belgelerin henüz yapılmış mı? Hala onları takip edemiyorum, güzel olan API belgelerini buldum, ama gerçekten iyi bir başlangıç ​​noktası vermedim! – Ben

+0

Google.Apis.Util, GoogleAuthenticationServer, NativeApplicationClient nedir lütfen bağımlılıklar için bazı açıklamalar gönderin. Ayrıca, herhangi bir şansın var mı? – Cherven

+0

En son Google Api kitaplığını NuGet https://www.nuget.org/packages/Google.Apis/ aracılığıyla yüklediğimde, "Google.Apis.Authentication" ifadesini tanımıyor. Bu, yukarıda yapıştırdığınız kodun çalışan bir sürümü mi? Java kütüphanesi bir güçlükten daha azsa, lütfen bize bildirin. – Disasterkid

5

İşte Michael'ın yanıtının kapalı bölümünde merkezli bir çalışma örneği var: Bu senkron sorguları kullanır

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

. Eşzamansız sorgular için kod biraz farklı olur.

Hem BigQuery hizmeti dll (ikili yüklemedeki 'Hizmetler' dizininin altında) hem de 'Lib' dizinindeki diğer dll'lere başvurmanız gerekir. İkili sürüm burada: https://developers.google.com/bigquery/docs/developers_guide#batchqueries

Biz o kadar kişi örnekleri: http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release

.NET kodu (onlar aynı API açıklamasının kapalı oluşturulan konum) Java kütüphanesi koduna çok benzer olacak yakında orada, ama umarım bu arada yardımcı olur.

+0

ile düzeltildi, bkz. Http://bitvectors.blogspot.de/2014/05/front-end-google -bigquery-with-aspnet_27.html –

+0

Bağladığınız kitaplık Windows 8.1 gerektirir ve tüm NuGet paketlerinin çözülmesi gerekir. YouTube'daki bazı videolarınızı Big Query hakkında gördüm. Yukarıdaki kod snippet'inde kullanılan 'using' ifadeleri, kodun geri kalanıyla çalışmaz. Java kütüphanesi daha kararlıysa lütfen bize bildirin. – Disasterkid