Node.js和Kafka:消息未发布到kafka主题

时间:2018-06-21 09:48:16

标签: javascript node.js apache-kafka salesforce

与Node.js相比,它相对较新,有人可以告诉我为什么我无法将事件发送给Kafka主题吗?代码不会产生任何错误。

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require('./config.js');
var nforce = require('nforce');
var dateTime = require('node-datetime');
var kafka = require('kafka-node');

var routes = require('./routes/index');

var dt = dateTime.create();

var org = nforce.createConnection({
  clientId: config.CLIENT_ID,
  clientSecret: config.CLIENT_SECRET,
  redirectUri: config.CALLBACK_URL + '/oauth/_callback',
  mode: 'multi',
  environment: config.ENVIRONMENT  // optional, sandbox or production, production default
});

HighLevelProducer = kafka.HighLevelProducer,
client = new kafka.Client('localhost:9092'),
producer = new HighLevelProducer(client),


org.authenticate({ username: config.USERNAME, password: config.PASSWORD }, function(err, oauth) {

  if(err) return console.log(err);
  if(!err) {
    console.log('*** Successfully connected to Salesforce ***');
    // add any logic to perform after login
  }

  // subscribe to a pushtopic
  var str = org.stream({ topic: config.PUSH_TOPIC, oauth: oauth });

  str.on('connect', function(){
    console.log('Connected to pushtopic: ' + config.PUSH_TOPIC);
  });

  str.on('error', function(error) {
    console.log('Error received from pushtopic: ' + error);
  });

  str.on('data', function(data) {
    console.log('Received the following from pushtopic ---');
    var dataStream1 = data['sobject'];
    dataStream1['timestamp'] = dt.format('Y-m-d H:M:S');
    console.log(dataStream1);
    var dataStreamFinal = '[' + JSON.stringify(dataStream1) + ']';

    payloads = [
        { topic: 'testing1', messages: dataStreamFinal }
    ];

    console.log(payloads);

    producer.on('ready', function () {
    producer.send(payloads, function (err, data) {
        console.log(data);
    });
});

});

});


module.exports = {app: app, server: server};

我正在使用节点https://github.com/SOHU-Co/kafka-node的以下模块

在此之前,我只是添加了代码以从salesforce pushTopic中提取事件并将其发布到kafka是我的目标。

1 个答案:

答案 0 :(得分:0)

最终能够将数据推送到Kafka。尽管nforce尚不支持重播消息功能。

items = {'1': '2', '3': '4', '5': '6'}
    choice = input("Select your item: ")
    print(choice)
    for choice  in items:
       if choice in items:
        the_choice = items[choice]
        print("You chose",the_choice)
        break
    else:
        print("Uh oh, I don't know about that item")