Search code examples
c#integrationgoogle-bigquery

How to integrate Google Bigquery with c# console application


If it is possible to integrate Google big query with C# console application?.

If yes how we can do, i searched over internet i could not find proper answer for that.

I want connection string format? I have created Client ID from Google Developer console how authentication has done? It is one time configuration or every time we need to login in google account to authenticate.

If there is any sample application to connect sample data it would be helpful.

Thanks, Selvakumar S


Solution

  • In your answer i could not able to add namespace "using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;"

    But i manage to retrieve results from BigQuery using below code, you need to update Project Name, Project Id and Query.

    Download Client ID (I am using Installed Application - Other category ) generate JSON file and add into your Debug folder.

    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;
    
    namespace GoogleBigQuery
    {
        public class Class1
        {
            private static void Main()
            {
                UserCredential credential;
                using (var stream = new FileStream("client_secrets.json", FileMode.Open,
                                                FileAccess.Read))
                {
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                       GoogleClientSecrets.Load(stream).Secrets,
                       new[] { BigqueryService.Scope.Bigquery },
                       "user", CancellationToken.None).Result;
                }
    
                //  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 = "PROJECT NAME"
                });
    
                string query = "YOUR QUERY";
    
                JobsResource j = Service.Jobs;
                QueryRequest qr = new QueryRequest();
                qr.Query = query;
    
                DataTable DT = new DataTable();
                int i = 0;
    
                QueryResponse response = j.Query(qr, "PROJECT ID").Execute();
    
                if (response != null)
                {
                    int colCount = response.Schema.Fields.Count;
    
                    foreach (var Column in response.Schema.Fields)
                    {
                        DT.Columns.Add(Column.Name);
                    }
    
                    foreach (TableRow row in response.Rows)
                    {
                        DataRow dr = DT.NewRow();
    
                        for (i = 0; i < colCount; i++)
                        {
                            dr[i] = row.F[i].V;
                        }
    
                        DT.Rows.Add(dr);
                    }
                }
                else
                {
                    Console.WriteLine("Response is null");
                }
            }
        }
    }
    

    Thanks.