input: kafka_franz: seed_brokers: - united-manufacturing-hub-kafka.united-manufacturing-hub.svc.cluster.local:9092 regexp_topics: true topics: - umh.v1.+.+.+.+.+.+.+.mes consumer_group: kafka_to_postgresql_historian_bridge auto_replay_nacks: false pipeline: processors: - bloblang: | let kafka_key = meta("kafka_key") let is_work_order = $kafka_key == "work_order" root = if $is_work_order == true { root } else { deleted() } - bloblang: | # Step 3: Validate that the message is valid JSON let invalid_json = this.catch(true) meta invalid_json_count = if $invalid_json == true { 1 } else { 0 } root = this - metric: type: counter_by name: invalid_json value: ${! meta("invalid_json_count") } - bloblang: | let invalid_json = meta("invalid_json_count") root = if $invalid_json.string() == "0" { root } else { deleted() } - bloblang: | let valid_schema = this.json_schema("""{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["order_id", "external_order_id", "article_id", "planned_quantity", "status", "archived", "planned_start", "planned_end", "created_at", "created_by"], "properties": { "order_id": { "type": "integer" }, "external_order_id": { "type": "string" }, "article_id": { "type": "integer" }, "planned_quantity": { "type": "integer" }, "status": { "type": "string" }, "archived": { "type": "boolean" }, "planned_start": { "type": "string", "format": "date-time" }, "planned_end": { "type": "string", "format": "date-time" }, "created_at": { "type": "string", "format": "date-time" }, "created_by": { "type": "string" }, "actual_start": { "type": ["null", "string"], "format": "date-time" }, "actual_end": { "type": ["null", "string"], "format": "date-time" } } }""").catch(false) # If the message conforms to the schema, keep it; otherwise, delete it root = match { $valid_schema != false => this, # Keep the message if valid _ => deleted() # Delete if schema validation fails #_ => throw("error: schema validation failed") } - log: level: DEBUG fields_mapping: | root.kafka_key = meta("kafka_key") root.is_work_order = meta("kafka_key") == "work_order" root.this_invalid_json = this.catch(true) == true root = root output: broker: pattern: fan_out outputs: - sql_insert: driver: postgres dsn: postgres://kafkatopostgresqlv2:changemetoo@united-manufacturing-hub.united-manufacturing-hub.svc.cluster.local:5432/umh_v2 table: singeling_work_order suffix: ON CONFLICT (order_id) DO NOTHING columns: - order_id - external_order_id - article_id - planned_quantity - status - archived - planned_start - planned_end - actual_start - actual_end - created_at - created_by args_mapping: | [ this.order_id, this.external_order_id, this.article_id, this.planned_quantity, this.status, this.archived, this.planned_start, this.planned_end, this.actual_start.or(null), this.actual_end.or(null), this.created_at, this.created_by ] batching: period: 5s count: 10000 max_in_flight: 512 init_statement: | DO $$ BEGIN -- Create the table if it doesn't exist CREATE TABLE IF NOT EXISTS singeling_work_order ( order_id BIGINT PRIMARY KEY, external_order_id TEXT UNIQUE NOT NULL, article_id BIGINT NOT NULL, planned_quantity INT NOT NULL, status TEXT NOT NULL, archived BOOLEAN NOT NULL, planned_start TIMESTAMPTZ NOT NULL, planned_end TIMESTAMPTZ NOT NULL, actual_start TIMESTAMPTZ, actual_end TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL, created_by TEXT NOT NULL ); END $$; cache_resources: - label: id_cache memory: default_ttl: 24h logger: level: DEBUG format: logfmt add_timestamp: true