package com.alibaba.otter.canal.utils; import com.alibaba.otter.canal.bo.FileUploadBO; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * @author kaikai_zheng * @version 1.0.0 * @className XmlContentUtil * @description //XML模板解析 * @data 2020-07-13 15:01 */ public class XmlContentUtil { /** * Log转化为ES标准数据 * * @param object 要转化的实体类FileUploadBO * @return */ public static XContentBuilder getXContentBuilder(Object object) { if (object.getClass().getName().contains("FileUploadBO")) { return getEsbMonitorXContentBuilder((FileUploadBO) object); } return null; } /** * FileUploadBO转化为ES标准数据 * * @param fileUploadBO * @return */ public static XContentBuilder getEsbMonitorXContentBuilder(FileUploadBO fileUploadBO) { XContentBuilder xContentBuilder = null; try { xContentBuilder = XContentFactory.jsonBuilder().startObject()// 标识开始设置值 .field("row_update_time", fileUploadBO.getRowUpdateTime()) .endObject(); } catch (Exception e) { e.printStackTrace(); } return xContentBuilder; } /** * 生成ES标准数据 * * @param clazz * @return * @throws IOException */ @SuppressWarnings("rawtypes") public static XContentBuilder getXContentBuilderMapping(Class clazz) throws IOException { if (clazz.getName().contains("FileUploadBO")) { return XmlContentUtil.getEsbMonitorXContentBuilderMapping(); } return null; } /** * 生成Monitor的ES标准数据模板 * * @return * @throws IOException */ public static XContentBuilder getEsbMonitorXContentBuilderMapping() throws IOException { Map<String, Object> keyword = new HashMap<String, Object>(); keyword.put("type", "keyword"); keyword.put("ignore_above", 256); XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject()//标识开始设置值 .startObject("row_update_time") .startObject("date").field("type", "date").startObject("fields").field("keyword", keyword).endObject().endObject() .endObject().endObject(); return mapping; } }