elasticsearch5.4.0 java开发记录三

本编用来记录读取数据库信息添加到elasticsearch中 我们成为建立索引

建立索引注意几点:

1,如果elasticsearch集群自己定义了集群名称,获取客户端的时候也要带上setting集群名称一样;

2,索引名类型名通俗易懂即可;

3,读取数据库信息转换成json格式数据,因为elasticsearch只支持json格式的数据;

4,第二部分的mapping也可以不设置,当进行数据索引时elasticsearch自动识别类型,但是最好还是自己定义下;

5,索引时最好批量提交;

6,项目日志要清楚;

elasticsearch java代码如下:

/**
	 * 1,创建新的索引 
           2,别名别名指向新的索引 
           3,删除旧的索引及指向旧索引的别名
          4,约定真实索引名称为indexName-yyyymmdd
	 * 别名:indexName
	 */
	@Override
	public void bulkCreateIndex() {

		Date date = new Date();

		String indexName = INDEX_NAME + DateTimeUtils.getStringDate(date);

		String aliasNmae = INDEX_NAME;

		String typeName = TYPE_NAME;

		long start = System.currentTimeMillis();
		indexName = indexName.toLowerCase();
		TransportClient client = elasticsearchClientService.getClient();

		// 创建新索引
		if (ElasticSearchUtils.createIndex(client, indexName)) {
			LOGGER.info("创建索引:【{}】成功.", indexName);
		}
		// 设置索引mapping
		if (ElasticSearchUtils.setIndexMapping(client, indexName, typeName,
				getMapping(indexName))) {
			LOGGER.info("设置索引:【{}】,mapping成功.", indexName);
		}
		List<ProductInfoBo> listBos = new ArrayList<ProductInfoBo>();

		int total = this.getTotal();

		Map<String, Object> conditionMap = new HashMap<String, Object>();

		if (pageSize.isEmpty()) {
			pageSize = "5000";
		}
		int size = Integer.valueOf(pageSize);

		int number = 1;

		do {

			conditionMap.put("start", (number - 1) * size);
			conditionMap.put("end", ((number * size) > total) ? total
					: (number * size));

			long s = System.currentTimeMillis();
			listBos = productInfoDao.query(conditionMap);

			long e = System.currentTimeMillis() - s;
			// 批量提交
			BulkRequestBuilder bulkRequest = client.prepareBulk();

			if (!CollectionUtils.isEmpty(listBos)) {

				s = System.currentTimeMillis();

				for (int i = 0; i < listBos.size(); i++) {
					ProductInfoBo bo = listBos.get(i);
					bulkRequest.add(client.prepareIndex(indexName, typeName)
									.setSource(JSON.toJSONString(bo),XContentType.JSON));
				}
				BulkResponse bulkResponse = bulkRequest.get();
				LOGGER.info("索引完成第【{}】页,从start:【{}】- end:【{}】,查询到数量:【{}】,表查询耗时:【{}】毫秒,索引耗时:【{}】",
						number, conditionMap.get("start"),
						conditionMap.get("end"), listBos.size(), e,
						(System.currentTimeMillis() - s));

				if (bulkResponse.hasFailures()) {
					String message = bulkResponse.buildFailureMessage();
					LOGGER.error("进行第:【{}】页索引数据,出现异常:【{}】", number, message);
				}
				number++;
			}

		} while (!CollectionUtils.isEmpty(listBos));

		try{
			// 别名指向新索引
			if (ElasticSearchUtils.addAliasIndex(client, indexName, aliasNmae)) {
				LOGGER.info("索引【{}】设置别名【{}】成功.", indexName, aliasNmae);
			}
			// 删除旧索引别名
			String oldIndexName = INDEX_NAME+ DateTimeUtils.getYestodayString(date);
			if (ElasticSearchUtils.removeAliasIndex(client, oldIndexName, aliasNmae)) {
				LOGGER.info("清除旧索引:【{}】别名【{}】成功.", oldIndexName, aliasNmae);
			}
			// 删除指向旧索引的别名及旧索引
			if (ElasticSearchUtils.deleteIndex(client, oldIndexName)) {
				LOGGER.info("清除旧索引:【{}】成功.", oldIndexName);
			}
			
		}catch (Exception e) {
			LOGGER.error("添加索引别名异常:【{}】",e);
		}
		LOGGER.info("创建索引【{}】类型【{}】数据量:【{}】,共耗时:【{}】秒", indexName, typeName,
				total, (System.currentTimeMillis() - start) / 1000);
	}
此第三部分到此就完成,上面代码小弟一直担心的地方是,
BulkRequestBuilder bulkRequest = client.prepareBulk();
放在do while 循环中是否有性能上的缺陷,有大神飘过时,望指正。此篇如有错误之处,不足之处望指正,多谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值