本篇内容主要讲解“elasticsearch批量入库bulk性能实例测试”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“elasticsearch批量入库bulk性能实例测试”吧!
我的需求很简单,就是用httpCilent组件,通过REST批量接口bulk提交一些数据。跑一下代码1W条数据导入完成耗时0.843秒,到这里我本可以高高兴兴的交差了,但是我又干了一件事情,用Java原生客户端跑了1W条数据,发现导入速度是0.359秒。管他呢,完成任务。但是,,但是我纠结了,作为一个有追求,有思想的码农,我纠结了。这个时间差距有点大啊。我就怀疑代码有问题了。我就又用了官方的JEST客户端试了下速度。发现速度也不快啊。然后,我的心理就一直缠绕着一个问题,批量导入哪家强?就做了一系列测试。本来测试个Http就好了,但是,又一个但是。我就根本停不下来了。
单位是秒,数据量是1W条,总大小是672 kb。httpClient v1是用最笨的方法写的。V2s是加入了线程池,先初始化Client。速度取了3次的平均值。Java的客户端快的时候有0.2秒,也就是每秒5W多记录。测试10W条记录的时候发生了一点意外XContentBuilder太吃内存了,也就是JSON解析。我的2G内存的渣渣机器,跑不起来。换了服务器给3G内存测试的。测试数据很简单,下面这样。
{"create":{"_index":"nq_test2","_type":"base"}}\n{"name":"value1"}\n
第一组
0.359 java
0.843 httpClient v1
0.593 httpClient v2
0.719 jest
0.581 javascript
0.739 python
0.389 nest (.net客户端)
第二组
10W 条 6825 kb
5.343 python
2.746 httpClient v2
2.145 java
http协议的REST接口,总是要比TCP协议的Java原生客户端要慢一些,意外发现是C#的Http组件,性能不错啊。
python
def test(): actions=[] for i in range(1,100001): actions.append({"_index": 'nq_test2', "_type": "python","_source":{ "name":"value"+str(i)}}) starttime = time.clock() helpers.bulk(es,actions,chunk_size=50000) endtime = time.clock() print "cost = "+str(endtime - starttime)
JS
var client = new elasticsearch.Client({ host: 'http://myes:9200', sniffOnStart: false, sniffInterval: 60000, }); function buildData(){ var actions=[]; for(var i=1;i<=10000;i++){ actions.push({ index: { _index: 'nq_test2', _type: 'js'} }); actions.push({ name: 'value'+i }); } return actions; } function test(){ var actions = buildData(); var beginTime = new Date().getTime(); client.bulk({ body: actions }, function (err, resp) { var endTime = new Date().getTime(); //console.info(resp); $("#costDiv").html('cost = '+(endTime-beginTime)/1000.0); $("#tookDiv").html('took = '+resp.took); }); }
Java
public void test(int count) throws IOException{ BulkRequestBuilder bulkRequest = client.prepareBulk(); System.out.println("count ="+count); for(int i=1;i<=count;i++){ bulkRequest.add(client.prepareIndex("nq_test2", "java") .setSource(jsonBuilder() .startObject() .field("name", "value"+i) .endObject() ) ); } long beginTime = System.currentTimeMillis(); BulkResponse bulkResponse = bulkRequest.execute().actionGet(); if (bulkResponse.hasFailures()) { System.out.println("erros"); } long endTime = System.currentTimeMillis(); System.out.println("took ="+bulkResponse.getTookInMillis()); System.out.println("cost = "+(endTime-beginTime)/1000f); }
Jest
public void test() { Bulk.Builder builder = new Bulk.Builder() .defaultIndex("nq_test2") .defaultType("jest"); for(int i=1;i<=10000;i++){ builder.addAction(new Index.Builder("{\"name\":\"value"+i+"\"}").build()); } Bulk bulk =builder.build(); try { long beginTime = System.currentTimeMillis(); JestResult result = client.execute(bulk); long endTime = System.currentTimeMillis(); System.out.println("cost = "+(endTime-beginTime)/1000f); System.out.println("took = "+ result.getValue("took")); } catch (Exception e) { e.printStackTrace(); } }
Nest (c#)
private void testBtn_Click(object sender, EventArgs e) { var settings = new ConnectionSettings( new Uri(urlTxt.Text) ); var Client = new ElasticClient(settings); var Descriptor = new BulkDescriptor(); Descriptor.FixedPath("nq_test2", "dotnet"); var Count = Convert.ToInt32(numericUpDown1.Value); foreach (var i in Enumerable.Range(0, Count)) { Descriptor.Create<Item>(op => op.Document(new Item { name = "value"+i })); } var BeginTime = DateTime.Now.Millisecond; var Result = Client.Bulk(Descriptor); var EndTime = DateTime.Now.Millisecond; label1.Text = "cost = "+((EndTime - BeginTime) / 1000f).ToString()+" sec"; label3.Text = "took = " + Result.Took.ToString() + " sec"; }
到此,相信大家对“elasticsearch批量入库bulk性能实例测试”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。