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

时间: 2024-12-17 18:52:11

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

向mysql中插入Date类型的数据

先看数据库表的定义 date字段为sql.date类型.我要向其中插入指定的日期和当前日期. 一.插入当前日期 思路:先获取当前系统,在将当前系统时间转换成sql类型的时间,然后插入数据库.代码如下 public static void insert_now() throws ClassNotFoundException, SQLException{ java.util.Date utilDate = new Date(); //获取java.util.Date对象---也即当前时间 java.

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

需要指出的是,C#中有datetime类型,但是这个类型是包括小时,分钟,秒的.这个格式与数据库中的Date类型不符,如果将now设为datetime类型插入数据会失败. 需要通过TO_DATE('字段','YYYY-MM-DD'))转换.如下: string.Format("insert into tablename (TIME) values(TO_DATE('{0}','YYYY-MM-DD'))",now) 错误写法: string.Format("insert in

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

问题描述: 一个表中原本应该存放date类型的数据,但是不知道之前哪位大仙把两个字段的类型建成了NUMBER类型的了,这样在后台看时间肯定不方便.现在需要改成date类型,但是现在库中是有数据的,不能直接从NUMBER改为DATE.所以需要建立先创建两个DATE类型的临时字段,然后把对应字段的数据转换为Date类型的数据之后存到新字段上面,最后删除老字段,将新字段改名为老字段. 一.新建两个临时字段 ALTER TABLE CS_USER ADD (CREATEDATE1 DATE,OPERAT

SQL语句oracle中如何插入Date类型的数据

转:http://chwshuang.iteye.com/blog/933926 在开发的时候,经常要写条SQL语句将信息插入表中,插入的数据如果字段是date类型,就必须将date类型转换成字符串String类型在通过sql语句插入数据库.这是我字段唯一的方法,如果有高人请另赐教! 我的解决方法是用oracle中的to_date()方法,具体看下面的例子 Oracle中插入date数据代码 insert into news(ID,MSG,SEND_TIME,TIMER) Oracle中插入da

毕设的学习(16)Java存取mysql的Date类型数据

https://www.cnblogs.com/Latiny/p/8613352.html 这是用jdbc的,所以有个rs 即ResultSet //设置日期格式SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // new Date()为获取当前系统时间 String nowTime = df.format(new Date()); 直接使用nowTime可以插入. 原文地址:https://www.

在java中将日期(Date类型的数据)增加或减少一定时间的方法

实现的功能:数据来源一般都是从数据库获取的日期类型数据,本例只介绍获取当前时间后,在当前时间基础上增加15分钟. Date date = new Date();date.setTime(date.getTime() + 15*60*1000);System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(date)); 原文地址:https://www.cnblogs.com/elnimo/p/106

copy sqlserver中DATE类型的数据转化 CONVERT

copy http://www.cnblogs.com/benwu/p/3939044.html 主要描述的是SQL Server使用convert取得datetime日期数据的实际操作流程,在实际操作中用SQL Server数据库中用convert来获取datetime日期数据,以下实例包含各种日期格式的转换. 语句及查询结果: Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varch

sqlserver中DATE类型的数据转化 CONVERT

主要描述的是SQL Server使用convert取得datetime日期数据的实际操作流程,在实际操作中用SQL Server数据库中用convert来获取datetime日期数据,以下实例包含各种日期格式的转换. 语句及查询结果: Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE(), 1): 05/16/0 SQL Server使用conve

hibernate查询返回一个list ,Date类型追加数据

public Pagination getLookPage(BeanPatrolScheduling beanPatrolScheduling, int pageNo, int pageSize) { SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); Pagination pagination = new Pagination(); StringBuffer sql = new StringBuffer(