简体   繁体   English

如何从spring boot调用python程序?

[英]How to call a python program from spring boot?

I am using implicit library in python to implement a recommendation system.我在 python 中使用隐式库来实现推荐系统。 The model is ready, but the problem is my application is java based(spring boot).模型已准备就绪,但问题是我的应用程序是基于 Java 的(spring boot)。 Now I've the model, matrices and everything setup in python.现在我已经在 python 中设置了模型、矩阵和所有内容。 Basically the model has a offline component, where i perform the data feeding and matrix factorization stuff.基本上该模型有一个离线组件,我在其中执行数据馈送和矩阵分解的工作。 After that I've a python method which returns the recommendation for a user.之后,我有一个 python 方法,它为用户返回推荐。

The offline component will run once in a month.离线组件将在一个月内运行一次。 For the whole month I need to store the factorized matrix in some place and whenever required I need to call my python method from REST endpoint, which will utilize the stored matrix and return some results.在整个月中,我需要将分解矩阵存储在某个地方,并且在需要时我需要从 REST 端点调用我的 python 方法,该方法将利用存储的矩阵并返回一些结果。

So I've two questions.所以我有两个问题。

1. My factorized matrix is a numpy.sparse_matrix. 1. 我的分解矩阵是一个 numpy.sparse_matrix。 How can I store it, so that it will be reused again and again?我如何存储它,以便它可以一次又一次地重复使用?
2. How do i invoke a python method in java endpoint which will utilize the methods from implicit library then do the calculation based on the matrix and return the result? 2.我如何在java端点中调用python方法,该方法将利用隐式库中的方法然后基于矩阵进行计算并返回结果?

Any insight would be appreciated.任何见解将不胜感激。 Thanks in advance.提前致谢。

推荐的方法是在 REST 微服务中转换 Python 代码,然后在 Java 程序中使用它。

You can directly Runtime from java for the same.您可以直接从 java 运行时进行相同的操作。

String fetching = "python " + "c:\\<PythonFileName>.py \"" + songDetails + "\"";
String[] commandToExecute = new String[]{"cmd.exe", "/c", fetching};
Runtime.getRuntime().exec(commandToExecute);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM