溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Elasticsearch 500萬(wàn)索引批量存儲(chǔ)php的示例分析

發(fā)布時(shí)間:2021-11-12 11:05:11 來(lái)源:億速云 閱讀:192 作者:小新 欄目:云計(jì)算

這篇文章給大家分享的是有關(guān)Elasticsearch 500萬(wàn)索引批量存儲(chǔ)php的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

引用文件

dict.txt.cache.json, pinyin.php 云盤下載地址:

http://pan.baidu.com/s/1c1W6fQC

索引生成測(cè)試代碼 php

單條索引生成,速度較慢  elastic.php

<?php
use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';

include('pinyin.php');
ini_set('memory_limit','10280M');
$start = "開(kāi)始時(shí)間:" . time() . PHP_EOL;
echo $start;
$client = ClientBuilder::create()->build();

$dict = array_map(function($str){
    return str_ireplace('.', '', $str);},
    array_keys(json_decode(file_get_contents('./dict.txt.cache.json'),
        true)
    )
);
$a = 1;
do {
    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => $a,
        'body' => [
            'user_id' =>  mt_rand(100000000, 2000000000).'@qq.com',
            'name' => $tmpname = $dict[mt_rand(1000,360000)],
            'py' => \Utils\Pinyin::conv($tmpname),
            'phone' => array_rand(array_flip([13,15,17,18])) . mt_rand(100000000, 999999999),
            'mail' => substr(
                    str_shuffle(
                        "0123456789abcdefghijklmnopqrstuvwxyz"),
                    0, mt_rand(5,20)
                ) . '@' . array_rand(
                    array_flip(
                        [
                            '163.com',
                            '126.com',
                            'yeah.net',
                            'qq.com',
                            'foxmail.com',
                            'gmail.com',
                            'yahoo.com',
                            'hotmail.com',
                            'sina.com',
                            'sina.cn',
                            'sina.com.cn'
                        ]
                    )
                ),
            'appoint' => implode(
                '-',
                array_map(function() use($dict) {
                    return $dict[mt_rand(1000,360000)] . (mt_rand(0,100) > 60 ?
                        $dict[mt_rand(1000,360000)] : '') . (mt_rand(0,100) > 80 ?
                        $dict[mt_rand(1000,360000)] : '');},
                    array_pad([], mt_rand(2,5), '')
                )
            ),
        ]
    ];
    //創(chuàng)建索引
    $response = $client->index($params);

    $a = isset($a) ? ++$a : 1;
    $a % 50 === 0 ? print(($b = isset($b) ? ++$b : 1).'%'.PHP_EOL) : '';
} while ($a <= 5000);
$end = "結(jié)束時(shí)間:" . time() . PHP_EOL;
echo $end;
echo "耗時(shí):" . ($end - $start);

批量索引生成  elasticBulk.php

<?php
/**
 * 500萬(wàn)用戶模擬數(shù)據(jù)批量新增到 elasticsearch demo
 */
use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';

include('pinyin.php');
ini_set('memory_limit','10280M');

$connectionPool = '\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool';
$selector = '\Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector';
$client = ClientBuilder::create()
    ->setRetries(10)//重試次數(shù),默認(rèn)重試次數(shù)為集群節(jié)點(diǎn)數(shù)
    ->setConnectionPool($connectionPool)
    ->setSelector($selector)
    ->build();

$dict = array_map(function($str){
    return str_ireplace('.', '', $str);},
    array_keys(json_decode(file_get_contents('./dict.txt.cache.json'),
            true)
    )
);

echo $start = "開(kāi)始時(shí)間:" . time() . PHP_EOL;
createData($client, $dict);
echo $end = "結(jié)束時(shí)間:" . time() . PHP_EOL;
echo "耗時(shí):" . $end - $start;

function createData($client, $dict){
    $bulk = array('index'=>'my_index4','type'=>'my_type4');
    //bulk批量生成
    for($j = 0;$j <= 99; $j++) {
        for($i = $j * 50000 + 1; $i <= $j * 50000 + 50000; $i ++) {
            $bulk['body'][]=array(
                'index' => array(
                    '_id'=>$i
                ),
                'type' => 'blocking'
            );

            $bulk['body'][] = [
                'user_id' =>  mt_rand(100000000, 2000000000).'@qq.com',
                'name' => $tmpname = $dict[mt_rand(1000,360000)],
                'py' => \Utils\Pinyin::conv($tmpname),
                'phone' => array_rand(array_flip([13,15,17,18])) . mt_rand(100000000, 999999999),
                'mail' => substr(
                        str_shuffle(
                            "0123456789abcdefghijklmnopqrstuvwxyz"),
                        0, mt_rand(5,20)
                    ) . '@' . array_rand(
                        array_flip(
                            [
                                '163.com',
                                '126.com',
                                'yeah.net',
                                'qq.com',
                                'foxmail.com',
                                'gmail.com',
                                'yahoo.com',
                                'hotmail.com',
                                'sina.com',
                                'sina.cn',
                                'sina.com.cn'
                            ]
                        )
                    ),
                'appoint' => implode(
                    '-',
                    array_map(function() use($dict) {
                        return $dict[mt_rand(1000,360000)] . (mt_rand(0,100) > 60 ?
                            $dict[mt_rand(1000,360000)] : '') . (mt_rand(0,100) > 80 ?
                            $dict[mt_rand(1000,360000)] : '');},
                        array_pad([], mt_rand(2,5), '')
                    )
                ),
            ];
        }
        $client->bulk($bulk);

        //進(jìn)度統(tǒng)計(jì)
        print($j + 1).'%'.PHP_EOL;
    }
}

單節(jié)點(diǎn)測(cè)試結(jié)論

1.單條性能約100條/s , 批量性能約5000條/s

2.單節(jié)點(diǎn)批量處理一次性bulk數(shù)據(jù)上限35萬(wàn),否則報(bào)錯(cuò):PHP Fatal error:  Uncaught exception 'Elasticsearch\Common\Exceptions\NoNodesAvailableException' with message 'No alive nodes found in your cluster

3. 500萬(wàn)全真模擬數(shù)據(jù)占空間2G

Elasticsearch 500萬(wàn)索引批量存儲(chǔ)php的示例分析

4.查詢性能:

第一次稍慢,但在1秒以內(nèi)

Elasticsearch 500萬(wàn)索引批量存儲(chǔ)php的示例分析

第二次極速

Elasticsearch 500萬(wàn)索引批量存儲(chǔ)php的示例分析感謝各位的閱讀!關(guān)于“Elasticsearch 500萬(wàn)索引批量存儲(chǔ)php的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI