用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操作了。

ES数据-MySql处理Date类型的数据导入处理的更多相关文章

  1. 向mysql中插入Date类型的数据

    先看数据库表的定义 date字段为sql.date类型.我要向其中插入指定的日期和当前日期. 一.插入当前日期 思路:先获取当前系统,在将当前系统时间转换成sql类型的时间,然后插入数据库.代码如下 ...

  2. oracle中时间戳转为Date类型的数据

    问题描述: 一个表中原本应该存放date类型的数据,但是不知道之前哪位大仙把两个字段的类型建成了NUMBER类型的了,这样在后台看时间肯定不方便.现在需要改成date类型,但是现在库中是有数据的,不能 ...

  3. 使用js处理后台返回的Date类型的数据

    从后台返回的日期类型的数据,如果直接在前端进行显示的话,显示的就是一个从 1970-01-01 00:00:00到现在所经过的毫秒数,而在大多数业务中都不可能显示这个毫秒数,大多数都是显示一个正常的日 ...

  4. 向数据库中插入一个DateTime类型的数据到一个Date类型的字段中,需要转换类型。TO_DATE('{0}','YYYY-MM-DD'))

    需要指出的是,C#中有datetime类型,但是这个类型是包括小时,分钟,秒的.这个格式与数据库中的Date类型不符,如果将now设为datetime类型插入数据会失败. 需要通过TO_DATE('字 ...

  5. Android向Rest服务Post数据遇到的Date类型数据问题

    今天在Android端向Rest服务Post数据时,总是不成功,查了很多资料,才知道Rest端将json串反序列化时,需要的时间格式必须是UTC类型,及Date(12345678+0800)格式. A ...

  6. 使用springmvc从页面中获取数据,然后根据获得的参数信息进行修改,如果修改的数据中含有不是基本数据类型的参数。比如传的参数中有Date类型的数据时,需要我们进行参数类型转换。

    1.1 需求 在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式. 1.2 需求分析 由于日期数据有很多格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑 ...

  7. 关于Java读取mysql中date类型字段默认值'0000-00-00'的问题

    今天在做项目过程中,查询一个表中数据时总碰到这个问题:      java.sql.SQLException:Value '0000-00-00' can not be represented as ...

  8. MySQL中date类型的空值0000-00-00和00:00:00

    1.如果mysql中使用了date类型,并且默认值为'0000-00-00', 那么数据库中的'0000-00-00 00:00:00', '0000-00-00', '00:00:00'这三个值是相 ...

  9. 如何查询mysql中date类型的时间范围记录?

    java date类型 会不会自动转换 mysql date类型? 抹除掉后面 时间 ? 时间不是查询条件?

随机推荐

  1. Javascript实现页面跳转的几种方式

    概述 相信很多Web开发者都知道,在开发Web程序的时候,对于页面之间的跳转,有很多种,但是有效的跳转则事半功倍,下面就是我在平时的开发过程中所用到的一些JavaScript跳转方式,拿出和大家共享一 ...

  2. Apache服务器性能监控

    Apache服务器性能监控 1.使用自带mod_status模块监控 1)加载mod_status.so 模块 在httpd.conf中打开LoadModule status_module modul ...

  3. 一起来做chrome扩展《AJAX请求》

    chrome在一次更新之后,出于安全考虑,完全的禁止了content_script从https向http发起ajax请求,即使正常情况下也会在console里给出提示.这对于WEB来讲是好事,但对于扩 ...

  4. 关于mouse_event和sendinput无效的原因

    关于mouse_event和sendinput无效的原因 SetCursorPos 有用,   于mouse_event和sendinput 无用, 导致问题不清晰,  原来是我换了杀毒软件, 360 ...

  5. 关于 windows 下 node_modules\node-sass\vendor 的报错解决方法

    项目git clone下来之后,运行npminstall, npm start报错代码如下: ERROR in ENOENT: no such file or directory, scandir ' ...

  6. 浅析“依赖注入(DI)/控制反转(IOC)”的实现思路

    开始学习Spring的时候,对依赖注入(DI)——也叫控制反转(IOC)—— 的理解不是很深刻.随着学习的深入,也逐渐有了自己的认识,在此记录,也希望能帮助其他入门同学更深入地理解Spring.本文不 ...

  7. 《Linux内核设计与实现》CHAPTER17阅读梳理

    <Linux内核设计与实现>CHAPTER17阅读梳理 [学习时间:3.5hours] [学习内容:设备类型,模块,内核对象,sysfs] 个人思考部分见[]标出的部分 一.课堂讲解整理& ...

  8. 可爱的Python_课后习题_CDay−5 Python 初体验和原始需求

    计算今年是否是闰年.判断闰年条件,满足年份模400 为0,或者模4 为0 但模100不为0. def is_learp_year(year): """判断年份是否为润年& ...

  9. magento目录结构说明,Magento文件夹结构说明,Magento folder structure

    /app – 程序根目录     /app/etc – 全局配置文件目录     /app/code – 所有模块安装其模型和控制器的目录     /app/code/core – 核心代码或经过认证 ...

  10. delphi之事件

    delphi的事件如上图所示: 图中oncloseup代表的是日期选择下拉框关闭时触发的事件. //事件定义 procedure Ondatechange(Sender: TObject); //事件 ...