Hila Gal
Hila Gal

Reputation: 149

Big Query InsertAll using C#

I'm using this code while trying to insert data using Big Query. Everything is running without any exceptions, but my table is empty. What is the problem with my code?

         string SERVICE_ACCOUNT_EMAIL = "MyAccount";
        var certificate = new X509Certificate2(@"XXX.p12", "notasecret", X509KeyStorageFlags.Exportable);
        ServiceAccountCredential credential = new ServiceAccountCredential(
          new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
          {
              Scopes = new[] { BigqueryService.Scope.BigqueryInsertdata, BigqueryService.Scope.Bigquery }

          }.FromCertificate(certificate));
        // Create the service.
        var service = new BigqueryService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "test"

        });

        Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest tabreq = new Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest();
        List<Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData> tabrows = new List<Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData>();
        Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData rd = new Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData();
        IDictionary<string, object> r = new Dictionary<string, object>();
        r.Add("Key", "Value");
        rd.Json = r;
        tabrows.Add(rd);
        tabreq.Rows = tabrows;
        tabreq.Kind = "bigquery#tableDataInsertAllRequest";
        service.Tabledata.InsertAll(tabreq, "xxx", "xxx", "xxx"); 

Upvotes: 2

Views: 2512

Answers (1)

Edgar Jimenez
Edgar Jimenez

Reputation: 11

Your codes looks good, but the only one problem is that you need to send data, actually you are not sending any data, test this code and let me know (I created a table with two fields P1 and P2)

var logs = new List<TableDataInsertAllRequest.RowsData>();
var theLog = new TableDataInsertAllRequest.RowsData();
theLog.Json = new Dictionary<string, object>();
theLog.Json.Add("P1", "Hola");
theLog.Json.Add("P2", "Mundo");            
logs.Add(theLog);

var service = GetBigQueryService();
var content = new TableDataInsertAllRequest();
content.Rows = logs;
content.Kind = "bigquery#tableDataInsertAllRequest";
content.IgnoreUnknownValues = true;
content.SkipInvalidRows = true;

var insertTask = service.Tabledata.InsertAll(content, "Your_Project_Id", "Your_DataSet", "Your_Table");
TableDataInsertAllResponse response = insertTask.Execute();

Upvotes: 1

Related Questions