@@ -12,21 +12,28 @@ import com.google.cloud.bigquery.ExternalTableDefinition
12
12
import com .google .cloud .bigquery .StandardTableDefinition
13
13
14
14
case class GCPFormatProvider (sparkSession : SparkSession ) extends FormatProvider {
15
- lazy val bigQueryClient = BigQueryOptions .getDefaultInstance.getService
15
+ lazy val bqOptions = BigQueryOptions .getDefaultInstance
16
+ lazy val bigQueryClient = bqOptions.getService
16
17
def readFormat (tableName : String ): Format = {
17
18
18
19
val btTableIdentifier : TableId = BigQueryUtil .parseTableId(tableName)
20
+
21
+ // Order of Precedence for Default Project
22
+ // Explicitly configured project in code (e.g., setProjectId()).
23
+ // GOOGLE_CLOUD_PROJECT environment variable.
24
+ // project_id from the ADC service account JSON file.
25
+ // Active project in the gcloud CLI configuration.
26
+ // No default project: An error will occur if no project ID is available.
27
+
19
28
val unshadedTI : BTableId =
20
- BTableId .of(btTableIdentifier.getProject , btTableIdentifier.getDataset, btTableIdentifier.getTable)
29
+ BTableId .of(bqOptions.getProjectId , btTableIdentifier.getDataset, btTableIdentifier.getTable)
21
30
22
31
val tableOpt = Option (bigQueryClient.getTable(unshadedTI))
23
-
24
32
tableOpt match {
25
33
case Some (table) => {
26
- table.getDefinition match {
27
- case ExternalTableDefinition => BQuery (unshadedTI.getProject)
28
- case StandardTableDefinition => GCS (unshadedTI.getProject)
29
- }
34
+ if (table.getDefinition.isInstanceOf [ExternalTableDefinition ]) GCS (unshadedTI.getProject)
35
+ else if (table.getDefinition.isInstanceOf [StandardTableDefinition ]) BQuery (unshadedTI.getProject)
36
+ else throw new IllegalStateException (s " Cannot support table of type: ${table.getDefinition}" )
30
37
}
31
38
case None => Hive
32
39
}
0 commit comments