當前位置: 首頁>>代碼示例>>Python>>正文


Python protocol.Py4JJavaError方法代碼示例

本文整理匯總了Python中py4j.protocol.Py4JJavaError方法的典型用法代碼示例。如果您正苦於以下問題:Python protocol.Py4JJavaError方法的具體用法?Python protocol.Py4JJavaError怎麽用?Python protocol.Py4JJavaError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在py4j.protocol的用法示例。


在下文中一共展示了protocol.Py4JJavaError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: install_exception_handler

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def install_exception_handler():
    """
    Hook an exception handler into Py4j, which could capture some exceptions in Java.

    When calling Java API, it will call `get_return_value` to parse the returned object.
    If any exception happened in JVM, the result will be a Java exception object, it raise
    py4j.protocol.Py4JJavaError. We replace the original `get_return_value` with one that
    could capture the Java exception and throw a Python one (with the same error message).

    It's idempotent, could be called multiple times.
    """
    original = py4j.protocol.get_return_value
    # The original `get_return_value` is not patched, it's idempotent.
    patched = capture_java_exception(original)
    # only patch the one used in py4j.java_gateway (call Java API)
    py4j.java_gateway.get_return_value = patched 
開發者ID:huseinzol05,項目名稱:Gather-Deployment,代碼行數:18,代碼來源:exceptions.py

示例2: run

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def run(self):
        while True:
            try:
                self.connect_to_rabbitmq()
                self.init_rabbitmq_channels()
                current_app.logger.info('Request consumer started!')

                try:
                    self.request_channel.start_consuming()
                except pika.exceptions.ConnectionClosed as e:
                    current_app.logger.error('connection to rabbitmq closed: %s', str(e), exc_info=True)
                    self.rabbitmq.close()
                    continue
                self.rabbitmq.close()
            except Py4JJavaError as e:
                current_app.logger.critical("Critical: JAVA error in spark-request consumer: %s, message: %s", str(e), str(e.java_exception), exc_info=True)
                time.sleep(2)
            except Exception as e:
                current_app.logger.critical("Error in spark-request-consumer: %s", str(e), exc_info=True)
                time.sleep(2) 
開發者ID:metabrainz,項目名稱:listenbrainz-server,代碼行數:22,代碼來源:request_consumer.py

示例3: init_spark_session

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def init_spark_session(app_name):
    """ Initializes a Spark Session with the given application name.

        Args:
            app_name (str): Name of the Spark application. This will also occur in the Spark UI.
    """
    global session, context, sql_context
    try:
        session = SparkSession \
                .builder \
                .appName(app_name) \
                .config("spark.hadoop.dfs.client.use.datanode.hostname", "true") \
                .config("spark.hadoop.dfs.datanode.use.datanode.hostname", "true") \
                .config("spark.driver.maxResultSize", "4g") \
                .getOrCreate()
        context = session.sparkContext
        context.setLogLevel("ERROR")
        sql_context = SQLContext(context)
    except Py4JJavaError as err:
        raise SparkSessionNotInitializedException(app_name, err.java_exception) 
開發者ID:metabrainz,項目名稱:listenbrainz-server,代碼行數:22,代碼來源:__init__.py

示例4: compute_rmse

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def compute_rmse(model, data, n, model_id):
    """ Compute RMSE (Root Mean Squared Error).

        Args:
            model: Trained model.
            data (rdd): Rdd used for validation i.e validation_data
            n (int): Number of rows/elements in validation_data.
            model_id (str): Model identification string.
    """
    try:
        predictions = model.predictAll(data.map(lambda x: (x.user, x.product)))
        predictionsAndRatings = predictions.map(lambda x: ((x[0], x[1]), x[2])) \
                                           .join(data.map(lambda x: ((x[0], x[1]), x[2]))) \
                                           .values()
        return sqrt(predictionsAndRatings.map(lambda x: (x[0] - x[1]) ** 2).reduce(add) / float(n))
    except Py4JJavaError as err:
        current_app.logger.error('Root Mean Squared Error for model "{}" not computed\n{}'.format(
                                 model_id, str(err.java_exception)), exc_info=True)
        sys.exit(-1) 
開發者ID:metabrainz,項目名稱:listenbrainz-server,代碼行數:21,代碼來源:train_models.py

示例5: train

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def train(training_data, rank, iteration, lmbda, alpha, model_id):
    """ Train model.

        Args:
            training_data (rdd): Used for training.
            rank (int): Number of factors in ALS model.
            iteration (int): Number of iterations to run.
            lmbda (float): Controls regularization.
            alpha (float): Constant for computing confidence.
            model_id (str): Model identification string.

        Returns:
            model: Trained model.

    """
    try:
        model = ALS.trainImplicit(training_data, rank, iterations=iteration, lambda_=lmbda, alpha=alpha)
        return model
    except Py4JJavaError as err:
        current_app.logger.error('Unable to train model "{}"\n{}'.format(model_id, str(err.java_exception)), exc_info=True)
        sys.exit(-1) 
開發者ID:metabrainz,項目名稱:listenbrainz-server,代碼行數:23,代碼來源:train_models.py

示例6: save_model

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def save_model(model_id, model):
    """ Save model to HDFS.

        Args:
            model_id (str): Model identification string.
            model: Trained model
    """
    # delete previously saved model before saving a new model
    delete_model()

    dest_path = get_model_path(model_id)
    try:
        current_app.logger.info('Saving model...')
        model.save(listenbrainz_spark.context, dest_path)
        current_app.logger.info('Model saved!')
    except Py4JJavaError as err:
        current_app.logger.error('Unable to save model "{}"\n{}. Aborting...'.format(model_id,
                                 str(err.java_exception)), exc_info=True)
        sys.exit(-1) 
開發者ID:metabrainz,項目名稱:listenbrainz-server,代碼行數:21,代碼來源:train_models.py

示例7: get_mock_stage_completed

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def get_mock_stage_completed():
    def _inner(failure_reason):
        class JavaException:
            def __init__(self):
                self._target_id = "id"

        class FailureReason:
            def get(self):
                if failure_reason:
                    return "failure-reason"
                else:
                    raise Py4JJavaError("msg", JavaException())

        class StageInfo:
            def stageId(self):  # noqa: N802
                return "sample-stage-id-submit"

            def name(self):
                return "run-job"

            def attemptId(self):  # noqa: N802
                return 14

            def failureReason(self):  # noqa: N802
                return FailureReason()

        class MockStageCompleted:
            def stageInfo(self):  # noqa: N802
                return StageInfo()

        return MockStageCompleted()

    return _inner 
開發者ID:getsentry,項目名稱:sentry-python,代碼行數:35,代碼來源:test_spark.py

示例8: capture_java_exception

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def capture_java_exception(f):
    def deco(*a, **kw):
        try:
            return f(*a, **kw)
        except Py4JJavaError as e:
            s = e.java_exception.toString()
            stack_trace = '\n\t at '.join(map(lambda x: x.toString(),
                                              e.java_exception.getStackTrace()))
            for exception in exception_mapping.keys():
                if s.startswith(exception):
                    raise exception_mapping[exception](s.split(': ', 1)[1], stack_trace)
            raise
    return deco 
開發者ID:huseinzol05,項目名稱:Gather-Deployment,代碼行數:15,代碼來源:exceptions.py

示例9: get_private_field

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def get_private_field(java_obj, field_name):
    try:
        field = java_obj.getClass().getDeclaredField(field_name)
        field.setAccessible(True)
        return field.get(java_obj)
    except Py4JJavaError:
        cls = java_obj.getClass()
        while cls.getSuperclass() is not None:
            cls = cls.getSuperclass()
            try:
                field = cls.getDeclaredField(field_name)
                field.setAccessible(True)
                return field.get(java_obj)
            except Py4JJavaError:
                pass 
開發者ID:huseinzol05,項目名稱:Gather-Deployment,代碼行數:17,代碼來源:test_case_utils.py

示例10: test_stopiteration_is_raised

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def test_stopiteration_is_raised(self):

        def stopit(*args, **kwargs):
            raise StopIteration()

        def legit_create_combiner(x):
            return [x]

        def legit_merge_value(x, y):
            return x.append(y) or x

        def legit_merge_combiners(x, y):
            return x.extend(y) or x

        data = [(x % 2, x) for x in range(100)]

        # wrong create combiner
        m = ExternalMerger(Aggregator(stopit, legit_merge_value, legit_merge_combiners), 20)
        with self.assertRaises((Py4JJavaError, RuntimeError)) as cm:
            m.mergeValues(data)

        # wrong merge value
        m = ExternalMerger(Aggregator(legit_create_combiner, stopit, legit_merge_combiners), 20)
        with self.assertRaises((Py4JJavaError, RuntimeError)) as cm:
            m.mergeValues(data)

        # wrong merge combiners
        m = ExternalMerger(Aggregator(legit_create_combiner, legit_merge_value, stopit), 20)
        with self.assertRaises((Py4JJavaError, RuntimeError)) as cm:
            m.mergeCombiners(map(lambda x_y1: (x_y1[0], [x_y1[1]]), data)) 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:32,代碼來源:tests.py

示例11: test_pipe_functions

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def test_pipe_functions(self):
        data = ['1', '2', '3']
        rdd = self.sc.parallelize(data)
        with QuietTest(self.sc):
            self.assertEqual([], rdd.pipe('cc').collect())
            self.assertRaises(Py4JJavaError, rdd.pipe('cc', checkCode=True).collect)
        result = rdd.pipe('cat').collect()
        result.sort()
        for x, y in zip(data, result):
            self.assertEqual(x, y)
        self.assertRaises(Py4JJavaError, rdd.pipe('grep 4', checkCode=True).collect)
        self.assertEqual([], rdd.pipe('grep 4').collect()) 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:14,代碼來源:tests.py

示例12: test_stopiteration_in_user_code

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def test_stopiteration_in_user_code(self):

        def stopit(*x):
            raise StopIteration()

        seq_rdd = self.sc.parallelize(range(10))
        keyed_rdd = self.sc.parallelize((x % 2, x) for x in range(10))
        msg = "Caught StopIteration thrown from user's code; failing the task"

        self.assertRaisesRegexp(Py4JJavaError, msg, seq_rdd.map(stopit).collect)
        self.assertRaisesRegexp(Py4JJavaError, msg, seq_rdd.filter(stopit).collect)
        self.assertRaisesRegexp(Py4JJavaError, msg, seq_rdd.foreach, stopit)
        self.assertRaisesRegexp(Py4JJavaError, msg, seq_rdd.reduce, stopit)
        self.assertRaisesRegexp(Py4JJavaError, msg, seq_rdd.fold, 0, stopit)
        self.assertRaisesRegexp(Py4JJavaError, msg, seq_rdd.foreach, stopit)
        self.assertRaisesRegexp(Py4JJavaError, msg,
                                seq_rdd.cartesian(seq_rdd).flatMap(stopit).collect)

        # these methods call the user function both in the driver and in the executor
        # the exception raised is different according to where the StopIteration happens
        # RuntimeError is raised if in the driver
        # Py4JJavaError is raised if in the executor (wraps the RuntimeError raised in the worker)
        self.assertRaisesRegexp((Py4JJavaError, RuntimeError), msg,
                                keyed_rdd.reduceByKeyLocally, stopit)
        self.assertRaisesRegexp((Py4JJavaError, RuntimeError), msg,
                                seq_rdd.aggregate, 0, stopit, lambda *x: 1)
        self.assertRaisesRegexp((Py4JJavaError, RuntimeError), msg,
                                seq_rdd.aggregate, 0, lambda *x: 1, stopit) 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:30,代碼來源:tests.py

示例13: test_with_different_versions_of_python

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def test_with_different_versions_of_python(self):
        rdd = self.sc.parallelize(range(10))
        rdd.count()
        version = self.sc.pythonVer
        self.sc.pythonVer = "2.0"
        try:
            with QuietTest(self.sc):
                self.assertRaises(Py4JJavaError, lambda: rdd.count())
        finally:
            self.sc.pythonVer = version 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:12,代碼來源:tests.py

示例14: test_py4j_exception_message

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def test_py4j_exception_message(self):
        from pyspark.util import _exception_message

        with self.assertRaises(Py4JJavaError) as context:
            # This attempts java.lang.String(null) which throws an NPE.
            self.sc._jvm.java.lang.String(None)

        self.assertTrue('NullPointerException' in _exception_message(context.exception)) 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:10,代碼來源:tests.py

示例15: _java2py

# 需要導入模塊: from py4j import protocol [as 別名]
# 或者: from py4j.protocol import Py4JJavaError [as 別名]
def _java2py(sc, r, encoding="bytes"):
    if isinstance(r, JavaObject):
        clsName = r.getClass().getSimpleName()
        # convert RDD into JavaRDD
        if clsName != 'JavaRDD' and clsName.endswith("RDD"):
            r = r.toJavaRDD()
            clsName = 'JavaRDD'

        if clsName == 'JavaRDD':
            jrdd = sc._jvm.org.apache.spark.ml.python.MLSerDe.javaToPython(r)
            return RDD(jrdd, sc)

        if clsName == 'Dataset':
            return DataFrame(r, SQLContext.getOrCreate(sc))

        if clsName in _picklable_classes:
            r = sc._jvm.org.apache.spark.ml.python.MLSerDe.dumps(r)
        elif isinstance(r, (JavaArray, JavaList)):
            try:
                r = sc._jvm.org.apache.spark.ml.python.MLSerDe.dumps(r)
            except Py4JJavaError:
                pass  # not pickable

    if isinstance(r, (bytearray, bytes)):
        r = PickleSerializer().loads(bytes(r), encoding=encoding)
    return r 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:28,代碼來源:common.py


注:本文中的py4j.protocol.Py4JJavaError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。