Guest User

Untitled

a guest
Dec 11th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. var kafka = require('kafka-node');
  2. var HighLevelProducer = kafka.HighLevelProducer;
  3. var KeyedMessage = kafka.KeyedMessage;
  4. var Client = kafka.Client;
  5. var client = new Client('localhost:9200', 'my-client-id', { sessionTimeout: 300, spinDelay: 100, retries: 2 }); // For this demo we just log client errors to the console.
  6. client.on('error', function(error) { console.error(error); });
  7.  
  8. var producer = new HighLevelProducer(client);
  9. console.log('==========')
  10. producer.on('ready', function() {
  11. console.log('++++++++++++')
  12. var messageBuffer = Buffer.from({
  13. enumField: 'sym1',
  14. id: '3e0c63c4-956a-4378-8a6d-2de636d191de',
  15. timestamp: Date.now()
  16. });
  17. // Create message and encode to Avro buffer
  18. // Create a new payload
  19. var payload = [{
  20. topic: 'node-test',
  21. messages: messageBuffer,
  22. attributes: 1 /* Use GZip compression for the payload */
  23. }];
  24.  
  25. //Send payload to Kafka and log result/error
  26. producer.send(payload, function(error, result) {
  27. console.info('Sent payload to Kafka: ', payload);
  28. if (error) {
  29. console.error(error);
  30. } else {
  31. var formattedResult = result[0]
  32. console.log('result: ', result)
  33. }
  34. });
  35. });
  36.  
  37. // For this demo we just log producer errors to the console.
  38. producer.on('error', function(error) {
  39. console.error(error);
  40. });
  41.  
  42. // var Producer = kafka.Producer
  43. // var client = new kafka.Client('localhost:')
Add Comment
Please, Sign In to add comment