kafka-node

Producer

Producer(client,[options])

  • client:和kafka服务保持连接的client对象
  • options:一些关于producer的属性

    {
        // Configuration for when to consider a message as acknowledged, default 1 
        requireAcks: 1,
        // The amount of time in milliseconds to wait for all acks before considered, default 100ms 
        ackTimeoutMs: 100,
        // Partitioner type (default = 0, random = 1, cyclic = 2, keyed = 3), default 0 
        partitionerType: 2
    }

Events

  • ready:这个事件被触发,当我们准备好发送消息的时候
  • error:发生错误的时候,触发这个事件

send(payloads,cb)

  • payloads:是一个数组,元素都是ProducerRequest,ProducerRequest是一个JSON Object 类似与下面的数据

    {
       topic: 'topicName',
       messages: ['message body'], // multi messages should be a array, single message can be just a string or a KeyedMessage instance 
       key: 'theKey', // only needed when using keyed partitioner 
       partition: 0, // default 0 
       attributes: 2 // default: 0 
    }
  • cb:是一个回调函数
  • attribute 控制着发送消息的压缩方式
    • 0:不压缩
    • 1:GZip的方式压缩
    • 2: snappy压缩格式
⚠️一批给同一个topic发送多个消息,要使用messages,否则会丢失信息

createTopics(topics,async,cb)

  • 这个方法用来在kafka server上创建新的topics.只有在auto.create.topics.enable是true的时候这个函数才会起作用
  • 我们的client发送消息的给server的时候,server会自动创建队列(topics)
  • 当async呗设置为false的时候,这个函数不会结束,除非所有的topic都被创建,否则是立即返回。就是是否阻塞的意思
  • topics:是一个数组。
  • async: 是一个布尔类型的变量
  • cb: 函数,回调函数
example:
var kafka = require('kafka-node'),
    Producer = kafka.Producer,
    client = new kafka.Client(),
    producer = new Producer(client);
// Create topics sync 
producer.createTopics(['t','t1'], false, function (err, data) {
    console.log(data);
});
// Create topics async 
producer.createTopics(['t'], true, function (err, data) {});
producer.createTopics(['t'], function (err, data) {});// Simply omit 2nd arg 

HighLevelProducer

基本和Producer一样

HighLevelProducer(client,[option])

  • client:和Producer类似,client都是和kafka server保持一个连接
  • options: 属性关于Producer

    {
        // Configuration for when to consider a message as acknowledged, default 1 
        requireAcks: 1,
        // The amount of time in milliseconds to wait for all acks before considered, default 100ms 
        ackTimeoutMs: 100,
        // Partitioner type (default = 0, random = 1, cyclic = 2, keyed = 3), default 2 
        partitionerType: 3
    }

Events

  • ready:被触发当准备好发送消息的时候
  • error:发生错误的时候触发

Consumer

Consumer(client,payloads,options)

  • client:和上方一样
  • payloads:数组,数组的元素类型是FetchRequest,FetchRequest是一个JSON类型的数据,类似如下:

    {
       topic: 'topicName',
       offset: 0, //default 0 
    }
  • options:关于consumer的属性设置,例如下:

    {
        groupId: 'kafka-node-group',//consumer group id, default `kafka-node-group` 
        // Auto commit config 
        autoCommit: true,
        autoCommitIntervalMs: 5000,
        // The max wait time is the maximum amount of time in milliseconds to block waiting if insufficient data is available at the time the request is issued, default 100ms 
        fetchMaxWaitMs: 100,
        // This is the minimum number of bytes of messages that must be available to give a response, default 1 byte 
        fetchMinBytes: 1,
        // The maximum bytes to include in the message set for this partition. This helps bound the size of the response. 
        fetchMaxBytes: 1024 * 1024,
        // If set true, consumer will fetch message from the given offset in the payloads 
        fromOffset: false,
        // If set to 'buffer', values will be returned as raw buffer objects. 
        encoding: 'utf8'
    }
example:
var kafka = require('kafka-node'),
    Consumer = kafka.Consumer,
    client = new kafka.Client(),
    consumer = new Consumer(
        client,
        [
            { topic: 't', partition: 0 }, { topic: 't1', partition: 1 }
        ],
        {
            autoCommit: false
        }
    );

on(‘message’,onMessage)

    一般来说,我们会consume最后一个提交的messages从我们的current group
  • onMessage:函数,新消息来的时候的回调函数
example:
consumer.on('message', function (message) {
    console.log(message);
});

addTopics(topics,cb,fromoffset)

添加进来的topics如果不存在,则返回错误

- topics:数组,被添加的topic的数组
- cb:函数,回调函数
- fromOffset:布尔值,如果是真的,consumber将会从特定的位置获取message,否则默认最后一个

removeTopics(topics,cb)

  • topics:数组,被移除的topic的数组
  • cb:回调函数

Commit(cb)

  • cb:回调函数

setOffset(topic,partition,offset)

设定给定topic的offset

- topic:字符串
- partition:数字
- offset:数字

example:
consumer.setOffset('topic', 0, 0);

Pause()

暂停consumer,仅仅是暂停consumer,不会影响producer

resume()

恢复consumer,恢复fetch 的这个循环

PauseTopics(topics)

暂停指定的topics
example:
consumer.pauseTopics([
    'topic1',
    { topic: 'topic2', partition: 0 }
]);

resumeTopics(topics)

close(force,cb)

  • force:布尔类型的值,如果是true,那么她会强制consumer提交现在的offset在关闭之前,默认的是false
example:
consumer.close(true, cb);
consumer.close(cb); //force is disabled 
`kafka-node`是一个Node.js库,用于编写生产者(Producer)、消费者(Consumer)和管理连接到Apache Kafka集群的应用程序。以下是使用`kafka-node`的基本步骤: 1. **安装依赖**: 使用npm或yarn安装`kafka-node`: ``` npm install kafka-node ``` 2. **初始化连接**: 初始化Producer或Consumer实例,需要提供Kafka服务器的信息,比如主机名和端口号: ```javascript const { KafkaClient, Producer, Consumer } = require('kafka-node'); const client = new KafkaClient({ 'metadata.broker.list': 'localhost:9092' }); ``` 3. **生产者示例**: 对于生产者,你可以创建一个Producer实例并发布消息到指定主题: ```javascript const producer = new Producer(client); producer.on('ready', () => { producer.send([{ topic: 'your-topic', messages: ['Hello World'] }]); producer.close(); }); ``` 4. **消费者示例**: 对于消费者,创建一个Consumer实例并订阅特定的主题: ```javascript const consumer = new Consumer(client, [{ topic: 'your-topic', fromStart: true }], { autoCommit: false }); consumer.on('data', (message) => { console.log(`Received message: ${message.value.toString()}`); }); consumer.on('error', (error) => { console.error(`Consumer error: ${error}`); }); consumer.run(); ``` 消费者通常会自动消费数据,除非设置了`autoCommit: false`,此时需要手动提交偏移量。 5. **关闭连接**: 当不再需要时,记得关闭Producer或Consumer实例以释放资源: ```javascript producer.close(); consumer.stop(); client.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值