首页 > 编程语言 >Google BigQuery - .NET/C# API Reference Documentation

Google BigQuery - .NET/C# API Reference Documentation

时间:2023-04-03 17:46:22浏览次数:63  
标签:Google score Reference C# client BigQueryClient new table row

Was this helpful?  

Google.Cloud.BigQuery.V2

Google.Cloud.BigQuery.V2 is a.NET client library for the Google BigQuery API. It wraps the Google.Apis.Bigquery.v2 generated library, providing a higher-level API to make it easier to use.

Note: This documentation is for version 3.2.0 of the library. Some samples may not work with other versions.

Installation

Install the Google.Cloud.BigQuery.V2 package from NuGet. Add it to your project in the normal way (for example by right-clicking on the project in Visual Studio and choosing "Manage NuGet Packages...").

Authentication

When running on Google Cloud Platform, no action needs to be taken to authenticate.

Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it. The credentials will automatically be used to authenticate. See the Getting Started With Authentication guide for more details.

Getting started

Common operations are exposed via the BigQueryClient class, and additional wrapper classes are present to make operations with datasets, tables and query results simpler.

Client life-cycle management

In many cases you don't need to worry about disposing of BigQueryClient objects, and can create them reasonably freely - but be aware that this can causes issues with memory and network connection usage. We advise you to reuse a single client object if possible; if your architecture requires you to frequently create new client objects, please dispose of them to help with timely resource clean-up. See the resource clean-up guide for more details.

Sample code

Querying

 
BigQueryClient client = BigQueryClient.Create(projectId);
BigQueryTable table = client.GetTable("bigquery-public-data", "samples", "shakespeare");

string sql = $"SELECT corpus AS title, COUNT(word) AS unique_words FROM {table} GROUP BY title ORDER BY unique_words DESC LIMIT 10";
BigQueryParameter[] parameters = null;
BigQueryResults results = client.ExecuteQuery(sql, parameters);

foreach (BigQueryRow row in results)
{
    Console.WriteLine($"{row["title"]}: {row["unique_words"]}");
}

Parameterized queries

Queries can be provided with parameters, either using names (the default):

 
BigQueryClient client = BigQueryClient.Create(projectId);
BigQueryTable table = client.GetTable(datasetId, tableId);
string sql = $"SELECT player, score, level FROM {table} WHERE score >= @score AND level >= @level";
BigQueryParameter[] parameters = new[]
{
    new BigQueryParameter("level", BigQueryDbType.Int64, 2),
    new BigQueryParameter("score", BigQueryDbType.Int64, 1500)
};
BigQueryResults results = client.ExecuteQuery(sql, parameters);
foreach (BigQueryRow row in results)
{
    Console.WriteLine($"Name: {row["player"]}; Score: {row["score"]}; Level: {row["level"]}");
}

Or using positional parameters:

 
BigQueryClient client = BigQueryClient.Create(projectId);
BigQueryTable table = client.GetTable(datasetId, tableId);
string sql = $"SELECT player, score, level FROM {table} WHERE score >= ? AND level >= ?";
BigQueryParameter[] parameters = new[]
{
    new BigQueryParameter(BigQueryDbType.Int64, 1500), // For score
    new BigQueryParameter(BigQueryDbType.Int64, 2), // For level
};
QueryOptions queryOptions = new QueryOptions { ParameterMode = BigQueryParameterMode.Positional };
BigQueryResults results = client.ExecuteQuery(sql, parameters, queryOptions);
foreach (BigQueryRow row in results)
{
    Console.WriteLine($"Name: {row["player"]}; Score: {row["score"]}; Level: {row["level"]}");
}

Using legacy SQL

By default, BigQueryClient uses Standard SQL. To use Legacy SQL, simply set UseLegacySql to true in the query options, and make sure that you use the legacy format for the table name, as shown below.

 
BigQueryClient client = BigQueryClient.Create(projectId);
BigQueryTable table = client.GetTable("bigquery-public-data", "samples", "shakespeare");

string sql = $"SELECT TOP(corpus, 10) AS title, COUNT(*) AS unique_words FROM {table:legacy}";
BigQueryParameter[] parameters = null;
BigQueryResults results = client.ExecuteQuery(sql, parameters, new QueryOptions { UseLegacySql = true });

foreach (BigQueryRow row in results)
{
    Console.WriteLine($"{row["title"]}: {row["unique_words"]}");
}

Wildcard queries

Wildcard queries can be used to query multiple tables at the same time. Wildcard table names only work in queries written using Standard SQL, so make sure to use the standard format for the table name as shown below.

 
BigQueryClient client = BigQueryClient.Create(projectId);

string sql = $"SELECT year, mo, da, temp, min, max FROM `bigquery-public-data.noaa_gsod.gsod*` where `max` > 120 and `max` < 121 LIMIT 10";
BigQueryParameter[] parameters = null;
BigQueryResults results = client.ExecuteQuery(sql, parameters);

foreach (BigQueryRow row in results)
{
    Console.WriteLine($"On {row["year"]}-{row["mo"]}-{row["da"]} the mean temperature was {row["temp"]} with min temperature at {row["min"]} and max temperature at {row["max"]}.");
}

Data insertion

 
BigQueryClient client = BigQueryClient.Create(projectId);

// Create the dataset if it doesn't exist.
BigQueryDataset dataset = client.GetOrCreateDataset("mydata");

// Create the table if it doesn't exist.
BigQueryTable table = dataset.GetOrCreateTable("scores", new TableSchemaBuilder
{
    { "player", BigQueryDbType.String },
    { "gameStarted", BigQueryDbType.Timestamp },
    { "score", BigQueryDbType.Int64 }
}.Build());

// Insert a single row. There are many other ways of inserting
// data into a table.
table.InsertRow(new BigQueryInsertRow
{
    { "player", "Bob" },
    { "score", 85 },
    { "gameStarted", new DateTime(2000, 1, 14, 10, 30, 0, DateTimeKind.Utc) }
});

DML

BigQuery supports DML.

Suppose we have a high score table, and we realize that on one day we accidentally recorded incorrect scores: each player was only awarded half the score they actually earned. We can update the data afterwards using DML:

 
BigQueryClient client = BigQueryClient.Create(projectId);
BigQueryTable table = client.GetTable(datasetId, tableId);
BigQueryResults result = client.ExecuteQuery(
    $"UPDATE {table} SET score = score * 2 WHERE DATE(game_started) = @date",
    new[] { new BigQueryParameter("date", BigQueryDbType.Date, new DateTime(2013, 6, 1)) })
    .ThrowOnAnyError();
Console.WriteLine($"Modified {result.NumDmlAffectedRows} row(s)");

Important note on the result returned by DML operations (in version 1.3.0)

In version 1.3.0, iterating over the results of a BigQueryResults object returned from a DML operation will iterate over the entire table modified by that operation. This is a side-effect of the way the underlying API is called, but it's rarely useful to iterate over the results. The NumDmlAffectedRows property of the results object is useful, however, in determining how many rows were modified.

From version 1.4.0-beta01 onwards, the BigQueryResults object returned from a DML operation returns no rows, but NumDmlAffectedRows still returns the number of affected rows.

Creating a table partitioned by time

 
BigQueryClient client = BigQueryClient.Create(projectId);
TableSchema schema = new TableSchemaBuilder
{
    { "message", BigQueryDbType.String }
}.Build();
Table tableToCreate = new Table
{
    TimePartitioning = TimePartition.CreateDailyPartitioning(expiration: null),
    Schema = schema
};
BigQueryTable table = client.CreateTable(datasetId, tableId, tableToCreate);
// Upload a single row to the table, using JSON rather than the streaming buffer, as
// the _PARTITIONTIME column will be null while it's being served from the streaming buffer.
// This code assumes the upload succeeds; normally, you should check the job results.
table.UploadJson(new[] { "{ \"message\": \"Sample message\" }" }).PollUntilCompleted();

BigQueryResults results = client.ExecuteQuery(
    $"SELECT message, _PARTITIONTIME AS pt FROM {table}",
    parameters: null);
List<BigQueryRow> rows = results.ToList();
foreach (BigQueryRow row in rows)
{
    string message = (string) row["message"];
    DateTime partition = (DateTime) row["pt"];
    Console.WriteLine($"Message: {message}; partition: {partition:yyyy-MM-dd}");
}

Querying an external data source

As described in the documentation, BigQuery can query some external data sources. The sample code below demonstrates querying a CSV file stored in Google Cloud Storage.

 
BigQueryClient client = BigQueryClient.Create(projectId);
TableSchema schema = new TableSchemaBuilder
{
    { "name", BigQueryDbType.String },
    { "score", BigQueryDbType.Int64 }
}.Build();
Table tableToCreate = new Table
{
    ExternalDataConfiguration = new ExternalDataConfiguration
    {
        SourceFormat = "CSV",
        SourceUris = new[] { $"gs://{bucket}/{objectName}" }
    },
    Schema = schema
};
BigQueryTable table = client.CreateTable(datasetId, tableId, tableToCreate);
BigQueryParameter[] parameters = null;
List<BigQueryRow> rows = client.ExecuteQuery($"SELECT name, score FROM {table} ORDER BY score", parameters).ToList();
foreach (BigQueryRow row in rows)
{
    Console.WriteLine($"{row["name"]} - {row["score"]}");
}
  ref:https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest

标签:Google,score,Reference,C#,client,BigQueryClient,new,table,row
From: https://www.cnblogs.com/watermarks/p/17283787.html

相关文章

  • C#如何更新配置文件中的连接字符串
    以MySql为例,其它数据库使用方法一样说明:正常情况下,如果数据库在本机,尽量使用Windows身份验证,如果不在本机,连接字符串里的密码也是需要加密存储,本文只做演示,所以直接使用明文密码。如下在App.config中添加了两条如下连接字符串   第一条是使用ADO.Net使用的连接字符串,第......
  • Low-Code,一定“low”吗?
    作者:京东保险吴凯前言低代码是一组数字技术工具平台,基于图形化拖拽、参数化配置等更为高效的方式,实现快速构建、数据编排、连接生态、中台服务。通过少量代码或不用代码实现数字化转型中的场景应用创新。本文将重点介绍低代码相关知识,包括低代码的定义与意义、相关概念、行业发......
  • EasyCVR插件工具:如何删除EasyShark的抓包数据?
    在前期的文章中,我们分享了关于EasyCVR平台新增的插件工具,感兴趣的用户可以查看这篇文章:《EasyCVR视频融合平台开放插件功能:支持EasyNTS与EasyShark》。其中,EasyShark是用于抓包的工具,支持在客户端直接抓包查看服务器的SIP消息。但是,有用户反馈,由于经常抓包产生了大量的数据,不知道如......
  • Hystrix(一):为什么@EnableCircuitBreaker和@HystrixCommand能驱动Hystrix
    一、@EnableCircuitBreakerEnableCircuitBreaker源码如下:从源码看出实例化了@EnableCircuitBreaker注解实例化了EnableCircuitBreakerImportSelector这个类。再来看EnableCircuitBreakerImportSelector源码:EnableCircuitBreakerImportSelector继承了SpringFactoryImportSelector,Spr......
  • 多精度 simulator 中的 RL:一篇 14 年 ICRA 的古早论文
    目录全文快读0abstract1intro2relatedwork3背景&假设3.1RL&KWIK(knowwhatitknows)的背景3.2问题定义4Multi-FidelityBanditOptimization4.1MF寻找最优arm的算法(MF-bandit)4.2一个例子4.3理论证明5Multi-FidelityRL5.1MFRLalgorithm5.2一个例子5.3理论......
  • Java记录唯一性check
    /***记录唯一性check**@paramid主键*@paramentity实体记录,必须实现equals()方法才能验证更新的场合*@paramfields唯一键字段名称*/if(entity==null||fields.length==0){return;}try{@SuppressWarnings("unchecked")......
  • shellcode获取MessageBoxA的地址
    _asm{pushebpmoveax,fs:[30h];获得PEB地址moveax,[eax+0ch];获得LDR地址moveax,[eax+14h];获得PEB_LDR_DATA中InMemoryOrderModuleList的Flinkmovecx,eax;因为eax中的Flink也就是等于LDR_DATA_TABLE_ENTRY......
  • background-color 只填充容器的一半
     关键字的取值:toright  (表示从左往右渐变)toleft    (表示从右往左渐变)totop    (表示从下往上渐变)tobottom (表示从上往下渐变)角度的取值: 0deg  (从下到上totop)  180deg(从上到下tobottom)90deg  (从左到右toright)-90deg......
  • Chrome103版本获取不到sessionStorage
    问题现象:上传附件功能报错,经排查发现,是因为上送字段中userId获取失败,被服务端拒绝请求。userId=window.sessionStorage.getItem('userId')问题暴露阶段:生产环境Chrome103问题原因:上传功能是在新弹开tab页中实现的,Chrome89后,新弹开的tab页默认不再共享sessionStorage。而测试环......
  • @Async配置与使用
    应用场景同步:同步就是整个处理过程顺序执行,当各个过程都执行完毕,并返回结果。异步:异步调用则是只是发送了调用的指令,调用者无需等待被调用的方法完全执行完毕;而是继续执行下面的流程。例如,在某个调用中,需要顺序调用A,B,C三个过程方法;如他们都是同步调用,则需要将他们都顺序......