将python算法转为scala_PyJava: PyJava 是一个用于在 Java/Scala 和 Python 之间转换数据的库...

PyJava

This library is an ongoing effort towards bringing the data exchanging ability

between Java/Scala and Python. PyJava introduces Apache Arrow as the exchanging data format,

this means we can avoid ser/der between Java/Scala and Python which can really speed up the

communication efficiency than traditional way.

When you invoke python code in Java/Scala side, PyJava will start some python workers automatically

and send the data to python worker, and once they are processed, send them back. The python workers are reused

by default.

The initial code in this lib is from Apache Spark.

Install

Setup python(>= 3.6) Env(Conda is recommended):

pip uninstall pyjava && pip installpyjava

Setup Java env(Maven is recommended):

tech.mlsql

pyjava-2.4_2.12

0.2.8.0

Using python code snippet to process data in Java/Scala

With pyjava, you can run any python code in your Java/Scala application.

val envs = new util.HashMap[String, String]()

// prepare python environment

envs.put(str(PythonConf.PYTHON_ENV), "source activate dev && export ARROW_PRE_0_15_IPC_FORMAT=1 ")

// describe the data which will be transfered to python

val sourceSchema = StructType(Seq(StructField("value", StringType)))

val batch = new ArrowPythonRunner(

Seq(ChainedPythonFunctions(Seq(PythonFunction(

"""

|import pandas as pd

|import numpy as np

|

|def process():

| for item in context.fetch_once_as_rows():

| item["value1"] = item["value"] + "_suffix"

| yield item

|

|context.build_result(process())

""".stripMargin, envs, "python", "3.6")))), sourceSc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spark jar 任务通常是使用 ScalaJava 编写的,因为 Spark 的核心是用 Scala 编写的。虽然 PySpark 提供了使用 Python 进行 Spark 编程的支持,但是 PySpark 通常被视为 Spark 的一个辅助工具,而不是 Spark 的主要编程语言。因此,Spark jar 任务一般是使用 ScalaJava 编写的。 虽然在 Spark jar 任务中不能直接编写 Python 代码,但是可以通过使用 Py4J 框架在 Java/Scala 代码中调用 Python 代码。Py4J 是一个用于PythonJava 之间进行交互的桥接器,它允许 Java/Scala 代码调用 Python 代码,并返回 Python 代码执行的结果。 可以使用 Py4J 在 Spark jar 任务中调用 Python 代码的场景主要是需要使用 Python 中的一些第三方算法,而这些算法Java/Scala 中没有相应的实现。例如,如果您需要使用 Python 中的自然语言处理 NLTK 来处理文本数据,则可以使用 Py4J 在 Spark jar 任务中调用 NLTK,并使用它来处理文本数据。 以下是一个使用 Py4J 在 Spark jar 任务中调用 Python 代码的例子: ```scala import py4j.GatewayServer object Py4JExample { def main(args: Array[String]): Unit = { // Start the Py4J gateway server val gatewayServer = new GatewayServer(new Py4JExample) gatewayServer.start() // Call the Python function val pythonResult = gatewayServer.getPythonServerEntryPoint(Array.empty[String]) .callPythonFunction("my_python_function", "hello") // Print the result println(pythonResult) // Stop the Py4J gateway server gatewayServer.shutdown() } } class Py4JExample { def my_python_function(str: String): String = { // Call the NLTK library to process the input string ... // Return the processed string return processedString } } ``` 这个例子中,我们首先启动了 Py4J gateway server,并使用 Py4J 框架在 Scala 代码中调用了 Python 中的 my_python_function 函数,该函数接受一个字符串作为参数,并返回一个字符串。在 my_python_function 函数中,我们可以使用 Py4J 调用 Python 中的 NLTK 来处理输入的字符串。最后,我们将处理后的字符串返回给 Scala 代码,并将其打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值