77using System . Threading . Tasks ;
88using System . Threading ;
99
10- namespace AzureFunctions . Extensions . GoogleBigQuery {
10+ namespace AzureFunctions . Extensions . GoogleBigQuery
11+ {
1112
12- public class BigQueryService {
13+ public class BigQueryService
14+ {
1315
1416 private readonly GoogleBigQueryAttribute googleBigQueryAttribute ;
1517 private readonly TableSchema tableSchema ;
1618 private readonly IDictionary < string , IEnumerable < System . Reflection . PropertyInfo > > dictionaryOfProperties ;
1719
18- public BigQueryService ( GoogleBigQueryAttribute googleBigQueryAttribute , Type itemType ) {
20+ public BigQueryService ( GoogleBigQueryAttribute googleBigQueryAttribute , Type itemType )
21+ {
1922 this . googleBigQueryAttribute = GoogleBigQueryAttribute . GetAttributeByConfiguration ( googleBigQueryAttribute ) ;
2023 ( this . tableSchema , this . dictionaryOfProperties ) = TableSchemaBuilderService . GetTableSchema ( itemType ) ;
2124 }
2225
23- public Task CreateTable ( bool timePartitioning , CancellationToken cancellationToken ) {
26+ public Task CreateTable ( bool timePartitioning , CancellationToken cancellationToken )
27+ {
2428
2529 BigQueryClient client = GetBiqQueryClient ( ) ;
2630
@@ -34,7 +38,8 @@ public Task CreateTable(bool timePartitioning, CancellationToken cancellationTok
3438
3539 }
3640
37- public Task DeleteTable ( CancellationToken cancellationToken ) {
41+ public Task DeleteTable ( CancellationToken cancellationToken )
42+ {
3843
3944 BigQueryClient client = GetBiqQueryClient ( ) ;
4045
@@ -46,31 +51,44 @@ public Task DeleteTable(CancellationToken cancellationToken) {
4651
4752 }
4853
49- private Task < BigQueryTable > GetTable ( DateTime date , CancellationToken cancellationToken ) {
54+ private Task < BigQueryTable > GetTable ( DateTime date , CancellationToken cancellationToken )
55+ {
5056 BigQueryClient client = GetBiqQueryClient ( ) ;
5157
5258 return client . GetTableAsync ( googleBigQueryAttribute . DatasetId , $ "{ googleBigQueryAttribute . TableId } ${ date : yyyyMMdd} ", null , cancellationToken ) ;
5359 }
5460
55- private BigQueryClient GetBiqQueryClient ( ) {
61+ private static BigQueryClient _client = null ;
62+
63+ private BigQueryClient GetBiqQueryClient ( )
64+ {
65+
66+ if ( _client != null ) { return _client ; }
67+
5668 GoogleCredential googleCredential = null ;
57- if ( googleBigQueryAttribute . Credentials != null ) {
69+ if ( googleBigQueryAttribute . Credentials != null )
70+ {
5871 googleCredential = GoogleCredential . FromStream ( new System . IO . MemoryStream ( googleBigQueryAttribute . Credentials ) ) ;
59- } else {
60- if ( ! string . IsNullOrWhiteSpace ( googleBigQueryAttribute . CredentialsFileName ) ) {
72+ }
73+ else
74+ {
75+ if ( ! string . IsNullOrWhiteSpace ( googleBigQueryAttribute . CredentialsFileName ) )
76+ {
6177 var path = System . IO . Path . GetDirectoryName ( typeof ( GoogleBigQueryAttribute ) . Assembly . Location ) ;
6278 var fullPath = System . IO . Path . Combine ( path , ".." , googleBigQueryAttribute . CredentialsFileName ) ;
6379 var credentials = System . IO . File . ReadAllBytes ( fullPath ) ;
6480 googleCredential = GoogleCredential . FromStream ( new System . IO . MemoryStream ( credentials ) ) ;
6581 }
6682 }
67- var client = BigQueryClient . Create ( googleBigQueryAttribute . ProjectId , googleCredential ) ;
68- return client ;
83+ _client = BigQueryClient . Create ( googleBigQueryAttribute . ProjectId , googleCredential ) ;
84+ return _client ;
6985 }
7086
71- public Task InsertRowsAsync ( DateTime date , IEnumerable < GoogleBigQueryRow > rows , CancellationToken cancellationToken ) {
87+ public Task InsertRowsAsync ( DateTime date , IEnumerable < GoogleBigQueryRow > rows , CancellationToken cancellationToken )
88+ {
7289
73- if ( rows != null && rows . Count ( ) > 0 ) {
90+ if ( rows != null && rows . Count ( ) > 0 )
91+ {
7492 int dateDiff = ( date - DateTime . UtcNow . Date ) . Days ;
7593
7694 if ( dateDiff >= - 31 && dateDiff <= 16 )
@@ -94,19 +112,22 @@ public Task InsertRowsAsync(DateTime date, IEnumerable<GoogleBigQueryRow> rows,
94112 } , cancellationToken ) . Unwrap ( ) ;
95113
96114 }
97- else {
115+ else
116+ {
117+
118+ //throw new Exception("BQ streaming API doesn't allow to write data 31 days to de past and 16 for tthe future in day partitioned tables.");
98119
99- BigQueryClient client = GetBiqQueryClient ( ) ;
120+ // BigQueryClient client = GetBiqQueryClient();
100121
101- IEnumerable < string > lines = BigQueryInsertRowService . GetBigQueryJobLines ( rows ) ;
122+ // IEnumerable<string> lines = BigQueryInsertRowService.GetBigQueryJobLines(rows);
102123
103- return client . UploadJsonAsync (
104- googleBigQueryAttribute . DatasetId ,
105- $ "{ googleBigQueryAttribute . TableId } ${ date : yyyyMMdd} ",
106- tableSchema ,
107- lines ,
108- new UploadJsonOptions ( ) { AllowUnknownFields = true } ,
109- cancellationToken ) ;
124+ // return client.UploadJsonAsync(
125+ // googleBigQueryAttribute.DatasetId,
126+ // $"{googleBigQueryAttribute.TableId}${date:yyyyMMdd}",
127+ // tableSchema,
128+ // lines,
129+ // new UploadJsonOptions() { AllowUnknownFields = true },
130+ // cancellationToken);
110131 }
111132 }
112133
0 commit comments