JAVA接口将从MySql查出的大量数据导入到Elasticsearch中实现类


这里是我的Service实现类, Controller 和 DAO层省略。

public void insertBigData() {
		//如果使用默认的cluster请保持Setting Empty
		Settings settings = Settings.EMPTY;
		TransportClient client;
		try {	
			//TransportClient连接es
			client = createConnection(settings,"127.0.0.1",9300);
			//调用插入大量数据方法
			String message = insertSomeRecords(client);
			System.out.println(message);
			//关闭连接
			client.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}


建立TransportClient连接的方法

private TransportClient createConnection(Settings settings,String ip,int port) throws UnknownHostException{
			TransportClient client = new PreBuiltTransportClient(settings)
	        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip),port));
			return client;
		}


BulkRequestBuilder插入多个文档

private String insertSomeRecords(TransportClient client){
			// 定义你的elasticsearch 的  index 和 type
			String index = "alloutlet";
			String type = "details";
			// 创建一个Json的list, 插入自己从MySql数据库查出的数据
			List> userList = outletOwnerMapper.allOutlet();
			Long count = (long) userList.size();		
			//核心方法BulkRequestBuilder拼接多个Json
			BulkRequestBuilder bulkRequest = client.prepareBulk();
			for (int i = 0; i < count; i++) {
			    bulkRequest.add(client.prepareIndex(index, type).setSource(userList.get(i)));
			}
			//插入文档至ES, 完成!
			bulkRequest.execute().actionGet();
			return bulkRequest.toString();
		}
		


 
  



完成后用es head查看,文档已插入成功,共48477个。 

JAVA接口将从MySql查出的大量数据导入到Elasticsearch中实现类_第1张图片





你可能感兴趣的:(elasticsearch)