您可以使用以下代码获取项目、数据集和表格的列表。
要获取公共数据,您可以使用以下代码
var SampleTableList = Service.Tables.List("publicdata", "samples").Execute();
因为 publicdata 只有一个数据集(样本),所以我们不能添加新的数据集,所以这段代码可以正常工作。
修改 ServiceAccountEmail、KeyFile 路径和 Key Secret 等属性。
using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Threading;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
using System.Data;
using Google.Apis.Services;
using System;
using System.Security.Cryptography.X509Certificates;
namespace GoogleBigQuery
{
public class Class1
{
private static void Main()
{
try
{
String serviceAccountEmail = "SERVICE ACCOUNT EMAIL";
var certificate = new X509Certificate2(@"KEY FILE NAME & PATH", "KEY SECRET", X509KeyStorageFlags.Exportable);
// SYNTAX: var certificate=new X509Certificate2(KEY FILE PATH+NAME (Here it resides in Bin\Debug folder so only name is enough), SECRET KEY, X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { BigqueryService.Scope.Bigquery, BigqueryService.Scope.BigqueryInsertdata, BigqueryService.Scope.CloudPlatform, BigqueryService.Scope.DevstorageFullControl }
}.FromCertificate(certificate));
// Create and initialize the Bigquery service. Use the Project Name value
// from the New Project window for the ApplicationName variable.
BigqueryService Service = new BigqueryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "APPLICATION NAME"
});
var SampleTableList = Service.Tables.List("publicdata", "samples").Execute();
var projectList = Service.Projects.List().Execute();
foreach (var projectDet in projectList.Projects)
{
var DataSetList = Service.Datasets.List(projectDet.Id).Execute();
foreach (var DataSetDet in DataSetList.Datasets)
{
var TablesList = Service.Tables.List(projectDet.Id, DataSetDet.Id).Execute();
}
}
}
catch (Exception e)
{
Console.WriteLine("Error Occurred: " + e.Message);
}
Console.ReadLine();
}
}
}