elasticSearch使用RestHighLevel批量提交数据

本文介绍了一种使用Java和ElasticSearch REST API批量导入大量数据到ElasticSearch索引的方法。通过读取本地文件,解析CSV数据,并利用批量请求减少网络往返次数,提高数据导入效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

elasticSearch基于restAPI批量提交数据 

public void openfile() {
		String filepath="D:\\DATA\\city_id=341800\\001033_0";
		String encoding ="UTF-8";
		RestHighLevelClient restClient=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.30.128",9200,"http")));
		File files =new File(filepath);
		try {
			InputStreamReader read = new InputStreamReader(new FileInputStream(files),encoding);
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
            try {
            	int i=1;
            	BulkRequest request = new BulkRequest();
            	BulkResponse bulkResponse;
				while((lineTxt = bufferedReader.readLine()) != null){
					String[]  strs=lineTxt.split(",");
					//System.out.println(strs.length);
					request.add(new IndexRequest("test-index", "test", ""+i+"").source(XContentType.JSON,"1", strs[0],
							"2",checkNull(strs[1]),
							"3",checkNull(strs[3]),
							"4",checkNull(strs[4]),
							"5",checkNull(strs[5]),
							....
							"67",checkNull(strs[58])));
					if(i%1000==0) {
						System.out.println(i);
						restClient.bulk(request);
					}
					i++;
				}
				restClient.bulk(request);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (UnsupportedEncodingException | FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值