1111import com .google .cloud .bigquery .TableDefinition ;
1212import com .google .cloud .bigquery .TableId ;
1313import io .cdap .cdap .api .data .schema .Schema ;
14+ import io .cdap .cdap .api .metrics .Metrics ;
1415import io .cdap .cdap .etl .api .engine .sql .SQLEngineException ;
1516import io .cdap .cdap .etl .api .engine .sql .dataset .SQLDataset ;
1617import io .cdap .plugin .gcp .bigquery .sink .BigQuerySinkUtils ;
1920import org .slf4j .LoggerFactory ;
2021
2122import java .util .Collections ;
23+ import java .util .HashMap ;
24+ import java .util .Map ;
2225import javax .annotation .Nullable ;
2326
2427/**
@@ -38,6 +41,7 @@ public class BigQuerySelectDataset implements SQLDataset, BigQuerySQLDataset {
3841 private final String jobId ;
3942 private final BigQueryJobType operation ;
4043 private final String selectQuery ;
44+ private final Metrics metrics ;
4145 private Long numRows ;
4246
4347 public static BigQuerySelectDataset getInstance (String datasetName ,
@@ -49,7 +53,8 @@ public static BigQuerySelectDataset getInstance(String datasetName,
4953 String bqTable ,
5054 String jobId ,
5155 BigQueryJobType jobType ,
52- String selectQuery ) {
56+ String selectQuery ,
57+ Metrics metrics ) {
5358
5459 return new BigQuerySelectDataset (datasetName ,
5560 outputSchema ,
@@ -60,7 +65,8 @@ public static BigQuerySelectDataset getInstance(String datasetName,
6065 bqTable ,
6166 jobId ,
6267 jobType ,
63- selectQuery );
68+ selectQuery ,
69+ metrics );
6470 }
6571
6672 private BigQuerySelectDataset (String datasetName ,
@@ -72,7 +78,8 @@ private BigQuerySelectDataset(String datasetName,
7278 String bqTable ,
7379 String jobId ,
7480 BigQueryJobType operation ,
75- String selectQuery ) {
81+ String selectQuery ,
82+ Metrics metrics ) {
7683 this .datasetName = datasetName ;
7784 this .outputSchema = outputSchema ;
7885 this .sqlEngineConfig = sqlEngineConfig ;
@@ -83,6 +90,7 @@ private BigQuerySelectDataset(String datasetName,
8390 this .jobId = jobId ;
8491 this .operation = operation ;
8592 this .selectQuery = selectQuery ;
93+ this .metrics = metrics ;
8694 }
8795
8896 public BigQuerySelectDataset execute () {
@@ -124,14 +132,14 @@ public BigQuerySelectDataset execute() {
124132 if (queryJob == null ) {
125133 throw new SQLEngineException ("BigQuery job not found: " + jobId );
126134 } else if (queryJob .getStatus ().getError () != null ) {
127- BigQuerySQLEngineUtils .logJobMetrics (queryJob );
135+ BigQuerySQLEngineUtils .logJobMetrics (queryJob , metrics );
128136 throw new SQLEngineException (String .format (
129137 "Error executing BigQuery Job: '%s' in Project '%s', Dataset '%s', Location'%s' : %s" ,
130138 jobId , project , bqDataset , location , queryJob .getStatus ().getError ().toString ()));
131139 }
132140
133141 LOG .info ("Created BigQuery table `{}` using Job: {}" , bqTable , jobId );
134- BigQuerySQLEngineUtils .logJobMetrics (queryJob );
142+ BigQuerySQLEngineUtils .logJobMetrics (queryJob , metrics );
135143 return this ;
136144 }
137145
0 commit comments