@@ -20,8 +20,39 @@ public BigQueryService(GoogleBigQueryAttribute googleBigQueryAttribute, Type ite
2020 ( this . tableSchema , this . dictionaryOfProperties ) = TableSchemaBuilderService . GetTableSchema ( itemType ) ;
2121 }
2222
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+
2349 private Task < BigQueryTable > GetTable ( DateTime date , CancellationToken cancellationToken ) {
50+ BigQueryClient client = GetBiqQueryClient ( ) ;
2451
52+ return client . GetTableAsync ( googleBigQueryAttribute . DatasetId , $ "{ googleBigQueryAttribute . TableId } ${ date : yyyyMMdd} ", null , cancellationToken ) ;
53+ }
54+
55+ private BigQueryClient GetBiqQueryClient ( ) {
2556 GoogleCredential googleCredential = null ;
2657 if ( googleBigQueryAttribute . Credentials != null ) {
2758 googleCredential = GoogleCredential . FromStream ( new System . IO . MemoryStream ( googleBigQueryAttribute . Credentials ) ) ;
@@ -34,36 +65,49 @@ private Task<BigQueryTable> GetTable(DateTime date, CancellationToken cancellati
3465 }
3566 }
3667 var client = BigQueryClient . Create ( googleBigQueryAttribute . ProjectId , googleCredential ) ;
37-
38- //return client.GetOrCreateTableAsync(datasetId, tableId, tableSchema, null, new CreateTableOptions() { TimePartitioning = new TimePartitioning() { Type = "DAY" } }, cancellationToken)
39- // .ContinueWith((createTableTask) => {
40- // return client.GetTableAsync(datasetId, $"{tableId}${date:yyyyMMdd}", null, cancellationToken);
41- // }, cancellationToken).Unwrap();
42- return client . GetTableAsync ( googleBigQueryAttribute . DatasetId , $ "{ googleBigQueryAttribute . TableId } ${ date : yyyyMMdd} ", null , cancellationToken ) ;
68+ return client ;
4369 }
4470
4571 public Task InsertRowsAsync ( DateTime date , IEnumerable < GoogleBigQueryRow > rows , CancellationToken cancellationToken ) {
4672
4773 if ( rows != null && rows . Count ( ) > 0 ) {
4874 int dateDiff = ( date - DateTime . UtcNow . Date ) . Days ;
4975
50- if ( dateDiff >= - 31 && dateDiff <= 16 ) {
76+ if ( dateDiff >= - 31 && dateDiff <= 16 )
77+ {
5178
5279 var bigQueryRows = rows . Select ( c => BigQueryInsertRowService . GetBigQueryInsertRow ( c , dictionaryOfProperties ) ) ;
5380
5481 return GetTable ( date , cancellationToken )
55- . ContinueWith ( ( tableTask ) => {
82+ . ContinueWith ( ( tableTask ) =>
83+ {
5684 BigQueryTable table = tableTask . Result ;
5785
5886 return table . InsertRowsAsync ( bigQueryRows , new InsertOptions ( ) { AllowUnknownFields = true } , cancellationToken )
59- . ContinueWith ( ( insertRowsTask ) => {
60- if ( insertRowsTask . IsFaulted ) {
87+ . ContinueWith ( ( insertRowsTask ) =>
88+ {
89+ if ( insertRowsTask . IsFaulted )
90+ {
6191 throw insertRowsTask . Exception . InnerExceptions . First ( ) ;
6292 }
6393 } ) ;
6494 } , cancellationToken ) . Unwrap ( ) ;
6595
6696 }
97+ else {
98+
99+ BigQueryClient client = GetBiqQueryClient ( ) ;
100+
101+ IEnumerable < string > lines = BigQueryInsertRowService . GetBigQueryJobLines ( rows ) ;
102+
103+ return client . UploadJsonAsync (
104+ googleBigQueryAttribute . DatasetId ,
105+ $ "{ googleBigQueryAttribute . TableId } ${ date : yyyyMMdd} ",
106+ tableSchema ,
107+ lines ,
108+ new UploadJsonOptions ( ) { AllowUnknownFields = true } ,
109+ cancellationToken ) ;
110+ }
67111 }
68112
69113 return Task . WhenAll ( ) ;
0 commit comments