Skip to content

Commit

Permalink
Update sheets api v4 (#4)
Browse files Browse the repository at this point in the history
* Migrate to the Google Sheets API v4

* Update README.md
  • Loading branch information
potix2 authored Sep 13, 2016
1 parent c8190a0 commit 05804a2
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 118 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ Google Spreadsheets datasource for [SparkSQL and DataFrames](http://spark.apache

[![Build Status](https://travis-ci.org/potix2/spark-google-spreadsheets.svg?branch=master)](https://travis-ci.org/potix2/spark-google-spreadsheets)

## Notice

The latest version (0.4.0) breaks compatibility with previous version. You must
use a ** spreadsheetId ** to identify which spreadsheets is to be accessed or altered.
On older versions a spreadsheet name is used.

If you don't know spreadsheetId, please read the [Introduction to the Google Sheets API v4](https://developers.google.com/sheets/guides/concepts).

## Requirements

## Linking

Using SBT:

```
libraryDependenicies += "com.github.potix2" %% "spark-google-spreadsheets" % "0.3.1"
libraryDependenicies += "com.github.potix2" %% "spark-google-spreadsheets" % "0.4.0"
```

Using Maven:
Expand All @@ -20,7 +28,7 @@ Using Maven:
<dependency>
<groupId>com.github.potix2<groupId>
<artifactId>spark-google-spreadsheets_2.11</artifactId>
<version>0.3.1</version>
<version>0.4.0</version>
</dependency>
```

Expand All @@ -30,7 +38,7 @@ Using Maven:
CREATE TABLE cars
USING com.github.potix2.spark.google.spreadsheets
OPTIONS (
path "YourSpreadsheet/worksheet1",
path "<spreadsheetId>/worksheet1",
serviceAccountId "[email protected]",
credentialPath "/path/to/credentail.p12"
)
Expand All @@ -48,20 +56,20 @@ val df = sqlContext.read.
format("com.github.potix2.spark.google.spreadsheets").
option("serviceAccountId", "[email protected]").
option("credentialPath", "/path/to/credentail.p12").
load("YourSpreadsheet/worksheet1")
load("<spreadsheetId>/worksheet1")

// Saves a DataFrame to a new worksheet
df.write.
format("com.github.potix2.spark.google.spreadsheets").
option("serviceAccountId", "[email protected]").
option("credentialPath", "/path/to/credentail.p12").
save("YourSpreadsheet/newWorksheet")
save("<spreadsheetId>/newWorksheet")

```

## License

Copyright 2015, Katsunori Kanda
Copyright 2016, Katsunori Kanda

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

Expand Down
13 changes: 7 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name := "spark-google-spreadsheets"

organization := "com.github.potix2"

scalaVersion := "2.11.7"
scalaVersion := "2.11.8"

crossScalaVersions := Seq("2.10.6", "2.11.7")
crossScalaVersions := Seq("2.10.6", "2.11.8")

version := "0.3.1-SNAPSHOT"
version := "0.4.0-SNAPSHOT"

spName := "potix2/spark-google-spreadsheets"

Expand All @@ -16,7 +16,7 @@ spIncludeMaven := true

spIgnoreProvided := true

sparkVersion := "1.5.0"
sparkVersion := "1.6.2"

val testSparkVersion = settingKey[String]("The version of Spark to test against.")

Expand All @@ -27,9 +27,10 @@ sparkComponents := Seq("sql")
libraryDependencies ++= Seq(
"org.slf4j" % "slf4j-api" % "1.7.5" % "provided",
"org.scalatest" %% "scalatest" % "2.2.1" % "test",
("com.google.api-client" % "google-api-client" % "1.20.0").
("com.google.api-client" % "google-api-client" % "1.22.0").
exclude("com.google.guava", "guava-jdk5"),
"com.google.gdata" % "core" % "1.47.1"
"com.google.oauth-client" % "google-oauth-client-jetty" % "1.22.0",
"com.google.apis" % "google-api-services-sheets" % "v4-rev18-1.22.0"
)

libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package com.github.potix2.spark.google.spreadsheets

import java.io.File

import com.github.potix2.spark.google.spreadsheets.SparkSpreadsheetService.{SparkSpreadsheet, SparkWorksheet}
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 Expand Up @@ -44,23 +43,13 @@ class DefaultSource extends RelationProvider with SchemaRelationProvider with Cr


override def createRelation(sqlContext: SQLContext, mode: SaveMode, parameters: Map[String, String], data: DataFrame): BaseRelation = {
def createWorksheet(spreadsheet: SparkSpreadsheet, worksheetName: String)
(implicit context:SparkSpreadsheetService.SparkSpreadsheetContext): SparkWorksheet = {
val columns = data.schema.fieldNames
val worksheet = spreadsheet.addWorksheet(worksheetName, columns.length, data.count().toInt)
worksheet.insertHeaderRow(columns)

worksheet
}

val (spreadsheetName, worksheetName) = pathToSheetNames(parameters)
implicit val context = createSpreadsheetContext(parameters)
val spreadsheet = SparkSpreadsheetService.findSpreadsheet(spreadsheetName)
if(!spreadsheet.isDefined)
throw new RuntimeException(s"no such a spreadsheet: $spreadsheetName")

val worksheet = createWorksheet(spreadsheet.get, worksheetName)
data.collect().foreach(row => worksheet.insertRow(Util.convert(data.schema, row)))
spreadsheet.get.addWorksheet(worksheetName, data.schema, data.collect().toList, Util.toRowData)
createRelation(sqlContext, context, spreadsheetName, worksheetName, data.schema)
}

Expand Down
Loading

0 comments on commit 05804a2

Please sign in to comment.