如果能够通过 ODBC 使用 BigQuery,那就太好了。我已经通过 ODBC 使用 MySQL,这将让我切换到 BigQuery,作为我的大数据表的 MySQL 的替代品。
最佳答案
Simba 是 ODBC 方面的专家(他们为您能想到的几乎所有数据源提供 ODBC 驱动程序),为 BigQuery 构建了一个 ODBC 连接器。您可以从 BigQuery 第三方工具页面 here 免费下载(适用于 Windows) (向下滚动到页面底部)。如果您更喜欢使用 Linux,您可以从 here 直接从 Simba 下载 ODBC 驱动程序。 .请注意,如果您从他们的网站下载,Simba 会收费。他们的驱动程序可能是最新的,并将包括支持。
需要注意的一个特性是 Simba 驱动程序能够将标准 SQL-92 兼容 SQL 转换为 BigQUEry 的 SQL 方言。如果您想将其用作关系数据库的直接替代品,这会很有用。但是,如果您想使用 BigQuery 的全部功能(嵌套数据、混洗 JOIN/GROUP BY),您应该关闭此开关以直接发送查询。
安装 BigQuery ODBC 驱动程序并创建 DSN(有关 DSN 创建的分步指南,请参阅 Simba ODBC 文档)后,您将需要开始进行 ODBC 查询。这是从 C# 执行此操作的代码示例。如果编译并运行它,它将连接到 BigQuery,运行一个简单的查询,并打印结果。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BigQueryE2E
{
/**
* Helper class to build an ODBC Connection to connect to a Simba
* BigQuery ODBC Driver.
*/
class ConnectionBuilder {
private String Dsn;
private String Catalog;
private String ExecCatalog;
private bool UseNativeQuery;
public ConnectionBuilder SetDsn(String dsn) {
Dsn = dsn;
return this;
}
public ConnectionBuilder SetCatalog(String catalog) {
Catalog = catalog;
return this;
}
public ConnectionBuilder SetBillingCatalog(String catalog) {
ExecCatalog = catalog;
return this;
}
public ConnectionBuilder SetUseNativeQuery(bool nativeQuery) {
UseNativeQuery = nativeQuery;
return this;
}
public OdbcConnection Build() {
if (Catalog == null || Dsn == null) {
throw new ArgumentException("Missing required Connection setting");
}
StringBuilder connectionString = new StringBuilder();
connectionString.AppendFormat("DSN={0}; Catalog={1};", Dsn, Catalog);
if (ExecCatalog != null) {
connectionString.AppendFormat("ExecCatalog={0};", ExecCatalog);
}
if (UseNativeQuery) {
connectionString.Append("UseNativeQuery=1");
}
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = connectionString.ToString();
return conn;
}
}
class Program {
private static String Query =
"SELECT corpus, SUM(word_count) " +
"FROM samples.shakespeare " +
"GROUP BY corpus";
private static void PrintResults(OdbcDataReader reader) {
for (int ii = 0; ii < reader.FieldCount; ii += 1) {
System.Console.Write("{0}{1}",
reader.GetName(ii),
ii + 1 < reader.FieldCount ? "\t" : "\n");
}
while (reader.Read()) {
for (int ii = 0; ii < reader.FieldCount; ii += 1) {
System.Console.Write("{0}{1}",
reader.GetValue(ii),
ii + 1 < reader.FieldCount ? "\t" : "\n");
}
}
}
static void Main(string[] args) {
OdbcConnection connection = new ConnectionBuilder()
.SetDsn("bigquery1")
.SetCatalog("publicdata")
.SetBillingCatalog("bigquery-e2e")
.Build();
try {
connection.Open();
using (OdbcCommand command = connection.CreateCommand()) {
command.CommandText = Query;
using (OdbcDataReader reader = command.ExecuteReader()) {
PrintResults(reader);
}
}
} catch (Exception ex) {
System.Console.WriteLine("Error {0}: {1}",
connection.State != ConnectionState.Open
? "opening connection" : "executing query",
ex);
} finally {
connection.Close();
}
System.Console.ReadKey();
}
}
}
关于odbc - 如何通过 ODBC 连接对 BigQuery 进行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22307360/