atharvalade commented on code in PR #3103: URL: https://github.com/apache/iggy/pull/3103#discussion_r3292154711
########## core/connectors/sinks/s3_sink/src/formatter.rs: ########## @@ -0,0 +1,287 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +use crate::OutputFormat; +use chrono::{DateTime, Utc}; +use iggy_connector_sdk::{ConsumedMessage, MessagesMetadata, Payload, TopicMetadata}; +use serde_json::{Map, Value}; + +pub fn format_message( + message: &ConsumedMessage, + topic_metadata: &TopicMetadata, + messages_metadata: &MessagesMetadata, + include_metadata: bool, + include_headers: bool, + format: OutputFormat, +) -> Vec<u8> { + match format { + OutputFormat::JsonLines | OutputFormat::JsonArray => format_json_message( + message, + topic_metadata, + messages_metadata, + include_metadata, + include_headers, + ), + OutputFormat::Raw => format_raw_message(message), + } +} + +fn format_json_message( + message: &ConsumedMessage, + topic_metadata: &TopicMetadata, + messages_metadata: &MessagesMetadata, + include_metadata: bool, + include_headers: bool, +) -> Vec<u8> { + let mut obj = Map::new(); + + if include_metadata { + obj.insert("offset".to_string(), Value::Number(message.offset.into())); + let ts = timestamp_to_rfc3339(message.timestamp); + obj.insert("timestamp".to_string(), Value::String(ts)); + obj.insert( + "stream".to_string(), + Value::String(topic_metadata.stream.clone()), + ); + obj.insert( + "topic".to_string(), + Value::String(topic_metadata.topic.clone()), + ); + obj.insert( + "partition_id".to_string(), + Value::Number(messages_metadata.partition_id.into()), + ); + } + + if include_headers && let Some(headers) = &message.headers { + let mut headers_obj = Map::new(); + for (key, value) in headers { + headers_obj.insert(key.to_string(), Value::String(value.to_string())); + } + obj.insert("headers".to_string(), Value::Object(headers_obj)); + } + + let payload_value = payload_to_json_value(&message.payload); + obj.insert("payload".to_string(), payload_value); + + match serde_json::to_vec(&Value::Object(obj)) { + Ok(bytes) => bytes, + Err(e) => { + tracing::warn!( + "Failed to serialize message at offset {}: {e}", + message.offset + ); + Vec::new() + } + } +} + +fn format_raw_message(message: &ConsumedMessage) -> Vec<u8> { + message.payload.try_to_bytes().unwrap_or_default() +} + +fn payload_to_json_value(payload: &Payload) -> Value { + match payload { + Payload::Json(value) => { + let bytes = simd_json::to_vec(value).unwrap_or_default(); Review Comment: Replaced with `owned_value_to_serde_json`, single structural conversion ########## core/connectors/sinks/s3_sink/src/formatter.rs: ########## @@ -0,0 +1,287 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +use crate::OutputFormat; +use chrono::{DateTime, Utc}; +use iggy_connector_sdk::{ConsumedMessage, MessagesMetadata, Payload, TopicMetadata}; +use serde_json::{Map, Value}; + +pub fn format_message( + message: &ConsumedMessage, + topic_metadata: &TopicMetadata, + messages_metadata: &MessagesMetadata, + include_metadata: bool, + include_headers: bool, + format: OutputFormat, +) -> Vec<u8> { + match format { + OutputFormat::JsonLines | OutputFormat::JsonArray => format_json_message( + message, + topic_metadata, + messages_metadata, + include_metadata, + include_headers, + ), + OutputFormat::Raw => format_raw_message(message), + } +} + +fn format_json_message( + message: &ConsumedMessage, + topic_metadata: &TopicMetadata, + messages_metadata: &MessagesMetadata, + include_metadata: bool, + include_headers: bool, +) -> Vec<u8> { + let mut obj = Map::new(); + + if include_metadata { + obj.insert("offset".to_string(), Value::Number(message.offset.into())); + let ts = timestamp_to_rfc3339(message.timestamp); + obj.insert("timestamp".to_string(), Value::String(ts)); + obj.insert( + "stream".to_string(), + Value::String(topic_metadata.stream.clone()), + ); + obj.insert( + "topic".to_string(), + Value::String(topic_metadata.topic.clone()), + ); + obj.insert( + "partition_id".to_string(), + Value::Number(messages_metadata.partition_id.into()), + ); + } + + if include_headers && let Some(headers) = &message.headers { + let mut headers_obj = Map::new(); + for (key, value) in headers { + headers_obj.insert(key.to_string(), Value::String(value.to_string())); + } + obj.insert("headers".to_string(), Value::Object(headers_obj)); + } + + let payload_value = payload_to_json_value(&message.payload); + obj.insert("payload".to_string(), payload_value); + + match serde_json::to_vec(&Value::Object(obj)) { + Ok(bytes) => bytes, + Err(e) => { + tracing::warn!( + "Failed to serialize message at offset {}: {e}", + message.offset + ); + Vec::new() + } + } +} + +fn format_raw_message(message: &ConsumedMessage) -> Vec<u8> { + message.payload.try_to_bytes().unwrap_or_default() +} + +fn payload_to_json_value(payload: &Payload) -> Value { + match payload { + Payload::Json(value) => { + let bytes = simd_json::to_vec(value).unwrap_or_default(); + serde_json::from_slice(&bytes).unwrap_or(Value::Null) + } + Payload::Text(text) => Value::String(text.clone()), + Payload::Raw(bytes) => match serde_json::from_slice(bytes) { + Ok(v) => v, + Err(_) => Value::String(base64_encode(bytes)), + }, + Payload::Proto(text) => Value::String(text.clone()), + Payload::FlatBuffer(bytes) => Value::String(base64_encode(bytes)), + } +} + +fn base64_encode(bytes: &[u8]) -> String { + use base64::Engine; + base64::engine::general_purpose::STANDARD.encode(bytes) +} + +fn timestamp_to_rfc3339(micros: u64) -> String { + let secs = (micros / 1_000_000) as i64; + let nanos = ((micros % 1_000_000) * 1_000) as u32; + DateTime::<Utc>::from_timestamp(secs, nanos) + .map(|dt| dt.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) + .unwrap_or_else(|| "1970-01-01T00:00:00Z".to_string()) +} + +pub fn finalize_buffer(data: &[Vec<u8>], format: OutputFormat) -> Vec<u8> { + match format { + OutputFormat::JsonLines => { + let mut result = Vec::new(); + for entry in data { + result.extend_from_slice(entry); + result.push(b'\n'); + } + result + } + OutputFormat::JsonArray => { + let entries: Vec<Value> = data + .iter() + .filter_map(|bytes| serde_json::from_slice(bytes).ok()) Review Comment: Pure byte concat now: `[` + comma-joined entries + `]`, no parse/reserialize -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
