ES數據-MySql處理Date類型的數據導入處理

  用ES的小夥伴們,相信你們都遇到過Mapping處理Date類型的數據頭疼問題吧。json

  不用頭疼了,我來給你提供一種解決方案:api

  一、Maping定義爲:app

    {
  "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類型。ui

 

在code中執行代碼時,用MySql函數UNIX_TIMESTAMP(cai.paytime)獲取日期的秒數據,插入到ES中code

 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成功!");
    }get

,執行添加,提醒一下ES默認會設置分詞,在添加以前,應該首先定義Mapping,在執行添加。string

而後就能夠執行select、update、delete操做了。it

相關文章
相關標籤/搜索