gpt4 book ai didi

.net - 带有 .NET 文档/示例的 Google BigQuery

转载 作者:行者123 更新时间:2023-12-02 12:22:27 25 4
gpt4 key购买 nike

我需要使用 Google BigQuery API 查询数据。但我正在努力寻找 .NET 示例,并且二进制文件中没有包含文档(Google.Apis.Bigquery.dll)。谁能给我提供 .NET 的示例用法吗?

最佳答案

这是一个工作示例,部分基于 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

我们很快就会提供更多样本,但希望这同时会有所帮助。

关于.net - 带有 .NET 文档/示例的 Google BigQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12443878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com