2012-09-16 101 views
10

我需要使用Google BigQuery API查詢數據。但我很努力地找到.NET示例,並且二進制文件(Google.Apis.Bigquery.dll)沒有包含任何文檔。任何人都可以爲我提供.NET的示例用法嗎?谷歌的BigQuery與.NET文檔/樣品

+0

請看下面的答案 - 他們有幫助嗎? –

+0

讓我們知道您是否需要更多幫助。如果下面的答案有效,請投票/接受。謝謝! –

+0

對於最近的一個,看到http://bitvectors.blogspot.de/2014/05/front-end-google-bigquery-with-aspnet_27.html –

回答

0

我們還沒有任何的BigQuery C#的樣品,但谷歌.NET庫提供各種樣品爲其他谷歌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); 
     } 
    } 
} 
+4

有這些樣品或文檔中已經取得了嗎?我似乎仍然無法跟蹤他們,我發現API文檔很好,但並沒有真正給出一個好的起點! – Ben

+0

什麼是Google.Apis.Util,GoogleAuthenticationServer,NativeApplicationClient 請發佈一些關於依賴關係的解釋。另外,你有樣品的機會嗎? – Cherven

+0

當我通過NuGet https://www.nuget.org/packages/Google.Apis/下載最新的Google Api庫時,它無法識別「Google.Apis.Authentication」。這是你上面粘貼的代碼的工作版本嗎?如果Java庫不麻煩,請讓我們知道,所以我們切換到這一點。 – Disasterkid

5

這裏的工作示例,基於部分關閉邁克爾的迴應:

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,加上在「庫」目錄中的其他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

,我們會得到更多的樣本在那裏很快,但希望這有助於在此期間。

+0

修正了最近的一個,看到http://bitvectors.blogspot.de/2014/05/front-end-google -bigquery -with-aspnet_27.html –

+0

您已鏈接的庫需要Windows 8.1及其所有NuGet包需要解決。我在YouTube上看到了一些關於Big Query的視頻。上面的代碼片段中使用的'using'語句不適用於其他代碼。如果Java庫更加穩定,請讓我知道,所以我們切換到這一點。 – Disasterkid