I have a C# code which inserts a row into BigQuery. But the table in BigQuery has a record field. How should I insert the data into that table?
BigQuery Table:
column1 | column2 | column3.a | column3.b | column4
*column3 is a RECORD having multiple rows of "a" and "b"
This is what I have got so far in my C# code:
BigQueryClient BQclient = BigQueryClient.Create(project_id, credentials);
var table = BQclient.GetDataset("dataset").GetTable("table");
List < BigQueryInsertRow > rows = new List < BigQueryInsertRow >();
try {
for (int i=0; i < data.Count; i++ ) {
BigQueryInsertRow insertRow = new BigQueryInsertRow();
insertRow.Add("column1", data[i].column1);
insertRow.Add("column2", data[i].column2);
↓↓↓ this is where I am having a hard time ↓↓↓
for (int j=0; j < data[i].column3.Count; j++ ) {
insertRow.Add("colum3.a", data[i].column3[j].a);
insertRow.Add("colum3.b", data[i].column3[j].b);
}
↑↑↑ this is where I am having a hard time ↑↑↑
insertRow.Add("column4"), data[i].column4);
rows.Add(insertRow);
}
table.InsertRows(rows);
}
catch (Exception e) {
throw e;
}
After playing around with it, I was able to insert the repeated data into my insertRow.
Here is what I did:
var temp = new BigQueryRow();
var list_data = new List<BigQueryInsertRow>();
for (int j=0; j<data[i].column3.Count; j++)
{
temp = new BigQueryRow();
temp.Add("a", data[i].column3[j].a);
temp.Add("b", data[i].column3[j].b);
list_data.Add(temp);
}
insertRow.Add("column3", list_data);
Apparently, each column in a record should be named by its own column name without attaching the parent column name which is column3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With