php kafka 消费代码

set('group.id', 'test-consumer-group');
$rk = new \RdKafka\Consumer($conf);
#设置日志级别
$rk->setLogLevel(LOG_DEBUG);
#集群地址
$rk->addBrokers($brokerList);

$topicConf = new \RdKafka\TopicConf();
#配置没有设置偏移初始量时的消费消息点 smallest表示从最开始消费
$topicConf->set('auto.offset.reset', 'smallest');
#offset存储方式
$topicConf->set('offset.store.method', 'file');
//        $topicConf->set('offset.store.method', 'broker');
#offset存储路径
$topicConf->set('offset.store.path', sys_get_temp_dir());
#自动提交offset时间间隔 0代表禁用
//        $topicConf->set("auto.commit.interval.ms", 100);
$topicConf->set("auto.commit.interval.ms", 60000);
#offset同步到磁盘
$topicConf->set("offset.store.sync.interval.ms", 60000);
#无offset时起始值
$topicConf->set("auto.offset.reset", "earliest");

#跟踪话题
$topic = $rk->newTopic('test10', $topicConf);

#第一个参数 要消耗的分区 , 第二个参数 开始消耗的偏移量  开始使用0分区
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);


while (true) {
	#第一个参数 要消耗的分区 , 第二个参数 超时时间
	$message = $topic->consume(0, 120*10000);

	switch ($message->err) {
		#消息传递成功
		case RD_KAFKA_RESP_ERR_NO_ERROR:
		
			$info = $message->payload;
            //业务处理
			if(!empty($info))
			{
				$info = explode('&',$info);
	            $imgInfo = [];
	            foreach ($info as $item)
	            {
	                $itemInfo = explode('=',$item);
	                $imgInfo[$itemInfo[0]] = $itemInfo[1];
	            }
	            
	            if(is_array($imgInfo))
	            {
	                parseInfo($imgInfo);
	            }
			}
			break;
		#消费者到达分区结尾
		case RD_KAFKA_RESP_ERR__PARTITION_EOF:
//                    $topic->consumeStop(0);
			echo "===2====";
			break;
		case RD_KAFKA_RESP_ERR__TIMED_OUT:
			echo "Timed out\n";
			break;
		default:
			
			break;
	}
}

function parseInfo($imgInfo)
{
	if(!empty($imgInfo))
	{
 		$imgId = empty($imgInfo['image_id'])?0:$imgInfo['image_id'];
        $title = empty($imgInfo['title'])?0:$imgInfo['title'];
        $desc = empty($imgInfo['desc'])?0:$imgInfo['desc'];
        $thumb_path = empty($imgInfo['thumb_path'])?'':$imgInfo['thumb_path'];
        $folder = empty($imgInfo['folder'])?'':$imgInfo['folder'];
        $file_name = empty($imgInfo['file_name'])?'':$imgInfo['file_name'];

        $thumb_path = str_replace('_TB_JIA_','+',$thumb_path);
        $thumb_path = str_replace('_TB_XIE_','/',$thumb_path);
        $thumb_path = str_replace('_TB_DENG_','=',$thumb_path);
        //_TB_DENG_

        if(!empty($thumb_path))
        {
        	$thumb_path = decryptOpenssl($thumb_path);
        	$thumb_path = json_decode($thumb_path);
        }

        $title = parseStr($title);
        $desc = parseStr($desc);

        $title_md5 = md5($title);
        $desc_md5 = md5($desc);
//        $title_cn = ApiTools::getTranslateFromToInfo($title,'en','zh',rand(0,config('param.BAIDU_LENGTH')));
//        $desc_cn = ApiTools::getTranslateFromToInfo($desc,'en','zh',rand(0,config('param.BAIDU_LENGTH')));

        $data['image_id'] = $imgId;
        $data['title_en'] = $title;
        $data['title_md5'] = $title_md5;
        $data['keyword_en'] = $desc;
        $data['keyword_md5'] = $desc_md5;
        $data['update_time'] = time();
        $data['thumb_path'] = $thumb_path;
        $data['folder'] = $folder;
        $data['file_name'] = $file_name;

       	$con = dbConnect();
       	//百度账号
       	$baiduAppIdLen = 2;

	    //更新用户头像
	    $oneSql = "select * from rf_image_attr where image_id='".$imgId."' limit 1";

	    $one = $con->query($oneSql);
	    $one_data=array();
	    while ($tmp=mysqli_fetch_assoc($one)) {
	        $one_data[]=$tmp;
	    }

        if(!count($one_data))
        {
            $title_cn = getTranslateFromToInfo($title,'en','zh',rand(0,$baiduAppIdLen));
            $data['title_cn'] = $title_cn;
            $desc_cn = getTranslateFromToInfo($desc,'en','zh',rand(0,$baiduAppIdLen));
            $data['keyword_cn'] = $desc_cn;
            $data['create_time'] = time();

		 	$sql = "INSERT rf_image_attr(image_id,title_en,title_md5,keyword_en,keyword_md5,update_time,thumb_path,folder,file_name,title_cn,keyword_cn,create_time)VALUES('".$data['image_id']."'
            ,'".$data['title_en']."','".$data['title_md5']."','".$data['keyword_en']."','".$data['keyword_md5']."','".$data['update_time']."','".$data['thumb_path']."','".$data['folder']."','".$data['file_name']."','".$data['title_cn']."','".$data['keyword_cn']."','".$data['create_time']."')";

            $con->query($sql);

        }else{

        	$data['title_cn'] = '';
        	$data['keyword_cn'] = '';
            if($one_data[0]['title_md5']!=$title_md5)
            {
                $title_cn = getTranslateFromToInfo($title,'en','zh',rand(0,$baiduAppIdLen));
                $data['title_cn'] = $title_cn;
            }
            if($one_data[0]['keyword_md5']!=$desc_md5)
            {
               $desc_cn = getTranslateFromToInfo($desc,'en','zh',rand(0,$baiduAppIdLen));
               $data['keyword_cn'] = $desc_cn;
            }
            $sql = "UPDATE rf_image_attr SET update_time=".$data['update_time'].",title_en='".$data['title_en']."',title_cn='".$data['title_cn']."',title_md5='".$data['title_md5']."',keyword_en='".$data['keyword_en']."',keyword_md5='".$data['keyword_md5']."',thumb_path='".$data['thumb_path']."',folder='".$data['folder']."',file_name='".$data['file_name']."',title_cn='".$data['title_cn']."',keyword_cn='".$data['keyword_cn']."' WHERE image_id='".$imgId."'";

            $con->query($sql);
        }

        unset($one_data);
	}

}
function parseStr($val)
{
    if(empty($val))return $val;
    if(strpos($val,'_T_')!=false)$val = str_replace('_T_',' ',$val);
    if(strpos($val,'_K_')!=false)$val = str_replace('_K_',',',$val);
    if(strpos($val,'_KC_')!=false)$val = str_replace('_KC_',',',$val);
    return $val;
}

function dbConnect()
{
	$dbHost = 'sxxx';
	$dbPort = 'xxx';
	$dbUser = 'xxx';
	$dbPwd = 'xxx';
	$dbDB = 'xxxx';
	$con = new mysqli($dbHost, $dbUser, $dbPwd, $dbDB,$dbPort);
	$con->query('set names utf8;');
	return $con;
}

function decryptOpenssl($str)
{
	//解码对应的key IV
	$localKey = "xxx";
	//16个字符
	$localIV     = "xxxx";
	$str = openssl_decrypt($str, 'AES-128-CBC', $localKey,0,$localIV);
	return $str;
}

function getTranslateFromToInfo($item,$from,$to,$ik)
{
	$baiduUrl = "http://api.fanyi.baidu.com/api/trans/vip/translate";


	//百度翻译 对应的 api 和 密钥
	$baiduAppId = [
	'xxx',
	'xxx',
	'xxx'
	];
	//百度翻译 对应的 api 和 密钥
	$baiduSceKey = [
	'xxx',
	'xxx',
	'xxx'
	];

	return translate($item,$from,$to,$baiduAppId[$ik],$baiduSceKey[$ik],$baiduUrl);
}

function translate($query, $from, $to,$appId,$secKey,$url)
{
    $args = array(
        'q' => $query,
        'appid' => $appId,
        'salt' => rand(10000,99999),
        'from' => $from,
        'to' => $to,
    );
    $args['sign'] = buildSign($query, $appId, $args['salt'], $secKey);
    $ret = call($url, $args);
    $ret = json_decode($ret, true);
    return empty($ret['trans_result'][0]['dst'])?$query:$ret['trans_result'][0]['dst'];
}


//加密
function buildSign($query, $appID, $salt, $secKey)
{/*{{{*/
    $str = $appID . $query . $salt . $secKey;
    $ret = md5($str);
    return $ret;
}/*}}}*/

//发起网络请求
function call($url, $args=null, $method="post", $testflag = 0, $timeout = 30, $headers=array())
{/*{{{*/

    $ret = false;
    $i = 0;
    while($ret === false)
    {
        if($i > 1)
            break;
        if($i > 0)
        {
            sleep(1);
        }
        $ret = callOnce($url, $args, $method, false, $timeout, $headers);
        $i++;
    }
    return $ret;
}/*}}}*/

function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = 30, $headers=array())
{
    $ch = curl_init();
    if($method == "post")
    {
        $data = convert($args);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    else
    {
        $data = convert($args);
        if($data)
        {
            if(stripos($url, "?") > 0)
            {
                $url .= "&$data";
            }
            else
            {
                $url .= "?$data";
            }
        }
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(!empty($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    if($withCookie)
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
    }
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}

function convert(&$args)
{/*{{{*/
    $data = '';
    if (is_array($args))
    {
        foreach ($args as $key=>$val)
        {
            if (is_array($val))
            {
                foreach ($val as $k=>$v)
                {
                    $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                }
            }
            else
            {
                $data .="$key=".rawurlencode($val)."&";
            }
        }
        return trim($data, "&");
    }
    return $args;
}/*}}}*/

 

你可能感兴趣的:(php框架,MQ)