2017-01-28 71 views
0

我使用https://github.com/potix2/spark-google-spreadsheets庫來讀取spark中的電子表格文件。它在我的地方完美運作。Google Spread Sheet Spark庫

val df = sqlContext.read. 
    format("com.github.potix2.spark.google.spreadsheets"). 
    option("serviceAccountId", "[email protected]"). 
    option("credentialPath", "/path/to/credentail.p12"). 
    load("<spreadsheetId>/worksheet1") 

我創建了一個新的裝配jar包含所有憑據,並使用該jar讀取文件。但是我在閱讀credentialPath文件時遇到問題。我試過用

getClass.getResourceAsStream("/resources/Aircraft/allAircraft.txt") 

但是庫只支持絕對路徑。請幫我解決這個問題。

+0

這可能是因爲將證書放入jar中是一個相當糟糕的主意。通過ENV傳遞或單獨部署。 – Reactormonk

+0

@Reactormonk,你可以提供一些建議//鏈接如何使用ENV。 thakns – John

+0

可能相關:https://softwareengineering.stackexchange.com/questions/205606/strategy-for-keeping-secret-info-such-as-api-keys-out-of-source-control – Reactormonk

回答

0

使用SBT並嘗試typesafe config library

這是一個simple but complete sample,它從位於resources文件夾中的配置文件讀取一些信息。

然後,您可以使用sbt-assembly插件組裝一個jar文件。

+0

使用類型安全配置庫來管理confiuraton非常簡單。感謝那。但我真的想從jar中獲取文件的文件位置。 http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file。是否有可能使用類型安全。我找不到這樣做的方法。 @amirkarimi – John

+0

正如你所說,圖書館只支持絕對路徑。將資源作爲流獲取,然後將其寫入物理文件並將文件路徑提供給庫?不過,它可能有一些嚴重的安全問題。 –

+0

順便說一句,試試這個:給這個憑證路徑:'ClassLoader.getSystemResource(「/ resources/...」)。toURI()'。 –

0

您可以使用--files參數​​或SparkContext.addFile()來分發憑證文件。如果要在工作節點中獲取憑證文件的本地路徑,則應調用SparkFiles.get("credential filename")

import org.apache.spark.SparkFiles 

// you can also use `spark-submit --files=credential.p12` 
sqlContext.sparkContext.addFile("credential.p12") 
val credentialPath = SparkFiles.get("credential.p12") 

val df = sqlContext.read. 
    format("com.github.potix2.spark.google.spreadsheets"). 
    option("serviceAccountId", "[email protected]"). 
    option("credentialPath", credentialPath). 
    load("<spreadsheetId>/worksheet1") 
+0

非常感謝,我會盡力讓你知道。順便說一句,有可能使用這個庫更新工作表。 – John

相關問題