Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fromServiceAccountJsonInputStream #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions 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 @@ -155,7 +155,17 @@ object BigQuery {
scopes: Seq[String] = Seq(BigqueryScopes.BIGQUERY)
): BigQuery = {

val credential = using(new FileInputStream(serviceAccountJsonFilePath)) { in =>
BigQuery.fromServiceAccountJsonInputStream(new FileInputStream(serviceAccountJsonFilePath), transport, jsonFactory, scopes)
Copy link
Owner

Choose a reason for hiding this comment

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

the FileInputStream resource must be closed.

}

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
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)
}

Expand Down
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)
}
}
}
}