Created
December 6, 2014 15:58
-
-
Save tanaka-takayoshi/de18d7828c2cb3aad1a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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