Last active
September 26, 2018 12:26
-
-
Save serafdev/8af63c8438e506316a9ffbb84cadfdd6 to your computer and use it in GitHub Desktop.
BigQuery Utils to run Queries and have a StringContext helper that will run the Query and return a Scala List of FieldValueList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.cloud.bigquery._ | |
import scala.collection.JavaConverters._ | |
object BQUtils { | |
private[this] def runQuery(queryString: String)(implicit bigQuery: BigQuery): TableResult = { | |
val queryConfig: QueryJobConfiguration = | |
QueryJobConfiguration.newBuilder(queryString).setUseLegacySql(false).setUseQueryCache(true).build() | |
val jobId: JobId = JobId.of(java.util.UUID.randomUUID.toString) | |
val queryJob: Job = bigQuery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build()).waitFor() | |
if (queryJob == null) { throw new RuntimeException("Query job no longer exists") } | |
else if (queryJob.getStatus.getError != null) { throw new RuntimeException(queryJob.getStatus.getError.toString) } | |
queryJob.getQueryResults() | |
} | |
implicit class BigQuerySQL(private val sc: StringContext) extends AnyVal { | |
def bq(args: Any*)(implicit bigQuery: BigQuery): List[FieldValueList] = { | |
runQuery(args.mkString("")).iterateAll().asScala.toList | |
} | |
} | |
} | |
/* | |
import BQUtils._ | |
object example { | |
implicit bigQuery: BigQuery = ... | |
// Create an apply method to User that takes a "FieldValueList" parameter. | |
// Or map it manually which I don't suggest because of efficiency reasons. | |
bq"select * from $datasetName.$tableName".map(User(_)) | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment