From cef254acc98d6299854c572c38bbde973170a09a Mon Sep 17 00:00:00 2001 From: Bobby Wang Date: Fri, 8 Apr 2022 11:51:09 +0800 Subject: [PATCH] [jvm-packages] remove the dep of com.fasterxml.jackson There is no need to add jackson for handling array inferface, since the json of array interface is really simple. --- jvm-packages/xgboost4j-gpu/pom.xml | 5 -- .../ml/dmlc/xgboost4j/gpu/java/CudfUtils.java | 72 +++++++++---------- 2 files changed, 35 insertions(+), 42 deletions(-) diff --git a/jvm-packages/xgboost4j-gpu/pom.xml b/jvm-packages/xgboost4j-gpu/pom.xml index 540038194593..fd53453aa44b 100644 --- a/jvm-packages/xgboost4j-gpu/pom.xml +++ b/jvm-packages/xgboost4j-gpu/pom.xml @@ -20,11 +20,6 @@ ${cudf.classifier} provided - - com.fasterxml.jackson.core - jackson-databind - 2.10.5.1 - org.apache.hadoop hadoop-hdfs diff --git a/jvm-packages/xgboost4j-gpu/src/main/java/ml/dmlc/xgboost4j/gpu/java/CudfUtils.java b/jvm-packages/xgboost4j-gpu/src/main/java/ml/dmlc/xgboost4j/gpu/java/CudfUtils.java index b63ef7f30538..f7071dcd5fb2 100644 --- a/jvm-packages/xgboost4j-gpu/src/main/java/ml/dmlc/xgboost4j/gpu/java/CudfUtils.java +++ b/jvm-packages/xgboost4j-gpu/src/main/java/ml/dmlc/xgboost4j/gpu/java/CudfUtils.java @@ -1,5 +1,5 @@ /* - Copyright (c) 2021 by Contributors + Copyright (c) 2021-2022 by Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,15 +16,7 @@ package ml.dmlc.xgboost4j.gpu.java; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.ObjectNode; +import java.util.ArrayList; /** * Cudf utilities to build cuda array interface against {@link CudfColumn} @@ -42,58 +34,64 @@ public static String buildArrayInterface(CudfColumn... cudfColumns) { // Helper class to build array interface string private static class Builder { - private JsonNodeFactory nodeFactory = new JsonNodeFactory(false); - private ArrayNode rootArrayNode = nodeFactory.arrayNode(); + private ArrayList colArrayInterfaces = new ArrayList(); private Builder add(CudfColumn... columns) { if (columns == null || columns.length <= 0) { throw new IllegalArgumentException("At least one ColumnData is required."); } for (CudfColumn cd : columns) { - rootArrayNode.add(buildColumnObject(cd)); + colArrayInterfaces.add(buildColumnObject(cd)); } return this; } private String build() { - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - JsonGenerator jsonGen = new JsonFactory().createGenerator(bos); - new ObjectMapper().writeTree(jsonGen, rootArrayNode); - return bos.toString(); - } catch (IOException ie) { - ie.printStackTrace(); - throw new RuntimeException("Failed to build array interface. Error: " + ie); + StringBuilder builder = new StringBuilder(); + builder.append("["); + for (int i = 0; i < colArrayInterfaces.size(); i++) { + builder.append(colArrayInterfaces.get(i)); + if (i != colArrayInterfaces.size() - 1) { + builder.append(","); + } } + builder.append("]"); + return builder.toString(); } - private ObjectNode buildColumnObject(CudfColumn column) { + /** build the whole column information including data and valid info */ + private String buildColumnObject(CudfColumn column) { if (column.getDataPtr() == 0) { throw new IllegalArgumentException("Empty column data is NOT accepted!"); } if (column.getTypeStr() == null || column.getTypeStr().isEmpty()) { throw new IllegalArgumentException("Empty type string is NOT accepted!"); } - ObjectNode colDataObj = buildMetaObject(column.getDataPtr(), column.getShape(), - column.getTypeStr()); + StringBuilder builder = new StringBuilder(); + String colData = buildMetaObject(column.getDataPtr(), column.getShape(), + column.getTypeStr()); + builder.append("{"); + builder.append(colData); if (column.getValidPtr() != 0 && column.getNullCount() != 0) { - ObjectNode validObj = buildMetaObject(column.getValidPtr(), column.getShape(), "