Skip to content

Commit 877cabc

Browse files
committed
create and delete table functions
1 parent 996d442 commit 877cabc

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,4 @@ __pycache__/
286286
*.btm.cs
287287
*.odx.cs
288288
*.xsd.cs
289+
/AzureFunctions.Extensions.GoogleBigQuery.DemoProject1/credencials.json

AzureFunctions.Extensions.GoogleBigQuery/BigQueryService.cs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,44 @@ public BigQueryService(GoogleBigQueryAttribute googleBigQueryAttribute, Type ite
2020
(this.tableSchema, this.dictionaryOfProperties) = TableSchemaBuilderService.GetTableSchema(itemType);
2121
}
2222

23-
private Task<BigQueryTable> GetTable(DateTime date, CancellationToken cancellationToken)
24-
{
25-
BigQueryClient client = GetClient();
26-
27-
//return client.GetOrCreateTableAsync(datasetId, tableId, tableSchema, null, new CreateTableOptions() { TimePartitioning = new TimePartitioning() { Type = "DAY" } }, cancellationToken)
28-
// .ContinueWith((createTableTask) => {
29-
// return client.GetTableAsync(datasetId, $"{tableId}${date:yyyyMMdd}", null, cancellationToken);
30-
// }, cancellationToken).Unwrap();
23+
public Task CreateTable(bool timePartitioning, CancellationToken cancellationToken) {
24+
25+
BigQueryClient client = GetBiqQueryClient();
26+
27+
return client.GetOrCreateTableAsync(
28+
googleBigQueryAttribute.DatasetId,
29+
googleBigQueryAttribute.TableId,
30+
tableSchema,
31+
null,
32+
timePartitioning ? new CreateTableOptions() { TimePartitioning = new TimePartitioning() { Type = "DAY" } } : null,
33+
cancellationToken);
34+
35+
}
36+
37+
public Task DeleteTable(CancellationToken cancellationToken) {
38+
39+
BigQueryClient client = GetBiqQueryClient();
40+
41+
return client.DeleteTableAsync(
42+
googleBigQueryAttribute.DatasetId,
43+
googleBigQueryAttribute.TableId,
44+
null,
45+
cancellationToken);
46+
47+
}
48+
49+
private Task<BigQueryTable> GetTable(DateTime date, CancellationToken cancellationToken) {
50+
BigQueryClient client = GetBiqQueryClient();
51+
3152
return client.GetTableAsync(googleBigQueryAttribute.DatasetId, $"{googleBigQueryAttribute.TableId}${date:yyyyMMdd}", null, cancellationToken);
3253
}
3354

34-
private BigQueryClient GetClient()
35-
{
55+
private BigQueryClient GetBiqQueryClient() {
3656
GoogleCredential googleCredential = null;
37-
if (googleBigQueryAttribute.Credentials != null)
38-
{
57+
if (googleBigQueryAttribute.Credentials != null) {
3958
googleCredential = GoogleCredential.FromStream(new System.IO.MemoryStream(googleBigQueryAttribute.Credentials));
40-
}
41-
else
42-
{
43-
if (!string.IsNullOrWhiteSpace(googleBigQueryAttribute.CredentialsFileName))
44-
{
59+
} else {
60+
if (!string.IsNullOrWhiteSpace(googleBigQueryAttribute.CredentialsFileName)) {
4561
var path = System.IO.Path.GetDirectoryName(typeof(GoogleBigQueryAttribute).Assembly.Location);
4662
var fullPath = System.IO.Path.Combine(path, "..", googleBigQueryAttribute.CredentialsFileName);
4763
var credentials = System.IO.File.ReadAllBytes(fullPath);
@@ -80,7 +96,7 @@ public Task InsertRowsAsync(DateTime date, IEnumerable<GoogleBigQueryRow> rows,
8096
}
8197
else {
8298

83-
BigQueryClient client = GetClient();
99+
BigQueryClient client = GetBiqQueryClient();
84100

85101
IEnumerable<string> lines = BigQueryInsertRowService.GetBigQueryJobLines(rows);
86102

0 commit comments

Comments
 (0)