zoukankan      html  css  js  c++  java
  • 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操作了。

  • 相关阅读:
    Asp.net 动态添加Meta标签
    【转】在SharePoint Server 2010中更改“我的网站”
    SPQuery DateTime 类型查询
    Asp.net Web Application 打开 SharePoint 2010 Site 错误 The Web application at could not be found
    How To Create SharePoint 2010 Site Collection In Its Own DB
    C# 文件打印
    面试题 java集合
    《深入理解Java虚拟机》(六)堆内存使用分析,垃圾收集器 GC 日志解读
    《深入理解Java虚拟机》(五)JVM调优
    《深入理解Java虚拟机》(四)虚拟机性能监控与故障处理工具
  • 原文地址:https://www.cnblogs.com/ywzq/p/5865701.html
Copyright © 2011-2022 走看看