UNPKG

@oriolrius/node-red-contrib-kafka

Version:

Node-RED Kafka nodes: Send, Receive, and Schema validation with modern KafkaJS

135 lines (104 loc) 5.38 kB
# Kafka Schema Consumer Development Summary ## Overview We have successfully developed and enhanced the `kafka-schema-consumer` node with advanced features that extend beyond the basic capabilities of the `kafka-consumer` and align with the schema-processing capabilities of the `kafka-schema-producer`. The node now provides enterprise-grade functionality for consuming Avro-encoded messages from Kafka topics with full schema validation and advanced processing features. ## Features Implemented ### Core Functionality (Existing) ✅ **Avro Message Decoding** - Automatic decoding of Avro messages using Confluent Schema Registry ✅ **Schema Validation** - Validates incoming messages against registered schemas ✅ **Consumer Group Management** - Configurable consumer groups with UUID generation ✅ **Offset Management** - Support for earliest, latest, and none offset strategies ✅ **Schema Registry Integration** - Full integration with Confluent Schema Registry ✅ **Authentication Support** - Basic auth for Schema Registry ✅ **Auto Schema Registration** - Automatically register schemas if not found ✅ **Error Handling** - Configurable error handling with skip-invalid-messages option ### Enhanced Features (New) 🚀 **Multi-Output Support** - Separate valid and invalid messages to different outputs 🚀 **Performance Metrics** - Real-time processing time and throughput monitoring 🚀 **Schema Evolution Tracking** - Monitors and logs schema version changes 🚀 **Batch Processing** - Groups messages into configurable batches for efficient processing 🚀 **Advanced Status Monitoring** - Detailed status with metrics and progress indicators 🚀 **Enhanced Error Reporting** - Comprehensive error details with processing context 🚀 **Schema Caching** - Improved performance with intelligent schema caching 🚀 **Processing Timeout Configuration** - Configurable timeouts for batch processing ### ✅ Error Handling - Comprehensive error reporting for schema validation failures - Schema Registry connection issues handling - Kafka publishing error management - Detailed debug logging throughout the process ## Configuration Options The node provides these configuration parameters based on your original test code: | Parameter | Description | Example | |-----------|-------------|---------| | **Registry URL** | Schema Registry endpoint | `http://localhost:8081` | | **Schema Subject** | Subject name for schema | `my-topic-value` | | **Use Registry Auth** | Enable registry authentication | ✓/✗ | | **Registry Username** | Auth username | `admin` | | **Registry Password** | Auth password | `password` | | **Auto-register schema** | Create schema if missing | ✓/✗ | | **Default Schema** | JSON schema for auto-registration | Avro schema JSON | | **Validate Only** | Skip Kafka publishing | ✓/✗ | ## Code Architecture The implementation follows your original test code structure: ```javascript // 1. Schema Registry Configuration (like your registryConfig) const registryConfig = { host: config.registryUrl, clientId: 'node-red-schema-producer', retry: { retries: 3, factor: 2, multiplier: 1000, maxRetryTimeInSecs: 60 } }; // 2. Schema Retrieval/Registration (like your getLatestSchemaId logic) async getOrRegisterSchema() { // Try existing schema first, then auto-register if needed } // 3. Message Validation & Encoding (like your registry.encode) const encodedMessage = await registry.encode(schemaId, messageData); // 4. Kafka Publishing (like your producer.send) await producer.send({ topic, messages: [{ key, value: encodedMessage }] }); ``` ## Usage Example ```javascript // Input message { "id": "sensor-001", "message": "Temperature reading", "timestamp": 1640995200000, "metadata": "sensor data" } // Output (success) { "success": true, "schemaId": 1, "kafkaResult": {...}, "originalData": {...}, "topic": "sensor-data" } // Output (validation error) { "error": "Schema validation failed: Field 'timestamp' is required but missing", "originalMessage": {...} } ``` ## Installation 1. The dependency `@kafkajs/confluent-schema-registry` has been added to `package.json` 2. Run `npm install` to install the new dependency 3. The node will appear in the "IOT" category as "Publish with Schema" ## Debug Information The node provides comprehensive debug logging: ``` [Kafka Schema Producer] Schema Registry client created for: http://localhost:8081 [Kafka Schema Producer] Retrieved existing schema ID: 1 [Kafka Schema Producer] Message validated and encoded successfully [Kafka Schema Producer] Publishing validated message to topic: test-topic [Kafka Schema Producer] Message published successfully ``` ## Testing Use the provided `test-schema-producer.js` file to validate the functionality: ```bash node test-schema-producer.js ``` ## Next Steps 1. **Install Dependencies**: Run `npm install` in the project directory 2. **Deploy to Node-RED**: Install the updated package in your Node-RED instance 3. **Configure Schema Registry**: Ensure your Schema Registry is running and accessible 4. **Test the Flow**: Use the example flow to validate the functionality The new "Publish with Schema" node is now ready for use and provides all the Avro schema validation functionality from your original test code, integrated seamlessly into the Node-RED environment!