kafka-php
kafka-php
kafka-php的github地址 https://github.com/jacky5059/kafka-php
生产者produce示例代码
<?php
set_include_path(
implode(PATH_SEPARATOR, array(
realpath(__DIR__ . '/../lib'),
get_include_path(),
))
);
require 'autoloader.php';
$host = 'localhost';
$port = 9092;
$topic = 'test';
$producer = new Kafka_Producer($host, $port, Kafka_Encoder::COMPRESSION_NONE);
$in = fopen('php://stdin', 'r');
while (true) {
echo "\nEnter comma separated messages:\n";
$messages = explode(',', fgets($in));
foreach (array_keys($messages) as $k) {
//$messages[$k] = trim($messages[$k]);
}
$bytes = $producer->send($messages, $topic);
printf("\nSuccessfully sent %d messages (%d bytes)\n\n", count($messages), $bytes);
}
简单消费者simple consumer示例代码
<?php
set_include_path(
implode(PATH_SEPARATOR, array(
realpath(__DIR__ . '/../lib'),
get_include_path(),
))
);
require 'autoloader.php';
$host = 'localhost';
$zkPort = 2181; //zookeeper
$kPort = 9092; //kafka server
$topic = 'test';
$maxSize = 10000000;
$socketTimeout = 2;
$offset = 0;
$partition = 0;
$nMessages = 0;
$consumer = new Kafka_SimpleConsumer($host, $kPort, $socketTimeout, $maxSize);
while (true) {
try {
//create a fetch request for topic "test", partition 0, current offset and fetch size of 1MB
$fetchRequest = new Kafka_FetchRequest($topic, $partition, $offset, $maxSize);
//get the message set from the consumer and print them out
$partialOffset = 0;
$messages = $consumer->fetch($fetchRequest);
foreach ($messages as $msg) {
++$nMessages;
echo "\nconsumed[$offset][$partialOffset][msg #{$nMessages}]: " . $msg->payload();
$partialOffset = $messages->validBytes();
}
//advance the offset after consuming each message
$offset += $messages->validBytes();
//echo "\n---[Advancing offset to $offset]------(".date('H:i:s').")";
unset($fetchRequest);
//sleep(2);
} catch (Exception $e) {
// probably consumed all items in the queue.
echo "\nERROR: " . get_class($e) . ': ' . $e->getMessage()."\n".$e->getTraceAsString()."\n";
sleep(2);
}
}
基于zookeeper的消费者zkconsumer示例代码
<?php
set_include_path(
implode(PATH_SEPARATOR, array(
realpath(__DIR__ . '/../lib'),
get_include_path(),
))
);
require 'autoloader.php';
// zookeeper address (one or more, separated by commas)
$zkaddress = 'localhost:8121';
// kafka topic to consume from
$topic = 'testtopic';
// kafka consumer group
$group = 'testgroup';
// socket buffer size: must be greater than the largest message in the queue
$socketBufferSize = 10485760; //10 MB
// approximate max number of bytes to get in a batch
$maxBatchSize = 20971520; //20 MB
$zookeeper = new Zookeeper($zkaddress);
$zkconsumer = new Kafka_ZookeeperConsumer(
new Kafka_Registry_Topic($zookeeper),
new Kafka_Registry_Broker($zookeeper),
new Kafka_Registry_Offset($zookeeper, $group),
$topic,
$socketBufferSize
);
$messages = array();
try {
foreach ($zkconsumer as $message) {
// either process each message one by one, or collect them and process them in batches
$messages[] = $message;
if ($zkconsumer->getReadBytes() >= $maxBatchSize) {
break;
}
}
} catch (Kafka_Exception_OffsetOutOfRange $exception) {
// if we haven't received any messages, resync the offsets for the next time, then bomb out
if ($zkconsumer->getReadBytes() == 0) {
$zkconsumer->resyncOffsets();
die($exception->getMessage());
}
// if we did receive some messages before the exception, carry on.
} catch (Kafka_Exception_Socket_Connection $exception) {
// deal with it below
} catch (Kafka_Exception $exception) {
// deal with it below
}
if (null !== $exception) {
// if we haven't received any messages, bomb out
if ($zkconsumer->getReadBytes() == 0) {
die($exception->getMessage());
}
// otherwise log the error, commit the offsets for the messages read so far and return the data
}
// process the data in batches, wait for ACK
$success = doSomethingWithTheMessages($messages);
// Once the data is processed successfully, commit the byte offsets.
if ($success) {
$zkconsumer->commitOffsets();
}
// get an approximate figure on the size of the queue
try {
echo "\nRemaining bytes in queue: " . $consumer->getRemainingSize();
} catch (Kafka_Exception_Socket_Connection $exception) {
die($exception->getMessage());
} catch (Kafka_Exception $exception) {
die($exception->getMessage());
}
kafka-php的更多相关文章
- Spark踩坑记——Spark Streaming+Kafka
[TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...
- 消息队列 Kafka 的基本知识及 .NET Core 客户端
前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...
- kafka学习笔记:知识点整理
一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险. ...
- .net windows Kafka 安装与使用入门(入门笔记)
完整解决方案请参考: Setting Up and Running Apache Kafka on Windows OS 在环境搭建过程中遇到两个问题,在这里先列出来,以方便查询: 1. \Jav ...
- kafka配置与使用实例
kafka作为消息队列,在与netty.多线程配合使用时,可以达到高效的消息队列
- kafka源码分析之一server启动分析
0. 关键概念 关键概念 Concepts Function Topic 用于划分Message的逻辑概念,一个Topic可以分布在多个Broker上. Partition 是Kafka中横向扩展和一 ...
- Kafka副本管理—— 为何去掉replica.lag.max.messages参数
今天查看Kafka 0.10.0的官方文档,发现了这样一句话:Configuration parameter replica.lag.max.messages was removed. Partiti ...
- Kafka:主要参数详解(转)
原文地址:http://kafka.apache.org/documentation.html ############################# System ############### ...
- kafka
2016-11-13 20:48:43 简单说明什么是kafka? Apache kafka是消息中间件的一种,我发现很多人不知道消息中间件是什么,在开始学习之前,我这边就先简单的解释一下什么是消息 ...
- Spark Streaming+Kafka
Spark Streaming+Kafka 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端, ...
随机推荐
- Tomcat内存溢出解决java.lang.OutOfMemoryError: PermGen space
背景:把两个项目同时部署在tomcat,启动快好的时候,报java.lang.OutOfMemoryError: PermGen space 原因:因为两个项目的jar包太多,JVM把里面的class ...
- Linux centos 6.4安装
Linux系统安装: 开启虚拟机: 界面说明:Install or upgrade an existing system 安装或升级现有的系统install system with basic vid ...
- 面向对象中的__slots__
类中的__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性. 而要限制class的属性就需要用到__sl ...
- Django 允许其他可以访问的设置
第一步:在run下修改edit 第二步:host改为0.0.0.0 第三步:setting文件中将 ALLOWED_HOSTS 改为 :ALLOWED_HOSTS = ['*',] 这样就可以通 ...
- [原创] Linux 中的 nohup 与 &
目录 背景 结论放前面 & nohup nohup + & 测试 直接运行 单独使用 & 单独使用 nohup nohup + & 背景 一直没搞清楚 nohup 与 ...
- [转] Jenkins pipeline 踩坑集合
[From] https://testerhome.com/topics/10328 前言 最近由于项目需要,接触到了Jenkins 2.0版本,其中最重要的特性就是提供了对pipeline的支持.简 ...
- 论文阅读 | ExtremeNet:Bottom-up Object Detection by Grouping Extreme and Center Points
相关链接 论文地址:https://arxiv.org/abs/1901.08043 论文代码:https://github.com/xingyizhou/ExtremeNet 概述 ExtremeN ...
- 集合 相关 深浅copy
'' 集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. {} ''' # set1 = set({1,2,3}) # set2 = {1,2,3,[2,3],{'name':' ...
- spark第八篇:与Phoenix整合
spark sql可以与hbase交互,比如说通过jdbc,但是实际使用时,一般是利用phoenix操作hbase.此时,需要在项目中引入phoenix-core-4.10.0-HBase-1.2.j ...
- docker升级
使用系统自带的docker源安装docker,安装的版本都是偏低的,因此需要进行版本升级 升级步骤如下: 1. 查找现主机上关于docker的已安装包, 若列出为空,跳过第2步 rpm -qa|gre ...