2015-06-29 34 views
17

Voglio archiviare gli argomenti Spark come il file di input, il file di output in un file di proprietà Java e passare quel file in Spark Driver. Sto utilizzando spark-submit per l'invio del lavoro ma non sono riuscito a trovare un parametro per passare il file delle proprietà. Hai qualche suggerimento?Come caricare il file delle proprietà java e usarlo in Spark?

+1

hai stancato questa opzione: --prop erties-file FILE Percorso di un file da cui caricare proprietà extra –

risposta

28

qui ho trovato una soluzione: il file

oggetti di scena: (mypropsfile.conf) // nota: Affisso la chiave con "scintilla". altri oggetti di scena saranno ignorati.

spark.myapp.input /input/path 
spark.myapp.output /output/path 

lancio

$SPARK_HOME/bin/spark-submit --properties-file mypropsfile.conf 

come chiamare in codice :(all'interno del codice)

sc.getConf.get("spark.driver.host") // localhost 
sc.getConf.get("spark.myapp.input")  // /input/path 
sc.getConf.get("spark.myapp.output")  // /output/path 
+1

+ stesso trucco funzionerà anche per la scintilla shell ... –

+0

grazie :-) Ha funzionato a meraviglia! – diplomaticguru

+0

@ ramisetty.vijay: l'estensione del file dovrebbe essere .conf oppure potremmo usare anche .properties? – Shankar

3

L'approccio della precedente risposta ha la limitazione che è ogni proprietà dovrebbe iniziare con spark nel file di proprietà-

ad es.

spark.myapp.input
spark.myapp.output

Se supponiamo di avere una proprietà che non inizia con spark:

job.property:

app.name = xyz

$SPARK_HOME/bin/spark-submit --properties-file job.property 

Spark ignorerà tutte le proprietà non ha prefisso spark. con il messaggio:

Attenzione: Ignorando proprietà config non scintilla: app.name = test

Come gestire il file di proprietà nel driver dell'applicazione ed esecutore:

${SPARK_HOME}/bin/spark-submit --files job.properties 

Codice Java per accedere al file di cache (processo.proprietà):

import java.util.Properties; 
import org.apache.hadoop.fs.FSDataInputStream; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 
import org.apache.spark.SparkFiles; 

//Load file to propert object using HDFS FileSystem 
String fileName = SparkFiles.get("job.properties") 
Configuration hdfsConf = new Configuration(); 
FileSystem fs = FileSystem.get(hdfsConf); 

//THe file name contains absolute path of file 
FSDataInputStream is = fs.open(new Path(fileName)); 
Properties prop = new Properties(); 
//load properties 
prop.load(is) 
//retrieve properties 
prop.getProperty("app.name"); 

Se avete ambiente specifiche proprietà (dev/test/prod) quindi forniscono variabile d'ambiente Java personalizzato APP_ENV in spark-submit:

${SPARK_HOME}/bin/spark-submit --conf \ 
"spark.driver.extraJavaOptions=-DAPP_ENV=dev spark.executor.extraJavaOptions=-DAPP_ENV=dev" \ 
--properties-file dev.property 

sostituire il driver o esecutore codice:

//Load file to propert object using HDFS FileSystem 
String fileName = SparkFiles.get(System.getProperty("APP_ENV")+".properties")