public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new kafka(); } return self::$instance; }
function runConsumer($topic) { $lockfile = '/tmp/mytest.lock'; $startTime = explode(' ', microtime()); $kmlCachePath = getconfig('kmlCachePath'); //本地缓存里存在数据则优先执行 $cacheFiles = getFileList('./cache/' . $topic); if (!empty($cacheFiles)) { sort($cacheFiles); foreach ($cacheFiles as $f) { $kmls = json_decode(file_get_contents($f)); $items = array_chunk($kmls, 25); foreach ($items as $item) { updataKml($item, $startTime, $f, 2, $topic); } } } // $i = 1; $f = ''; logs(date('h:i:s', time()) . $topic . ' start ...', 1, 'consumer', $topic); while ($da = kafka::getInstance()->get($topic)) { $starttime = explode(' ', microtime()); if (!empty($da->messageList)) { foreach ($da->messageList as $d) { $kmls[] = json_decode($d->message); } //$i++; //if($i > 10){ updataKml($kmls, $starttime, $f, 1, $topic); usleep(10); logs(date('H:i:s') . 'sleep 10', 1, 'consumer', $topic); $kmls = []; /* $i = 1; } }else{ if(!empty($kmls)){ updataKml($kmls,$starttime,$f,1, $topic); } break;*/ } else { unlink($lockfile); logs('success total time:' . getTime($startTime), 1, 'consumer', $topic); echo 'aa'; exit; } } logs('success total time:' . getTime($startTime), 1, 'consumer', $topic); unlink($lockfile); }
function insertKafka($kmldata, $f) { $formatArr = formatKml($kmldata, $f); if (!empty($formatArr)) { foreach ($formatArr as $k => $list) { $rows = array_chunk($list, 50); foreach ($rows as $row) { kafka::getInstance()->sendList($k, $row); } } } }
<?php require_once './kafka.php'; //$client = kafka::getInstance(); //$client->send(); /*for($i=1;$i<100;$i++){ kafka::getInstance()->send('storeId_1','aa'.$i); } **/ $res = kafka::getInstance()->get('storeId_1'); print_r($res);