Skip to content
This repository was archived by the owner on Jul 8, 2024. 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.riskidentdms.spark.google.spreadsheets

import com.google.api.services.sheets.v4.SheetsScopes
import com.google.auth.oauth2.{GoogleCredentials, OAuth2Credentials}

import java.time.Duration
import scala.collection.JavaConverters.seqAsJavaListConverter
import scala.io.Source

object Credentials {
private val scopes = List(SheetsScopes.SPREADSHEETS)

def credentialsFromFile(file: String): OAuth2Credentials = {
val lines = Source.fromFile(file)
try {
credentialsFromJsonString(lines.getLines().mkString)
} finally {
lines.close()
}
}

def credentialsFromJsonString(oauth2JSON: String): OAuth2Credentials = {
val credentials: GoogleCredentials = GoogleCredentials.fromStream(
new java.io.ByteArrayInputStream(oauth2JSON.getBytes(java.nio.charset.StandardCharsets.UTF_8))
).createScoped(scopes.asJava)

credentials.refreshIfExpired()
val accessToken = credentials.refreshAccessToken()

GoogleCredentials.newBuilder()
.setAccessToken(accessToken)
.setRefreshMargin(Duration.ofDays(1))
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package com.github.riskidentdms.spark.google.spreadsheets

import com.github.riskidentdms.spark.google.spreadsheets.util.Credentials
import org.apache.spark.sql.sources.{BaseRelation, CreatableRelationProvider, RelationProvider, SchemaRelationProvider}
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, SQLContext, SaveMode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import java.util.{List => JavaList}
import scala.collection.JavaConverters._
import com.google.api.client.json.gson.GsonFactory
import com.google.auth.http.HttpCredentialsAdapter
import com.google.auth.oauth2.OAuth2Credentials

import scala.Option.option2Iterable
import scala.util.Try

object SparkSpreadsheetService {
private val APP_NAME = "spark-google-spreadsheets-1.0.0"
private val APP_NAME = "spark-google-spreadsheets"
private val HTTP_TRANSPORT: NetHttpTransport =
GoogleNetHttpTransport.newTrustedTransport()
private val JSON_FACTORY: GsonFactory = GsonFactory.getDefaultInstance
Expand Down Expand Up @@ -94,10 +95,10 @@ object SparkSpreadsheetService {
}
}

case class SparkSpreadsheetContext(credentials: HttpCredentialsAdapter) {
case class SparkSpreadsheetContext(credentials: OAuth2Credentials) {

lazy val service: Sheets =
new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpCredentialsAdapter(credentials))
.setApplicationName(APP_NAME)
.build()

Expand Down Expand Up @@ -267,7 +268,7 @@ object SparkSpreadsheetService {
* @param credentials
* @return
*/
def apply(credentials: HttpCredentialsAdapter): SparkSpreadsheetContext =
def apply(credentials: OAuth2Credentials): SparkSpreadsheetContext =
SparkSpreadsheetContext(credentials)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.riskidentdms.spark.google.spreadsheets

import com.google.api.services.sheets.v4.model.{CellData, ExtendedValue, RowData}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.riskidentdms.spark.google.spreadsheets.util

import java.math.BigDecimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*/
package com.github.riskidentdms.spark.google.spreadsheets

import com.github.riskidentdms.spark.google.spreadsheets.util.Credentials

import org.scalatest.BeforeAndAfter
import org.scalatest.flatspec.AnyFlatSpec

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.github.riskidentdms.spark.google.spreadsheets

import SparkSpreadsheetService.SparkSpreadsheet
import com.github.riskidentdms.spark.google.spreadsheets.util.Credentials
import com.github.riskidentdms.spark.google.spreadsheets.SparkSpreadsheetService.SparkSpreadsheet
import com.google.api.services.sheets.v4.model.{CellData, ExtendedValue, RowData}
import org.apache.spark.sql.types.{DataTypes, StructField, StructType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.github.riskidentdms.spark.google.spreadsheets

import SparkSpreadsheetService.SparkSpreadsheetContext
import com.github.riskidentdms.spark.google.spreadsheets.util.Credentials
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types._
import org.apache.spark.sql.{DataFrame, Row, SQLContext, SparkSession}
Expand Down Expand Up @@ -107,9 +106,9 @@ class SpreadsheetSuite extends AnyFlatSpec with BeforeAndAfter {

trait PersonData {
val personsSchema = StructType(List(
StructField("id", IntegerType, true),
StructField("firstname", StringType, true),
StructField("lastname", StringType, true)))
StructField("id", IntegerType, nullable = true),
StructField("firstname", StringType, nullable = true),
StructField("lastname", StringType, nullable = true)))
}

trait PersonDataFrame extends PersonData {
Expand Down Expand Up @@ -139,7 +138,6 @@ class SpreadsheetSuite extends AnyFlatSpec with BeforeAndAfter {
behavior of "A DataFrame"

it should "be saved as a sheet" in new PersonDataFrame {
import com.github.riskidentdms.spark.google.spreadsheets._
withEmptyWorksheet { workSheetName =>
personsDF.write
.option("credentialsJson", oAuthJson)
Expand Down Expand Up @@ -238,7 +236,7 @@ class SpreadsheetSuite extends AnyFlatSpec with BeforeAndAfter {
}

trait UnderscoreDataFrame {
val aSchema: StructType = StructType(List(StructField("foo_bar", IntegerType, true)))
val aSchema: StructType = StructType(List(StructField("foo_bar", IntegerType, nullable = true)))
val aRows = Seq(Row(1), Row(2), Row(3))
val aRDD: RDD[Row] = sqlContext.sparkContext.parallelize(aRows)
val aDF: DataFrame = sqlContext.createDataFrame(aRDD, aSchema)
Expand Down