ES数据-MySql处理Date类型的数据导入处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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操作了。

以上是关于ES数据-MySql处理Date类型的数据导入处理的主要内容,如果未能解决你的问题,请参考以下文章

oracle --date-对应 mysql 时间类型的以及空值的处理

Elasticsearch 日期时间处理

将excel中数据用JAVA代码导入到oracle中,遇到日期类型应该怎么处理!?

Spark SQL大数据处理并写入Elasticsearch

Hibernate @Temporal 注解处理Java与Mysql时间日期类型的映射关系

如何向mysql数据库中插入日期