Skip to content

Instantly share code, notes, and snippets.

@serafdev
Last active September 26, 2018 12:26
Show Gist options
  • Save serafdev/8af63c8438e506316a9ffbb84cadfdd6 to your computer and use it in GitHub Desktop.
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
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