Py4J:使用 Python 启动 Spring 获得自动装配并调用 Java 代码

本文介绍了如何在Apache Zeppelin环境中,通过Py4J让Python调用Java代码,特别是在没有启动Spring的情况下,无法获得自动装配的功能。文章详细讲解了如何在Java中创建启动Spring的类,并利用双重检查锁避免重复启动。同时,展示了在Python中如何使用SparkContext的属性来调用这些Java方法,启动Spring并获取装配后的Java Bean,从而避免在Python中维护额外的配置文件。

公司交互式数据分析平台选择的是 Apache Zeppelin,可以配置并选择 Spark Interpreter 从而借助 Spark 来进行数据分析。在 Zeppelin 的一个 note 中,可以写 SQL 脚本(段落中使用 %sql 声明使用 SQL Interpreter)、Python 脚本(段落中使用 %pyspark 声明使用 Spark Interpreter),一个 note 中的多个 paragraph 可以使用不同的 interpreter,对于一些复杂的逻辑经常会使用 %pyspark 来借助 PySpark 运行 Python 脚本。

因为公司大数据项目主要是由 Java 代码写的,所以很多逻辑以及配置文件相关都是在 Java 代码中的。为了不在 Python 中重复写相同的逻辑,就需要使用 Python 来调用 Java 代码从而完成相应的逻辑。

Java 代码最终是打成 Jar 包配置在 Spark 中的,有一个比较烦人的地方是,通过 Py4J 虽然可以调用 Java 代码,但是由于没有启动 Spring 不能得到自动装配的能力,需要在 Python 代码中额外维护一份配置文件才能读取一些属性。在多个地方维护相同的文件,终究还是不方便。需要找到一种使用 Python 通过 Py4J 启动 Spring 的方法,这样获取到的 Java Bean 中就可以装配到配置属性了。

要想达到上述目的,需要 Java 代码和 Python 代码的配合。

  • 在 Java 代码中,可以新增一个 SpringUtils 的类,在其中启动 Main Class,需要注意的一点是,我们不需要启动 Tomcat(启动的话,需要占用 8080 端口,如果有其他应用占用,会有冲突),可以设置以非 Web 应用的方式启动 new SpringApplicationBuilder(Main.class).web(WebApplicationType.NONE)
# Author: Greg Lee Turnquist # Paperback: 264 pages # Publisher: Packt Publishing; 1st New edition edition (May 21, 2010) # Language: English # ISBN-10: 1849510660 # ISBN-13: 978-1849510660 Amazon 的介紹: http://www.amazon.com/Spring-Python-1-1-Greg-Turnquist/dp/1849510660 Spring Python captures the concepts of the Spring Framework and Spring Security and brings them to the world of Python and provides many functional parts to assemble applications. Spring Python is all about using the many useful features of Spring to the fullest and making these features available when working with Python. Get to grips with all of the concepts of Spring and apply these to the language and environment of Python to develop powerful applications for your own personal requirements. The book provides an introduction to Spring Python and steadily takes you towards the advanced features that this integration has to offer. Spring uses the Java programming language. Spring Python, the first Spring extension to go live, allows developers to make maximum use of Spring features in Python. This book starts off by introducing each of the core building blocks of Spring Python using real code examples and high-level diagrams. It explores the various concepts of Spring Python with the help of examples and case studies and focuses on vital Spring Python features to make the lives of Python and Java developers simple. The early chapters cover simple applications with simple operations including data access, and then subsequent chapters scale up to multi-node, secured, transactional applications stopping short of very advanced level complexity. This book will help you to scale your applications without having to add unnecessary complexity
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值