谷歌BigQuery中使用.NET文档/示例 [英] Google BigQuery with .NET documentation/ samples
问题描述
我需要查询使用谷歌BigQuery中的API 的数据。但我在努力寻找.NET样品,并且没有附带的文档二进制文件的(Google.Apis.Bigquery.dll)的。任何人都可以向我提供的样品使用的.NET?
I require to query data using Google BigQuery API. But I am struggling to find .NET Samples, and there was no documentation included with the binary (Google.Apis.Bigquery.dll). Can anybody provide me with sample usage for .NET?
推荐答案
下面是一个工作示例,根据部分关闭迈克尔的回应:
Here's a working sample, based in part off of Michael's response:
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);
}
}
}
本使用同步查询。对于asyncronous查询时,code会略有不同。
This uses synchronous queries. For asyncronous queries, the code would be slightly different.
您需要同时引用了BigQuery中的服务的dll(服务中的二进制下载目录下),再加上其他DLL中的库目录。二进制版本是在这里:的http://$c$c.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release
You'll need to reference both the BigQuery service dll (under 'Services' directory in the binary download), plus the other dlls in the 'Lib' directory. Binary release is here: http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release
在.NET code将是非常相似的Java库code(他们开的相同的API描述生成): https://developers.google.com/bigquery/docs/developers_guide#batchqueries
The .NET code is going to be very similar to the Java library code (they're generated off of the same API description): https://developers.google.com/bigquery/docs/developers_guide#batchqueries
我们会得到更多的样品在那里很快,但希望这有助于在其间。
We'll get more samples out there soon, but hopefully this helps in the meantime.
这篇关于谷歌BigQuery中使用.NET文档/示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!