elasticsearch 学习博客系列<三> ES 中 index-doc 的 添加(java)

上篇我们说了index的 mapping,这一篇 我们说一下 java 作为 客户端 来操作ES,下面讲一下 添加

说明:.添加的方式 一般以json字符串 来添加

 下面 我们以  ownused  索引为例讲解(ps:相应的 mapping 可自己建立 上一篇说过)

第一步:建立 实体类

package com.test.data;



import com.test.util.IpUtil;
import com.test.util.IPUtil;

/**
 * 
 *zqz
 */

public class OwnUsedAsset{


	
	/**
	 * 全省地址ID
	 */
	private long id;
	
	
	
	/**
	 * 区域-归属地
	 */
	private String area;
	
	/**
	 * IP
	 */
	private String IP;
	
	/**
	 * ipLong 保持唯一性
	 */
	private long ipLong;
	

	
	/**
	 * 状态值-1:正常资产 2:下线资产
	 */
	private long state;
	

	/**
	 * 备注
	 */
	private String remark;
	/**
	 * 备注
	 */
	private String downLineRemark;

	private long createTime;
	
	private long updateTime;
	
	private long actionTime;
	
	/**
	 * 字符串 格式
	 */
	private String detectTime;


	

	public String getDownLineRemark() {
		return downLineRemark;
	}

	public void setDownLineRemark(String downLineRemark) {
		this.downLineRemark = downLineRemark;
	}

	public String getDetectTime() {
		return detectTime;
	}

	public void setDetectTime(String detectTime) {
		this.detectTime = detectTime;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public long getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(long updateTime) {
		this.updateTime = updateTime;
	}

	public long getActionTime() {
		return actionTime;
	}

	public void setActionTime(long actionTime) {
		this.actionTime = actionTime;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}


	public long getState() {
		return state;
	}

	public void setState(long state) {
		this.state = state;
	}
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	

	public String getIP() {
		return IP;
	}

	public void setIP(String iP) {
		this.ipLong = IpUtil.ipToLong(iP);
		this.IP = iP;
	}

	public long getIpLong() {
		return ipLong;
	}

	public void setIpLong(long ipLong) {
		this.ipLong = ipLong;
	}


}


第二步: 对象json 转换类

package com.test.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

	 /**
     * 由对象转化成json 格式,添加到数据库
     */
	private static ObjectMapper objectMapper = new ObjectMapper();  
    public static String toJson(Object o){  
        try {  
            return objectMapper.writeValueAsString(o);  
        } catch (JsonProcessingException e) {  
            e.printStackTrace();  
        }  
        return "";  
    }  
}


第三步:建立 ES Client工具类 获取 ESClient

package com.test.util;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/
 * ES 测试工具类
 * @author zhangqingzhou
 *
 */
public abstract class EsClientUtil{
	/**
	 * 是否为调试
	 */
	private static boolean Debug = false;
	
	public Object execute(Object ... obj){
		@SuppressWarnings("rawtypes")
		List rsList = new ArrayList<>();
		ESClient esclient = null;
		try {
			esclient = (!Debug)?getDefaultDelegateClient():buildClient();
			if(null == esclient)
				return rsList;
			return executeIt(esclient, obj);
		} catch (Exception e) {
		}finally{
			ESClientFactory.me().closeClient(esclient);
		}
		return rsList;
	}
	/**
	 * 接口 的 执行方法,需要重写
	 * @param esclient
	 * @param obj
	 * @return
	 */
	protected abstract Object executeIt(ESClient esclient,Object ... obj);
	
	public static ESClient buildClient(){
		Settings settings = ImmutableSettings.settingsBuilder()//集群名
				.put("client.transport.sniff", true).put("cluster.name", "cupid-es-zhang-1").build();
		@SuppressWarnings("resource")
		TransportClient client = new TransportClient(settings)//地址+ 端口
		.addTransportAddress(new InetSocketTransportAddress("192.168.1.10", 9300));
		 ESClient delegateClient = new ESClient(client, Type.local);
		return delegateClient;
	}
	
	public static ESClient getDefaultDelegateClient(){
		ESClient cupidClient = null;
		try {
			//cupidClient = ESClientFactory.me().getDefaultDelegateClient();// 系统封装,
		} catch (Exception e) {
		}
		return (!Debug)?cupidClient
				:buildClient();
	}

	// 判断 此索引是否 存在
	public static boolean isExistIndex(String indexName){
		boolean flag = false;
		 
		ESClient client =getDefaultDelegateClient();
		try {
			if(client != null){
				flag =client.admin().indices().exists(
					new IndicesExistsRequest()
					.indices(new String[]{indexName}))
					.actionGet().isExists();
				
			}
		} catch (ElasticsearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
}


第四步:添加(分为单个值添加和批量添加)

package com.test.service.esService;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import org.apache.log4j.Logger;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolFilterBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.OrFilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.nutz.ioc.loader.annotation.IocBean;


import com.test.data.OwnUsedAsset;
import com.test.util.EsClientUtil;
import com.test.util.JsonUtil;


/**
 * 
 * @author Administrator
 *
 */
@IocBean
public class OwnUsedAssetService {

	
	
	private OwnUsedAssetService(){};
	/**
	 * 单例模式
	 */
	private static OwnUsedAssetService service = null;
	
	public static OwnUsedAssetService getInstance(){
		if(service == null){
			service = new OwnUsedAssetService();
		}
		
		return service;
	}
	private static String name ="ownused";
	/**
	 * 添加单个数据 boolean exist = EsClientUtil.isExistIndex(name);
	 * @param obj
	 */
	public void insertAddressToDB(OwnUsedAsset obj){
			
			ESClient client = EsClientUtil.getDefaultDelegateClient();
			if(client != null){
				//long id=new Date().getTime()*10000+new Random().nextInt(10000);
				
				obj.setId(obj.getIpLong());
				obj.setState(1);
				obj.setCreateTime(System.currentTimeMillis());
				obj.setUpdateTime(System.currentTimeMillis());
				obj.setActionTime(DateUtil.getNowWeekNumber());
				//obj.setDetectTime(DateUtil.getNowTimeStr());
				String data = JsonUtil.toJson(obj);
				client.prepareIndex(name,name)
				.setId(String.valueOf(obj.getIpLong()))
				.setSource(data)
				.execute().actionGet();
				
				// 添加到内存 OwnUsedAssetCache
				OwnUsedAssetCache.ownUsedCache.put(obj.getIP(), DateUtil.getNowWeekNumber());
			}
	}
	/**
	 * 批量添加
	 * @param objList
	 */
	public void insertAddressToDB(List<OwnUsedAsset> objList){
		if(objList.size() == 0) return;
		ESClient client = EsClientUtil.getDefaultDelegateClient();
		if(client != null){
			long actionTime = DateUtil.getNowWeekNumber();
			// 开启批量
			BulkRequestBuilder prepareBulk = client.prepareBulk();
			for (OwnUsedAsset obj : objList) {
				//long id=new Date().getTime()*10000+new Random().nextInt(10000);
				
				obj.setId(obj.getIpLong());
				obj.setState(1);
				obj.setCreateTime(System.currentTimeMillis());
				obj.setUpdateTime(System.currentTimeMillis());
				obj.setActionTime(actionTime);
				//obj.setDetectTime(DateUtil.getNowTimeStr());
				
				String data = JsonUtil.toJson(obj);
				prepareBulk.add(
						client.prepareIndex(name,name)
						.setId(String.valueOf(obj.getIpLong()))
						.setSource(data)
						);
				
				
				
			}
			
			prepareBulk.execute().actionGet();
			
			
			
		}
	}
	}

说明:首次 添加时  若索引 没有存在 则 创建,并 添加文档 doc,若是 docId 存在 则 覆盖 并 使 版本号 version +1  ,若 数据量过大 注意 分段 分批添加
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值