Logstash 로 DB와 연동하여 데이터를 가져오는 방법이 있지만 아래와 같이 간단한 스크립트로 다량의 데이터를 insert 할 수 있다. 너무 많은 데이터를 한방에 insert 하면 에러가 발생하기도 하니 끊어서 하면 좋을 듯.
insert_bulk.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <?php require 'vendor/autoload.php'; require '../setting.php'; if ($argc != 3) { fwrite(STDERR, "잘못된 인자입니다. index type\n\n"); exit(1); } // index, type 이름을 인자로 받는다. $index = $argv[1]; $type = $argv[2]; // 데이터를 가져올 mysql 혹은 mariadb 연결 $pdo = new PDO( 'mysql:host='.$setting['host'].';dbname='.$setting['db'].';port='.$setting['port'].';charset='.$setting['charset'], $setting['username'], $setting['password'] ); // 인스턴트 생성, Elasticsearch 가 9200 가 아닌 다른 포트를 사용할 경우 포트를 별도로 지정해야 한다. $hosts = [ 'localhost:9201' ]; use Elasticsearch\ClientBuilder; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts ->build(); // Build the client object $sql = "SELECT a.newscode, DATE_FORMAT(pubdate, '%Y-%m-%dT%H:%i:%s') pdate, UNIX_TIMESTAMP(pubdate) tdate, titlestr, linkstr, mediastr, contents FROM tbnews where pubdate >= :startdate and pubdate <= :enddate"; $startDate = '2018-10-17'; $endDate = '2018-10-20'; $fields = [ ':startdate' => $startDate, ':enddate' => $endDate ]; $stmt = $pdo->prepare($sql); $stmt->execute($fields); while($row = $stmt->fetch(PDO::FETCH_NUM)) { $newscode = $row[0]; $datetime = $row[1]; $timestamp = $row[2]; $titlestr = $row[3]; $linkstr = $row[4]; $mediastr = $row[5]; $summary = $row[6]; // echo $newscode."\t"; // echo $datetime."\t"; // echo $timestamp."\n"; // echo $titlestr."\n"; // echo $linkstr."\n"; // echo $mediastr."\n"; // echo $summary."\n\n"; // continue; $params['body'][] = [ 'index' => [ '_index' => $index, '_type' => $type, '_id' => $newscode ] ]; $params['body'][] = [ 'title' => $titlestr, 'link' => $linkstr, 'media' => $mediastr, 'datetime' => $datetime, 'summary' => $summary, 'timestamp' => $timestamp ]; } $responses = $client->bulk($params); $pdo = null; ?> | cs |
'Elasticsearch6' 카테고리의 다른 글
elasticsearch 6.* 기본기-2 Index (0) | 2018.10.22 |
---|---|
elasticsearch 6.* 기본기-1 Search (0) | 2018.10.19 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 4 (search) (0) | 2018.10.17 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 2 (php 설정) (0) | 2018.10.16 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 1 (환경구성) (0) | 2018.09.19 |