2017-10-11 5 views
0

私はIP上で動作しているSpark 2.1.1クラスタとJupyterノートブックの「スパークポート://リモート」pysparkを使用しています私は(スパークマスターIP) をSparkContextを正常に作成できます。私はSparkContextを(取得する前に、ファイルを開いた場合Pysparkは、Javaゲートウェイの例外がスローされます)

しかし、ハード・コーディングではなく、.propertiesファイルからspark_master_ipとspark.cores.maxを読みたい。私は私のカスタムスパークプロパティを読み取るしようとすると(私が解析し、正常に読み込まれた)「myspark_config.properties」ファイルにファイルを、私は()SparkContextを作成しようとすると、私は、次のJavaゲートウェイの例外を取得します。 は、ここに私のコードです:

import pyspark 
from pprint import pprint 
from pyspark import SparkConf 
def getproperties(): 
    """Get Spark configuration properties in python dictionary""" 
    global properties 
    properties = dict() 
    with open('myspark_config.properties') as f: 
     for line in f: 
       if not line.startswith('#') and not line.startswith('\n'): 
        tokens = line.split('=') 
        tokens[0] = tokens[0].strip() 
        tokens[1] = "=".join(tokens[1:]) 
        properties[tokens[0]] = tokens[1].strip() 

     f.close() 
    pprint(properties) 
    return(properties) 
properties = getproperties() 

conf = (SparkConf() 
      .setMaster(properties["spark_master_url"]) 
      .setAppName("testApp") 
      .set('spark.cores.max',properties["spark_app_cores"]) 
      .set('spark.executor.memory',properties["spark_app_memory"]) 
      .set('spark.dynamicAllocation.enabled','true') 
      .set('spark.shuffle.service.enabled','true') 

      ) 
# conf = (SparkConf() 
#    .setMaster("spark://remote:port") 
#    .setAppName("testApp") 
#    .set('spark.cores.max',"2") 
#    .set('spark.executor.memory',"2G") 
#    .set('spark.dynamicAllocation.enabled','true') 
#    .set('spark.shuffle.service.enabled','true') 
#  ) 
sc = pyspark.SparkContext(conf=conf) 

私は例外を取得しないと私は、ファイルやハードコードからSparkConfでスパークマスター()(現在はそれがコメントしている)を読んでいない場合は、私のコードがスムーズに実行されます。 「JAVA_HOME」、「SPARK_HOME」「PYTHONPATHは」正常に設定されていると私はアナコンダを使用していません。私はUbuntuと火花2.1.1

{'spark_app_cores': '"2"', 
'spark_app_memory': '"2G"', 
'spark_master_url': '"spark://remote:port"'} 

Exception         Traceback (most recent call last) 
<ipython-input-1-c893eaf079f2> in <module>() 
    36 #    .set('spark.shuffle.service.enabled','true') 
    37 #  ) 
---> 38 sc = pyspark.SparkContext(conf=conf) 

/usr/local/spark/python/pyspark/context.py in __init__(self, master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, gateway, jsc, profiler_cls) 
    113   """ 
    114   self._callsite = first_spark_call() or CallSite(None, None, None) 
--> 115   SparkContext._ensure_initialized(self, gateway=gateway, conf=conf) 
    116   try: 
    117    self._do_init(master, appName, sparkHome, pyFiles, environment, batchSize, serializer, 

/usr/local/spark/python/pyspark/context.py in _ensure_initialized(cls, instance, gateway, conf) 
    281   with SparkContext._lock: 
    282    if not SparkContext._gateway: 
--> 283     SparkContext._gateway = gateway or launch_gateway(conf) 
    284     SparkContext._jvm = SparkContext._gateway.jvm 
    285 

/usr/local/spark/python/pyspark/java_gateway.py in launch_gateway(conf) 
    93     callback_socket.close() 
    94   if gateway_port is None: 
---> 95    raise Exception("Java gateway process exited before sending the driver its port number") 
    96 
    97   # In Windows, ensure the Java child processes do not linger after Python has exited. 

Exception: Java gateway process exited before sending the driver its port number 

私は様々なリンクを介して見てきた上でのpython 2.7にJupyterノートブックを使用していないが、見つかりませ解決しています: pyspark java gateway error stackoverflow Github issue on java Gateway

答えて

0

をしかし、私が読みたいです(代わりにそれをハードコーディングする).propertiesファイルからspark_master_ipとspark.cores.max。素晴らしいアイデアをされていますが、これは$SPARK_HOME/conf/spark-defaults.confが何のためにあるのかであることを、事実を無視している

。そこに必要なプロパティを置くだけです。

が、私はこれは間違って見える

、次のJavaゲートウェイ例外を取得:

"=".join(tokens[1:]) 

プロパティで=を使用するのはなぜ?

それ以外の場合は効果がありません必要があります。また、Pythonは私が(API_KEYのようにデータベースのREST APIとtable_namesを呼び出すために)私自身の性質が自分のアプリケーションに固有のプロパティを設定する必要があるファイルのプロパティパーサhttps://docs.python.org/3/library/configparser.html

+0

を提供します。例api_key = "shajkhsakj = sdi23"これはプロパティファイル内の1つのエントリです。私はconfigparserやjpropertiesのような第三者のライブラリを使用することは許されていません。私はオープン()コマンドを使用して、Pythonでファイルを開こうとする場合、私は()SparkContextを初期化する前に、エラーを取得しない理由しかし、私の主な問題はあります。 –

関連する問題