diff --git a/src/main/scala/bigquery4s/BigQuery.scala b/src/main/scala/bigquery4s/BigQuery.scala index 9400123..50da2b9 100644 --- a/src/main/scala/bigquery4s/BigQuery.scala +++ b/src/main/scala/bigquery4s/BigQuery.scala @@ -1,6 +1,6 @@ package bigquery4s -import java.io.{ File, InputStreamReader, FileInputStream } +import java.io.{ File, InputStream, InputStreamReader, FileInputStream } import com.google.api.client.auth.oauth2.Credential import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp @@ -162,4 +162,18 @@ object BigQuery { BigQuery(transport, jsonFactory, credential) } + def fromServiceAccountJsonInputStream( + serviceAccountJsonInputStream: InputStream, + transport: HttpTransport = new NetHttpTransport, + jsonFactory: JsonFactory = new JacksonFactory, + scopes: Seq[String] = Seq(BigqueryScopes.BIGQUERY) + ): BigQuery = { + + val credential = using(serviceAccountJsonInputStream) { in => + GoogleCredential.fromStream(in).createScoped(scopes.asJava) + } + + BigQuery(transport, jsonFactory, credential) + } + } \ No newline at end of file diff --git a/src/test/scala/bigquery4s/ServiceAccountUsageExamplesSpec.scala b/src/test/scala/bigquery4s/ServiceAccountUsageExamplesSpec.scala new file mode 100644 index 0000000..44db298 --- /dev/null +++ b/src/test/scala/bigquery4s/ServiceAccountUsageExamplesSpec.scala @@ -0,0 +1,77 @@ +package bigquery4s + +import java.io.FileInputStream + +import org.scalatest._ +import org.slf4j.LoggerFactory + +/** + * requirement: $HOME/.bigquery/service_account.json + */ +class ServiceAccountUsageExamplesSpec extends FunSpec with Matchers { + + lazy val logger = LoggerFactory.getLogger(classOf[UsageExamplesSpec]) + val yourOwnProjectId = "thinking-digit-98014" + + describe("Simple query example with publicdata using service account json file") { + it("runs") { + val bq = BigQuery.fromServiceAccountJson() + + val datasets: Seq[WrappedDatasets] = bq.listDatasets("publicdata") + logger.info(datasets.mkString("\n")) + + val query = """ + SELECT weight_pounds,state,year,gestation_weeks FROM publicdata:samples.natality + ORDER BY weight_pounds DESC LIMIT 100; + """ + val jobId: JobId = bq.startQuery(yourOwnProjectId, query) + logger.info(s"JobId: $jobId") + + val job: WrappedCompletedJob = bq.await(jobId) + logger.info(s"Job: $job") + + val rows: Seq[WrappedTableRow] = bq.getRows(job) + val sampleCsv = rows.take(20).map(_.cells.map(_.value.orNull).mkString(",")) + logger.info(s""" + |*** QueryResult *** + | + |weight_pounds,state,year,gestation_weeks + |${sampleCsv.mkString("\n")} + |""".stripMargin) + } + } + + describe("Simple query example with publicdata using service account json string") { + it("runs") { + using(new FileInputStream(homeDir + "/.bigquery/service_account.json")) { in => + val bq = BigQuery.fromServiceAccountJsonInputStream(in) + + val datasets: Seq[WrappedDatasets] = bq.listDatasets("publicdata") + logger.info(datasets.mkString("\n")) + + val query = + """ + SELECT weight_pounds,state,year,gestation_weeks FROM publicdata:samples.natality + ORDER BY weight_pounds DESC LIMIT 100; + """ + val jobId: JobId = bq.startQuery(yourOwnProjectId, query) + logger.info(s"JobId: $jobId") + + val job: WrappedCompletedJob = bq.await(jobId) + logger.info(s"Job: $job") + + val rows: Seq[WrappedTableRow] = bq.getRows(job) + val sampleCsv = rows.take(20).map(_.cells.map(_.value.orNull).mkString(",")) + logger.info(s""" + |*** QueryResult *** + | + |weight_pounds,state,year,gestation_weeks + |${ + sampleCsv. + mkString("\n") + } + |""".stripMargin) + } + } + } +}