java调用1.5.1版本PMML

PMML版本1.5和1.4的API发生了变化。网上搜到的都是1.4版本的例子。所以这里提供一个1.5版本的例子。

public class PredictTest {
  public static void main(String[] args) throws Exception {
    String pathxml = "xgb_ip.pmml";
    Map<String, Double> map = new HashMap<String, Double>();
    map.put(“ip_count”, 10);
    map.put(“finger_count”, 2);
    ...
    predict(map, pathxml);
  }

  public static void predict(Map<String, Double> kxmap, String pathxml) throws Exception {
    File file = new File(pathxml);
    InputStream inputStream = new FileInputStream(file);
    PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
    inputStream.close();

    ModelEvaluatorBuilder evaluatorBuilder = new ModelEvaluatorBuilder(pmml, (String) null)
			.setModelEvaluatorFactory(ModelEvaluatorFactory.newInstance())
			.setValueFactoryFactory(ValueFactoryFactory.newInstance());
//			.setOutputFilter(this.filterOutput ? OutputFilters.KEEP_FINAL_RESULTS : OutputFilters.KEEP_ALL);

		Evaluator evaluator = evaluatorBuilder.build();

      List<InputField> inputFields = evaluator.getInputFields();

      Map<FieldName, FieldValue> arguments = new LinkedHashMap<FieldName, FieldValue>();
      for (InputField inputField : inputFields) {
        FieldName inputFieldName = inputField.getName();
        Object rawValue = kxmap.get(inputFieldName.getValue());
        FieldValue inputFieldValue = inputField.prepare(rawValue);
        arguments.put(inputFieldName, inputFieldValue);
      }

      Map<FieldName, ?> results = evaluator.evaluate(arguments);
      List<TargetField> targetFields = evaluator.getTargetFields();

      for (TargetField targetField : targetFields) {
        FieldName targetFieldName = targetField.getName();
        ProbabilityDistribution targetFieldValue = (ProbabilityDistribution)results.get(targetFieldName);
        System.out.println(" value: " + targetFieldValue.getValue("1"));
      }
  }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一些参考。 首先,您需要使用Java中的WebSocket客户端库来连接第三方WebSocket服务器。最常用的Java WebSocket客户端库之一是Java-WebSocket。您可以在Maven中添加以下依赖项来使用它: ``` <dependency> <groupId>org.java-websocket</groupId> <artifactId>Java-WebSocket</artifactId> <version>1.5.1</version> </dependency> ``` 接下来,您需要编写代码来连接WebSocket服务器并向其发送消息。以下是一个示例代码片段: ``` import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import java.net.URI; import java.net.URISyntaxException; public class MyWebSocketClient extends WebSocketClient { public MyWebSocketClient(URI serverUri) { super(serverUri); } @Override public void onOpen(ServerHandshake handshakedata) { System.out.println("Connected to server: " + getURI()); } @Override public void onMessage(String message) { System.out.println("Received message: " + message); // 在这里添加代码将接收到的消息存储到数据库中 } @Override public void onClose(int code, String reason, boolean remote) { System.out.println("Connection closed: " + reason); } @Override public void onError(Exception ex) { ex.printStackTrace(); } public static void main(String[] args) throws URISyntaxException { String serverUrl = "ws://example.com:8000/websocket"; MyWebSocketClient client = new MyWebSocketClient(new URI(serverUrl)); client.connect(); } } ``` 在`onMessage`方法中,您可以将接收到的消息存储到数据库中。您可以使用Java中的JDBC API来连接到数据库并执行SQL查询。以下是一个示例代码片段: ``` import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class MyDatabaseClient { private Connection connection; public MyDatabaseClient(String url, String username, String password) throws SQLException { connection = DriverManager.getConnection(url, username, password); } public void addMessage(String message) throws SQLException { String sql = "INSERT INTO messages (content) VALUES (?)"; try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setString(1, message); statement.executeUpdate(); } } public static void main(String[] args) throws SQLException { String dbUrl = "jdbc:mysql://localhost:3306/mydatabase"; String dbUsername = "myuser"; String dbPassword = "mypassword"; MyDatabaseClient client = new MyDatabaseClient(dbUrl, dbUsername, dbPassword); client.addMessage("Hello, world!"); } } ``` 您可以在`onMessage`方法中创建`MyDatabaseClient`实例并调用`addMessage`方法将接收到的消息存储到数据库中。 希望这些代码片段可以为您提供一些帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值