-
Notifications
You must be signed in to change notification settings - Fork 11
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
vijaykramesh
wants to merge
2
commits into
seratch:master
Choose a base branch
from
vijaykramesh:service_account_from_input_stream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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 | ||
|
@@ -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) | ||
} | ||
|
||
def fromServiceAccountJsonInputStream( | ||
serviceAccountJsonInputStream: InputStream, | ||
transport: HttpTransport = new NetHttpTransport, | ||
jsonFactory: JsonFactory = new JacksonFactory, | ||
scopes: Seq[String] = Seq(BigqueryScopes.BIGQUERY) | ||
): BigQuery = { | ||
|
||
val credential = using(serviceAccountJsonInputStream) { in => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
|
77 changes: 77 additions & 0 deletions
77
src/test/scala/bigquery4s/ServiceAccountUsageExamplesSpec.scala
This file contains 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
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) | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.