在PySpark运行自定义的Java类

编程入门 行业动态 更新时间:2024-10-15 00:19:42
本文介绍了在PySpark运行自定义的Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图运行PySpark定制HDFS读取器类。这个类是用Java编写的,我需要从PySpark访问它,无论是从壳或火花提交。

I'm trying to run a custom HDFS reader class in PySpark. This class is written in Java and I need to access it from PySpark, either from the shell or with spark-submit.

在PySpark,我检索来自SparkContext的JavaGateway( sc._gateway )。

In PySpark, I retrieve the JavaGateway from the SparkContext (sc._gateway).

说我有一个类:

package org.foo.module public class Foo { public int fooMethod() { return 1; } }

我试过把它打包成一个罐子,用传递 - 罐子选项pyspark,然后运行:

I've tried to package it into a jar and pass it with the --jar option to pyspark and then running:

from py4j.java_gateway import java_import jvm = sc._gateway.jvm java_import(jvm, "org.foo.module.*") foo = jvm.foo.module.Foo()

但我得到的错误:

But I get the error:

Py4JError: Trying to call a package.

有人能帮助呢?谢谢你。

Can someone help with this? Thanks.

推荐答案

在PySpark尝试以下

In PySpark try the following

from py4j.java_gateway import java_import java_import(sc._gateway.jvm,"org.foo.module.Foo") func = sc._gateway.jvm.Foo() func.fooMethod()

请确保您已编译您的Java code到一个可运行的罐子,并提交火花的工作,像这样

Make sure that you have compiled your Java code into a runnable jar and submit the spark job like so

spark-submit --driver-class-path "name_of_your_jar_file.jar" --jars "name_of_your_jar_file.jar" name_of_your_python_file.py

更多推荐

在PySpark运行自定义的Java类

本文发布于:2023-11-21 23:45:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   PySpark   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!