marshalling不能与marklogic中的jaxbhandle一起工作

lndjwyie  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(471)

我想将以下对象存储到marklogic中。

@XmlRootElement(name = "trainModel")
@XmlAccessorType(XmlAccessType.FIELD)
public class TrainedModel implements Serializable {

    private static final long serialVersionUID = 1L;

    private String modelName;
    private CrossValidatorModel crossValidatorModel;

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public CrossValidatorModel getCrossValidatorModel() {
        return crossValidatorModel;
    }

    public void setCrossValidatorModel(CrossValidatorModel crossValidatorModel) {
        this.crossValidatorModel = crossValidatorModel;
    }

    @Override
    public String toString() {
        return "TrainedModel [modelName=" + modelName + ", crossValidatorModel=" + crossValidatorModel + "]";
    }
}

我正在使用jaxbhandle来完成我的需求,但是在将trainedmodel对象存储到marklogic时,crossvalidatormodel变空了。
crossvalidatormodel是由提供的外部对象 org.apache.spark.ml.tuning.CrossValidatorModel .
我试过下面的代码,

CrossValidatorModel crossValidatorModel = createDataFrame(null);
    TrainedModel trainedModel = new TrainedModel();
    trainedModel.setModelName("sample");
    trainedModel.setCrossValidatorModel(crossValidatorModel);

    JAXBContext context = JAXBContext.newInstance(TrainedModel.class);  
    JAXBHandle<TrainedModel> jaxbHandle = new JAXBHandle<TrainedModel>(context);
    jaxbHandle.set(trainedModel);

    GenericDocumentManager docMgr = client.newDocumentManager();
    docMgr.writeAs("/shivling.xml", null, jaxbHandle);

这是我得到的xml结果,

<?xml  version="1.0" encoding="UTF-8"?>
<trainModel>
    <modelName>sample</modelName>
    <crossValidatorModel>
    </crossValidatorModel>
</trainModel>

crossvalidatormodel在此为空,请帮助我解决此问题。

btxsgosb

btxsgosb1#

null(在代码中)在xml中产生影响:结果表明代码中存在语义问题。
jaxb框架可以通过适当的marklogic文档管理器或旧的pojo方式交付:
一个简单的marklogic jaxb注解(在批量读写事务中使用dmsdk):
代码段:

XMLDocumentManager docMgr = marklogic.newXMLDocumentManager();

        TrainedModel trainedModel = new TrainedModel();

        trainedModel.setModelName("History and Psychology");
        trainedModel.setCrossValidatorModel("Biology");

        String docId = "/train-model.xml";

        docMgr.writeAs(docId, trainedModel);

结果:

subpar io方法:

TrainedModel trainedModel = new TrainedModel();

        trainedModel.setModelName("History and Current Affairs");
        trainedModel.setCrossValidatorModel("Psychology");

        // initialize JAXB 
        JAXBContext context = JAXBContext.newInstance(TrainedModel.class);

        // POJO Handle
        JAXBHandle<TrainedModel> writeHandle = new JAXBHandle<TrainedModel>(context);

        // Marshalling
        Marshaller jaxMar = context.createMarshaller();

        String docId = "/train-model.xml";

        // IsMarshalled
        jaxMar.marshal(trainedModel, System.out);

        // set Handle
        writeHandle.set(trainedModel);

       // write POJO to MarkLogic
        docMgr.write(docId, writeHandle);

相关问题