Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private readonly BigqueryService bigquery;
private readonly IBackOff backOff = new ExponentialBackOff();
public async Task InsertData()
{
var req = new TableDataInsertAllRequest
{
Rows = LoadRows()
};
var retry = 1;
var shouldCreateNewTable = false;
while (retry < backOff.MaxNumOfRetries)
{
try
{
var response =
await bigquery.Tabledata.InsertAll(req,
"testprj", "testdataset", "testtbl").ExecuteAsync();
if (response.InsertErrors == null || !response.InsertErrors.Any())
{
return;
}
var messages = response.InsertErrors.Zip(req.Rows, (x, r) => x.Errors.Select(e => new { x, r, e }).ToArray())
.SelectMany(xs => xs)
.Where(x => x.e.Reason != "stopped")
.Select(x => string.Format(@"Index:{0}
DebugInfo:{1}
ETag:{2}
Location:{3}
Message:{4}
Reason:{5}
PostRawJSON:{6}", x.x.Index, x.e.DebugInfo, x.e.ETag, x.e.Location, x.e.Message, x.e.Reason, JsonConvert.SerializeObject(x.r.Json, Formatting.None)));
Console.WriteLine(messages);
}
catch (OperationCanceledException e)
{
throw;
}
catch (GoogleApiException e)
{
shouldCreateNewTable = e.Message.Contains("Not Found: Table");
}
catch (Exception ex)
{
//log exception
}
if (shouldCreateNewTable)
{
await CreateTable();
}
retry++;
await Task.Delay(backOff.GetNextBackOff(retry));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.