Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/main/scala/bigquery4s/BigQuery.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 =>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the passed resource is closed here. I don't think it's a good approach. Closing a resource near its creation is easy to maintain.

GoogleCredential.fromStream(in).createScoped(scopes.asJava)
}

BigQuery(transport, jsonFactory, credential)
}

}
77 changes: 77 additions & 0 deletions src/test/scala/bigquery4s/ServiceAccountUsageExamplesSpec.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}