How can I make queries against BigQuery through an ODBC connection? -


it nice able use bigquery via odbc. use mysql through odbc, , let me switch bigquery drop-in replacement mysql big data tables.

simba, expert in odbc (they have odbc drivers every data source can think of), has built odbc connector bigquery. can download free (for windows) bigquery third party tools page here (scroll down bottom of page). if prefer use linux, can download odbc driver simba directly here. note simba charge if download site. driver may more up-to-date , include support.

a feature note simba driver has ability translate standard sql-92 compliant sql bigquery's dialect of sql. can useful if want use drop-in replacement relational database. however, if want use full features of bigquery (nested data, shuffled join / group by), should turn switch off send queries directly.

once you've installed bigquery odbc driver , created dsn (see simba odbc docs step-by-step guide dsn creation), you'll want start making odbc queries. here code sample c#. if compile , run it, connect bigquery, run simple query, , print results.

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 build odbc connection connect 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 corpus";      private static void printresults(odbcdatareader reader) {       (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()) {         (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);       } {         connection.close();       }       system.console.readkey();     }   } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -