能够通过ODBC使用BigQuery会很高兴。我已经通过ODBC使用了MySQL,这让我可以切换到BigQuery作为我的大数据表的MySQL的替代品。
答案 0 :(得分:7)
ODBC的专家Simba(他们为几乎所有你能想到的数据源提供了ODBC驱动程序),为BigQuery构建了一个ODBC连接器。您可以从BigQuery第三方工具页面here免费下载(适用于Windows)(向下滚动到页面底部)。如果您更喜欢使用Linux,可以直接从here从Simba下载ODBC驱动程序。请注意,如果您从他们的网站下载,Simba会收取费用。他们的驱动程序可能更新,并将包括支持。
需要注意的一个功能是,Simba驱动程序能够将标准的SQL-92兼容SQL转换为BigQUery的SQL方言。如果要将其用作关系数据库的替代品,这可能很有用。但是,如果要使用BigQuery的全部功能(嵌套数据,随机播放的JOIN / GROUP BY),则应关闭此开关以直接发送查询。
一旦安装了BigQuery ODBC驱动程序并创建了DSN(请参阅Simba ODBC文档以获取DSN创建的分步指南),您将需要开始进行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();
}
}
}