码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-09-12 19:09:47      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

  用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类型的数据导入处理

标签:

原文地址:http://www.cnblogs.com/ywzq/p/5865701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!