Java中調(diào)用Python的實現(xiàn)示例
Python語言有豐富的系統(tǒng)管理、數(shù)據(jù)處理、統(tǒng)計類軟件包,因此從java應用中調(diào)用Python代碼的需求很常見、實用。DataX 是阿里開源的一個異構(gòu)數(shù)據(jù)源離線同步工具,致力于實現(xiàn)包括關(guān)系型數(shù)據(jù)庫(MySQL、Oracle等)、HDFS、Hive、ODPS、HBase、FTP等各種異構(gòu)數(shù)據(jù)源之間穩(wěn)定高效的數(shù)據(jù)同步功能。Datax也是通過Java調(diào)用Python腳本。本文介紹幾種方法從java調(diào)用Python代碼,從而最大化利用兩個語言的優(yōu)勢。
Java core
Java提供了有兩種方法,分別為ProcessBuilder API和 JSR-223 Scripting Engine。
使用ProcessBuilder
通過ProcessBuilder創(chuàng)建本地操作系統(tǒng)進程啟動python并執(zhí)行Python腳本, hello.py腳本簡單輸出“Hello Python!”。需要開發(fā)環(huán)境已經(jīng)安裝了python,并設(shè)置了環(huán)境變量。
@Test public void givenPythonScript_whenPythonProcessInvoked_thenSuccess() throws Exception { ? ? ProcessBuilder processBuilder = new ProcessBuilder("python", resolvePythonScriptPath("hello.py")); ? ? processBuilder.redirectErrorStream(true); ? ? Process process = processBuilder.start(); ? ? List<String> results = readProcessOutput(process.getInputStream()); ? ? assertThat("Results should not be empty", results, is(not(empty()))); ? ? assertThat("Results should contain output of script: ", results, hasItem(containsString("Hello Python!"))); ? ? int exitCode = process.waitFor(); ? ? assertEquals("No errors should be detected", 0, exitCode); } private List<String> readProcessOutput(InputStream inputStream) throws IOException { ? ? try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) { ? ? ? ? return output.lines() ? ? ? ? ? ? .collect(Collectors.toList()); ? ? } } private String resolvePythonScriptPath(String filename) { ? ? File file = new File("src/test/resources/" + filename); ? ? return file.getAbsolutePath(); }
首先啟動帶一個參數(shù)的python命令,參數(shù)為python腳本的絕對路徑。可以放在java工程的resources目錄下。需要注意的是:redirectErrorStream(true),為了使得當執(zhí)行腳本出現(xiàn)錯誤時,錯誤輸出流被合并至標準輸出流。這樣設(shè)置可以從Process對象的getInputStream()方法中讀取錯誤信息。如果沒有該設(shè)置,則需要分別用兩個方法獲取流:getInputStream() 和 getErrorStream() 。processBuilder.start()獲取Process對象,然后讀取輸出流并驗證結(jié)果。
使用Java腳本引擎
java6首次引入JSR-223規(guī)范,定義一組提供基本腳本功能的腳本API。這些API提供了執(zhí)行腳本和在Java和腳本語言之間共享值的機制。該規(guī)范主要目的是為了統(tǒng)一Java與不同實現(xiàn)JVM的動態(tài)腳本語言的交互,Jython是在jvm上運行python的java實現(xiàn)。假設(shè)我們在CLASSPATH上有Jython,框架自動發(fā)現(xiàn)我們有可能使用該腳本引擎,并允許我們直接請求Python腳本引擎。因為Maven有Jython,我們可以在maven中引用,當然也下載直接安裝:
<dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.2</version> </dependency>
可以通過下面代碼列出所有支持的腳本引擎:
public static void listEngines() { ? ? ScriptEngineManager manager = new ScriptEngineManager(); ? ? List<ScriptEngineFactory> engines = manager.getEngineFactories(); ? ? for (ScriptEngineFactory engine : engines) { ? ? ? ? LOGGER.info("Engine name: {}", engine.getEngineName()); ? ? ? ? LOGGER.info("Version: {}", engine.getEngineVersion()); ? ? ? ? LOGGER.info("Language: {}", engine.getLanguageName()); ? ? ? ? LOGGER.info("Short Names:"); ? ? ? ? for (String names : engine.getNames()) { ? ? ? ? ? ? LOGGER.info(names); ? ? ? ? } ? ? } }
如果Jython在環(huán)境中可用,應該看到相應的輸出:
...
Engine name: jython
Version: 2.7.2
Language: python
Short Names:
python
jython
現(xiàn)在使用Jython調(diào)用hello.py腳本:
@Test public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception { ? ? StringWriter writer = new StringWriter(); ? ? ScriptContext context = new SimpleScriptContext(); ? ? context.setWriter(writer); ? ? ScriptEngineManager manager = new ScriptEngineManager(); ? ? ScriptEngine engine = manager.getEngineByName("python"); ? ? engine.eval(new FileReader(resolvePythonScriptPath("hello.py")), context); ? ? assertEquals("Should contain script output: ", "Hello Python!", writer.toString().trim()); }
使用該API比上面的示例更簡潔。首先設(shè)置ScriptContext包含StringWriter,用于保存執(zhí)行腳本的輸出。然后提供簡稱讓ScriptEngineManager 查找腳本引擎,可以使用python或jython。最后驗證輸出是否與期望一致。
其實也可以使用PythonInterpretor 類直接調(diào)用嵌入的python代碼:
@Test public void givenPythonInterpreter_whenPrintExecuted_thenOutputDisplayed() { ? ? try (PythonInterpreter pyInterp = new PythonInterpreter()) { ? ? ? ? StringWriter output = new StringWriter(); ? ? ? ? pyInterp.setOut(output); ? ? ? ? pyInterp.exec("print('Hello Python!')"); ? ? ? ? assertEquals("Should contain script output: ", "Hello Python!", output.toString().trim()); ? ? } }
使用PythonInterpreter類,可以通過exec方法直接執(zhí)行python代碼。和前面示例一樣通過StringWriter 捕獲執(zhí)行輸出。下面再看一個示例:
@Test public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() { try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("x = 10+10"); PyObject x = pyInterp.get("x"); assertEquals("x: ", 20, x.asInt()); } }
上面示例可以使用get方法訪問變量值。下面示例看如何捕獲錯誤:
try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("import syds"); }
運行上面代碼會拋出PyException 異常,與在本地執(zhí)行Python腳本輸出錯誤一樣。
下面有幾點注意事項:
- PythonIntepreter 實現(xiàn)了AutoCloseable,最好與 try-with-resources 一起使用。
- PythonIntepreter類名不是表示Python代碼的解析器,Python程序在Jython是運行在jvm中,執(zhí)行前需要編譯為java字節(jié)碼。
- 盡管Jython是Java的Python實現(xiàn),但它可能不包含與本機Python相同的所有子包。
下面示例展示如何把java變量賦給Python變量:
import org.python.util.PythonInterpreter;? import org.python.core.*;? class test3{ ? ? public static void main(String a[]){ ? ? ? ? int number1 = 10; ? ? ? ? int number2 = 32; ? ? ? ? try (PythonInterpreter pyInterp = new PythonInterpreter()) { ? ? ? ? ? ? python.set("number1", new PyInteger(number1)); ? ? ? ? ? ? python.set("number2", new PyInteger(number2)); ? ? ? ? ? ? python.exec("number3 = number1+number2"); ? ? ? ? ? ? PyObject number3 = python.get("number3"); ? ? ? ? ? ? System.out.println("val : "+number3.toString()); ? ? ? ? } ? ? } }
總結(jié)
本文介紹了如何從Java調(diào)用Python腳本,使用jython腳本引擎比ProcessBuilder類更簡單。另外Python可以便捷搭建http應用,Java也可以通過HTTP協(xié)議直接調(diào)用HTTP服務(wù)實現(xiàn)交互。
參考內(nèi)容:https://www.baeldung.com/java-working-with-python
到此這篇關(guān)于Java中調(diào)用Python的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java調(diào)用Python內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決java調(diào)用python代碼返回值中文亂碼問題
這篇文章主要介紹了解決java調(diào)用python代碼返回值中文亂碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Java線上問題排查神器Arthas實戰(zhàn)原理解析
原先我們Java中我們常用分析問題一般是使用JDK自帶或第三方的分析工具如jstat、jmap、jstack、?jconsole、visualvm、Java?Mission?Control、MAT等,還有一款神器Arthas工具,可幫助程序員解決很多繁瑣的問題,感興趣的朋友一起看看吧2022-01-01Java 并發(fā)編程學習筆記之Synchronized簡介
雖然多線程編程極大地提高了效率,但是也會帶來一定的隱患。比如說兩個線程同時往一個數(shù)據(jù)庫表中插入不重復的數(shù)據(jù),就可能會導致數(shù)據(jù)庫中插入了相同的數(shù)據(jù)。今天我們就來一起討論下線程安全問題,以及Java中提供了什么機制來解決線程安全問題。2016-05-05Spring的兩種事務(wù)管理機制的基本概念和demo示例
Spring事務(wù)包括聲明式事務(wù)管理和注解式事務(wù)管理,我們通過概念和小demo的形式一步一步地來一起學習這個知識點,需要的朋友可以參考下2023-07-07java定位死鎖的三種方法(jstack、Arthas和Jvisualvm)
這篇文章主要給大家介紹了關(guān)于java定位死鎖的三種方法,分別是通過jstack定位死鎖信息、通過Arthas工具定位死鎖以及通過 Jvisualvm 定位死鎖,文中還介紹了死鎖的預防方法,需要的朋友可以參考下2021-09-09