2012-09-16 10 views
10

Wymagam zapytania danych przy użyciu Google BigQuery API. Ale staram się znaleźć .NET Samples i nie było żadnej dokumentacji dołączonej do pliku binarnego (Google.Apis.Bigquery.dll). Czy ktokolwiek może mi podać przykładowe użycie dla .NET?Google BigQuery z dokumentacją .NET/próbkami

+0

Proszę zobaczyć odpowiedzi poniżej - czy oni pomogli? –

+0

Daj nam znać, jeśli potrzebujesz więcej pomocy. Jeśli poniższe odpowiedzi działają, zagłosuj/zaakceptuj. Dzięki! –

+0

Najnowsze informacje można znaleźć na stronie http://bitvectors.blogspot.de/2014/05/front-end-google-bigquery-with-aspnet_27.html –

Odpowiedz

0

Nie mamy jeszcze próbek BigQuery C#, ale biblioteka Google .NET zawiera różne próbki do innych interfejsów API Google, a kod jest podobny. See this page for an example.

Kod będzie wyglądał mniej więcej tak (Uwaga: Nie miałem jeszcze okazji przetestować tego jeszcze, więc zmiany są mile widziane ...). Nawiasem mówiąc, jesteśmy w trakcie robienia wielu aktualizacji dokumentacji dla BigQuery i mamy nadzieję opublikować próbki C# tak szybko, jak tylko możemy (ale najprawdopodobniej w przyszłym miesiącu).

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

Czy zostały już wykonane te próbki lub dokumentacja? Wciąż nie mogę ich wyśledzić, znalazłem dokumentację API, która jest dobra, ale tak naprawdę nie daje dobrego punktu wyjścia! – Ben

+0

co to jest Google.Apis.Util, GoogleAuthenticationServer, NativeApplicationClient proszę zamieścić wyjaśnienie zależności. Poza tym, masz szansę na próbki? – Cherven

+0

Po pobraniu najnowszej biblioteki Google Api przez NuGet https://www.nuget.org/packages/Google.Apis/ nie rozpoznaje ona 'Google.Apis.Authentication'. Czy jest to działająca wersja kodu wklejonego powyżej? Jeśli biblioteka Java jest mniej kłopotliwa, poinformuj nas o tym, abyśmy się przełączyli. – Disasterkid

5

Oto próbka robocza, częściowo oparte off reakcji Michaela:

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

ta wykorzystuje synchroniczne zapytań. W przypadku zapytań asynchronicznych kod byłby nieco inny.

Należy odwoływać się zarówno do biblioteki dll usługi BigQuery (w katalogu "Usługi" w pobieraniu binarnym), jak i innych bibliotek dll w katalogu "Lib". Binarna wersja jest tutaj: http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release

Kod .NET będzie bardzo podobny do kodu biblioteki Java (są one generowane off z tym samym opisem API): https://developers.google.com/bigquery/docs/developers_guide#batchqueries

Dostaniemy więcej próbek wkrótce, ale mam nadzieję, że to pomoże w międzyczasie.

+0

dla niedawnego, zobacz http://bitvectors.blogspot.de/2014/05/front-end-google -bigquery-with-aspnet_27.html –

+0

Połączona biblioteka wymaga systemu Windows 8.1 i wszystkie pakiety NuGet muszą zostać rozwiązane. Widziałem niektóre z Twoich filmów na YouTube o Big Query. Wyrażenia 'using' użyte w powyższym fragmencie kodu nie działają z resztą kodu. Jeśli biblioteka Java jest bardziej stabilna, daj nam znać, więc przełączamy się na nią. – Disasterkid

Powiązane problemy