ES数据-MySql处理Date类型的数据导入处理
用ES的小伙伴们,相信大家都遇到过Mapping处理Date类型的数据头疼问题吧。
不用头疼了,我来给你提供一种解决方案:
1、Maping定义为:
{
"mappings": {
"carecustomerlog_type_all": {
"properties": {
"ID": {
"type": "long"
},
"APPLYRATE": {
"type": "double"
},
"PREAPPLYRATE": {
"type": "double"
},
"TYPE": {
"type": "long"
},
"CDATE": {
"type": "long"
},
"CARECUSTOMID": {
"type": "long"
},
"CAREACCOUNTID": {
"type": "long"
},
"watenum": {
"type": "string",
"index": "not_analyzed"
},
"customerid": {
"type": "string",
"index": "not_analyzed"
},
"orderid": {
"type": "string",
"index": "not_analyzed"
},
"customername": {
"type": "string",
"index": "not_analyzed"
},
"content": {
"type": "string",
"index": "not_analyzed"
}
}
},
"careaccountin_type_all": {
"properties": {
"id": {
"type": "long"
},
"customerid": {
"type": "string",
"index": "not_analyzed"
},
"groupid": {
"type": "string",
"index": "not_analyzed"
},
"accountType": {
"type": "string",
"index": "not_analyzed"
},
"rate": {
"type": "double"
},
"amount": {
"type": "double"
},
"fee": {
"type": "double"
},
"sellerid": {
"type": "string",
"index": "not_analyzed"
},
"sellername": {
"type": "string",
"index": "not_analyzed"
},
"state": {
"type": "string",
"index": "not_analyzed"
},
"customername": {
"type": "string",
"index": "not_analyzed"
},
"createdate": {
"type": "string",
"index": "not_analyzed"
},
"groupname": {
"type": "string",
"index": "not_analyzed"
},
"adviserid": {
"type": "string",
"index": "not_analyzed"
},
"advisername": {
"type": "string",
"index": "not_analyzed"
},
"ordergroupid": {
"type": "string",
"index": "not_analyzed"
},
"ordergroupname": {
"type": "string",
"index": "not_analyzed"
},
"comm": {
"type": "string",
"index": "not_analyzed"
},
"watenum": {
"type": "string",
"index": "not_analyzed"
},
"appkey": {
"type": "string",
"index": "not_analyzed"
},
"paytime": {
"type": "long"
}
}
},
"carecustomerlog_type_funddetails": {
"properties": {
"ID": {
"type": "long"
},
"CDATE": {
"type": "long"
},
"orderid": {
"type": "string",
"index": "not_analyzed"
},
"PREAPPLYRATE": {
"type": "double"
},
"APPLYRATE": {
"type": "double"
},
"content": {
"type": "string",
"index": "not_analyzed"
},
"TYPE": {
"type": "long"
},
"CAREACCOUNTID": {
"type": "long"
},
"watenum": {
"type": "string",
"index": "not_analyzed"
},
"customerid": {
"type": "string",
"index": "not_analyzed"
},
"groupid": {
"type": "string",
"index": "not_analyzed"
},
"accountType": {
"type": "string",
"index": "not_analyzed"
},
"rate": {
"type": "double"
},
"amount": {
"type": "double"
},
"fee": {
"type": "double"
},
"sellerid": {
"type": "string",
"index": "not_analyzed"
},
"sellername": {
"type": "string",
"index": "not_analyzed"
},
"state": {
"type": "string",
"index": "not_analyzed"
},
"customername": {
"type": "string",
"index": "not_analyzed"
},
"createdate": {
"type": "string",
"index": "not_analyzed"
},
"groupname": {
"type": "string",
"index": "not_analyzed"
},
"adviserid": {
"type": "string",
"index": "not_analyzed"
},
"advisername": {
"type": "string",
"index": "not_analyzed"
},
"ordergroupid": {
"type": "string",
"index": "not_analyzed"
},
"ordergroupname": {
"type": "string",
"index": "not_analyzed"
},
"paytime": {
"type": "long"
}
}
}
}
}
在Mapping中把Date类型数据在es中定义成long类型。
在code中执行代码时,用MySql函数UNIX_TIMESTAMP(cai.paytime)获取日期的秒数据,插入到ES中
public static APIResult<String> save(String index, String type, String idName, JSONArray jsonArray)
{
BulkRequestBuilder bulkRequest = client.prepareBulk().setRefresh(true);
for (Iterator localIterator = jsonArray.iterator(); localIterator.hasNext(); ) { Object object = localIterator.next();
JSONObject json = StringUtils.isJSONObject(object);
String idValue = json.optString(idName);
if (StringUtils.isBlank(idValue)) {
idValue = idName;
}
if (StringUtils.isBlank(idName)) {
IndexRequestBuilder lrb = client.prepareIndex(index, type).setSource(json.toString());
bulkRequest.add(lrb);
}
else
{
IndexRequestBuilder lrb = client.prepareIndex(index, type, idValue).setSource(json.toString());
bulkRequest.add(lrb);
}
}
BulkResponse bulkResponse = null;
try {
bulkResponse = (BulkResponse) bulkRequest.execute().actionGet();
}
catch (Exception e) {
e.printStackTrace();
}
if (bulkResponse.hasFailures())
{
System.out.println(bulkResponse.getItems().toString());
return new APIResult(500, "保存ES失败!");
}
bulkRequest = client.prepareBulk();
return new APIResult(200, "保存ES成功!");
}
,执行添加,提醒一下ES默认会设置分词,在添加之前,应该首先定义Mapping,在执行添加。
然后就可以执行select、update、delete操作了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」