Elasticsearch 使用 php curl 插入数据

<?php
/**
 * Created by PhpStorm.
 * User: func7
 * Date: 2018/11/8
 * Time: 11:24
 */
set_time_limit(0);
header("Content-type: text/html; charset=utf-8");

$dbhost     = "127.0.0.1";
$username   = "test";
$userpass   = "testadmin";
$dbdatabase = "test";

$db_con = mysqli_connect($dbhost, $username, $userpass) or die("Unable to connect to the MySQL!");
//选择一个需要操作的数据库
$db_con->query("SET NAMES utf8");
mysqli_select_db($db_con, $dbdatabase);
/***********连接数据库****end******/
//应用查询及处理数据
$sqlData = mysqli_query($db_con, "SELECT
    a.id AS _id,
    a.id,
    a.NAME,
    a.STATUS,
    a.state,
    a.package,
    atype_id,
IF
    ( b.rank = 3, 3, 1 ) AS rank,
IF
    ( channel_type & 2 = 2, 0, 1 ) AS `show` 
FROM
    apps AS a
    LEFT JOIN apps_rank AS b ON a.id = b.app_id 
WHERE
    a.id >1
    AND a.id <=12000
     ");
//提取数据
$Esdata  = array();$cl      = curl_init();
curl_setopt($cl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($cl, CURLOPT_PORT, 9200);
curl_setopt($cl, CURLOPT_TIMEOUT, 2000);
curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cl, CURLOPT_FORBID_REUSE, 0);
curl_setopt($cl, CURLOPT_CUSTOMREQUEST, 'PUT'); //GET 获取 // DELETE 删除 //PUT 插入
if ($sqlData) {
    while ($dataRow = mysqli_fetch_assoc($sqlData)) {
        $Esdata['id']       = intval($dataRow['id']);
        $Esdata['name']     = $dataRow['NAME'];
        $Esdata['package']  = $dataRow['package'];
        $Esdata['state']    = intval($dataRow['state']);
        $Esdata['status']   = intval($dataRow['STATUS']);
        $Esdata['atype_id'] = intval($dataRow['atype_id']);
        $Esdata['rank']     = intval($dataRow['rank']);
        $Esdata['show']     = intval($dataRow['show']);
        $jsonStr            = json_encode($Esdata, JSON_UNESCAPED_UNICODE);
        //修改二,设置es导入
        $baseUri = 'http://localhost:9200/test/test_type/' . $Esdata['id'];
        curl_setopt($cl, CURLOPT_URL, $baseUri);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $jsonStr);
        $response = curl_exec($cl);
    }
}
unset($Esdatas);//销毁数组
file_put_contents('log.log',$response);
curl_close($cl); usleep(50000);//自定义延迟

Elasticsearch的增删改查操作 在使用curl的时候可以修改

curl_setopt($cl, CURLOPT_CUSTOMREQUEST, 'PUT'); //GET 获取 // DELETE 删除 //PUT 插入

 

posted @ 2018-11-08 14:50  Func~  阅读(804)  评论(0编辑  收藏  举报