This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new c18f03075c92 CAMEL-20940: camel-parquet-avro - marshal accepts a 
Collection, an array, an Iterator or a single record (#30918)
c18f03075c92 is described below

commit c18f03075c929d66a54506d06fff9bdc0e92945a
Author: Urmila Unni <[email protected]>
AuthorDate: Fri Nov 27 14:47:56 2026 +0530

    CAMEL-20940: camel-parquet-avro - marshal accepts a Collection, an array, 
an Iterator or a single record (#30918)
    
    Marshal cast the body to a List, so any other body failed with a 
ClassCastException. Write the elements of an Iterable, an array or an Iterator, 
or the body itself as a single record, and update the body description.
    
    Review:
    - a clear error without a schema, Stream bodies, and the Marshal docs
    - close a Stream body after reading it
    
    Co-authored-by: Claude <[email protected]>
---
 .../camel/catalog/dataformats/parquetAvro.json     |   2 +-
 .../camel/catalog/docs/parquetAvro-dataformat.adoc |   2 +-
 .../camel/dataformat/parquet/avro/parquetAvro.json |   2 +-
 .../src/main/docs/parquetAvro-dataformat.adoc      |   2 +-
 .../parquet/avro/ParquetAvroDataFormat.java        |  52 +++++++--
 .../avro/ParquetAvroMarshalBodyTypesTest.java      | 117 +++++++++++++++++++++
 6 files changed, 167 insertions(+), 10 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dataformats/parquetAvro.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dataformats/parquetAvro.json
index 008c2efc3b87..fbb5a8d84c04 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dataformats/parquetAvro.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dataformats/parquetAvro.json
@@ -16,7 +16,7 @@
     "modelJavaType": "org.apache.camel.model.dataformat.ParquetAvroDataFormat"
   },
   "bodyTypes": {
-    "marshalIn": { "carriers": [ "list" ], "description": "A List of Avro 
GenericRecords, or of objects of the class unmarshalType names. A single record 
fails, and so does an empty List without unmarshalType" },
+    "marshalIn": { "carriers": [ "list", "iterator", "pojo" ], "description": 
"The records: a List, another Collection, an array, an Iterator or a Stream of 
Avro GenericRecords, or of objects of the class unmarshalType names, or a 
single such record. Without unmarshalType, the records must be GenericRecords, 
and an empty body fails" },
     "marshalOut": { "carriers": [ "stream" ], "format": "binary", 
"description": "The Parquet file in the stream cache, or a byte array when 
stream caching is off" },
     "unmarshalIn": { "carriers": [ "bytes", "stream", "file" ], "format": 
"binary", "description": "A Parquet file, which is read into memory" },
     "unmarshalOut": { "carriers": [ "list" ], "byOption": [ { "option": 
"lazyLoad", "value": "true", "carriers": [ "iterator" ] } ], "description": "A 
List (ArrayList) of the records: GenericRecords, or objects of the class 
unmarshalType names. With lazyLoad, an Iterator of them for the split EIP" }
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/parquetAvro-dataformat.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/parquetAvro-dataformat.adoc
index c2166b39675f..8f7c0d0f0cd1 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/parquetAvro-dataformat.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/parquetAvro-dataformat.adoc
@@ -58,7 +58,7 @@ With `unmarshalType`, the records are objects of that class. 
Without it, they ar
 
 == Marshal
 
-Marshalling is the reverse process of unmarshalling, so when you have a List 
of `GenericRecord` objects or POJOs and marshal it, you will get the 
parquet-formatted output stream on your producer endpoint. A single record that 
is not in a List fails.
+Marshalling is the reverse process of unmarshalling, so when you have a List 
of `GenericRecord` objects or POJOs and marshal it, you will get the 
parquet-formatted output stream on your producer endpoint. The body can also be 
another Collection, an array, an Iterator or a Stream of records, or a single 
record. Without `unmarshalType`, the records must be `GenericRecord` objects, 
as the schema of the Parquet file is taken from them, and an empty body fails.
 
 [tabs]
 ====
diff --git 
a/components/camel-parquet-avro/src/generated/resources/META-INF/org/apache/camel/dataformat/parquet/avro/parquetAvro.json
 
b/components/camel-parquet-avro/src/generated/resources/META-INF/org/apache/camel/dataformat/parquet/avro/parquetAvro.json
index 008c2efc3b87..fbb5a8d84c04 100644
--- 
a/components/camel-parquet-avro/src/generated/resources/META-INF/org/apache/camel/dataformat/parquet/avro/parquetAvro.json
+++ 
b/components/camel-parquet-avro/src/generated/resources/META-INF/org/apache/camel/dataformat/parquet/avro/parquetAvro.json
@@ -16,7 +16,7 @@
     "modelJavaType": "org.apache.camel.model.dataformat.ParquetAvroDataFormat"
   },
   "bodyTypes": {
-    "marshalIn": { "carriers": [ "list" ], "description": "A List of Avro 
GenericRecords, or of objects of the class unmarshalType names. A single record 
fails, and so does an empty List without unmarshalType" },
+    "marshalIn": { "carriers": [ "list", "iterator", "pojo" ], "description": 
"The records: a List, another Collection, an array, an Iterator or a Stream of 
Avro GenericRecords, or of objects of the class unmarshalType names, or a 
single such record. Without unmarshalType, the records must be GenericRecords, 
and an empty body fails" },
     "marshalOut": { "carriers": [ "stream" ], "format": "binary", 
"description": "The Parquet file in the stream cache, or a byte array when 
stream caching is off" },
     "unmarshalIn": { "carriers": [ "bytes", "stream", "file" ], "format": 
"binary", "description": "A Parquet file, which is read into memory" },
     "unmarshalOut": { "carriers": [ "list" ], "byOption": [ { "option": 
"lazyLoad", "value": "true", "carriers": [ "iterator" ] } ], "description": "A 
List (ArrayList) of the records: GenericRecords, or objects of the class 
unmarshalType names. With lazyLoad, an Iterator of them for the split EIP" }
diff --git 
a/components/camel-parquet-avro/src/main/docs/parquetAvro-dataformat.adoc 
b/components/camel-parquet-avro/src/main/docs/parquetAvro-dataformat.adoc
index c2166b39675f..8f7c0d0f0cd1 100644
--- a/components/camel-parquet-avro/src/main/docs/parquetAvro-dataformat.adoc
+++ b/components/camel-parquet-avro/src/main/docs/parquetAvro-dataformat.adoc
@@ -58,7 +58,7 @@ With `unmarshalType`, the records are objects of that class. 
Without it, they ar
 
 == Marshal
 
-Marshalling is the reverse process of unmarshalling, so when you have a List 
of `GenericRecord` objects or POJOs and marshal it, you will get the 
parquet-formatted output stream on your producer endpoint. A single record that 
is not in a List fails.
+Marshalling is the reverse process of unmarshalling, so when you have a List 
of `GenericRecord` objects or POJOs and marshal it, you will get the 
parquet-formatted output stream on your producer endpoint. The body can also be 
another Collection, an array, an Iterator or a Stream of records, or a single 
record. Without `unmarshalType`, the records must be `GenericRecord` objects, 
as the schema of the Parquet file is taken from them, and an empty body fails.
 
 [tabs]
 ====
diff --git 
a/components/camel-parquet-avro/src/main/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroDataFormat.java
 
b/components/camel-parquet-avro/src/main/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroDataFormat.java
index 51be46e22ffe..abf6abe6377d 100644
--- 
a/components/camel-parquet-avro/src/main/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroDataFormat.java
+++ 
b/components/camel-parquet-avro/src/main/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroDataFormat.java
@@ -19,8 +19,12 @@ package org.apache.camel.dataformat.parquet.avro;
 import java.io.BufferedOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
+import java.util.stream.Stream;
 
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema;
@@ -51,9 +55,11 @@ import static 
org.apache.parquet.hadoop.ParquetFileWriter.Mode.OVERWRITE;
 import static org.apache.parquet.hadoop.metadata.CompressionCodecName.GZIP;
 
 @Dataformat("parquetAvro")
-@BodyTypes(marshalIn = @BodyType(value = Carrier.LIST,
-                                 description = "A List of Avro GenericRecords, 
or of objects of the class unmarshalType names."
-                                               + " A single record fails, and 
so does an empty List without unmarshalType"),
+@BodyTypes(marshalIn = @BodyType(value = { Carrier.LIST, Carrier.ITERATOR, 
Carrier.POJO },
+                                 description = "The records: a List, another 
Collection, an array, an Iterator or a"
+                                               + " Stream of Avro 
GenericRecords, or of objects of the class"
+                                               + " unmarshalType names, or a 
single such record. Without unmarshalType,"
+                                               + " the records must be 
GenericRecords, and an empty body fails"),
            marshalOut = @BodyType(value = Carrier.STREAM, format = "binary",
                                   description = "The Parquet file in the 
stream cache, or a byte array when stream caching is"
                                                 + " off"),
@@ -92,7 +98,7 @@ public class ParquetAvroDataFormat extends ServiceSupport 
implements DataFormat,
                 DEFAULT_UUID_GENERATOR.generateUuid(),
                 parquetOutput);
 
-        List<?> list = (List<?>) graph;
+        List<?> records = toRecords(graph);
 
         Schema schema = null;
         GenericData model = null;
@@ -105,7 +111,14 @@ public class ParquetAvroDataFormat extends ServiceSupport 
implements DataFormat,
             }
         }
         if (schema == null) {
-            schema = GenericContainer.class.cast(list.get(0)).getSchema();
+            if (records.isEmpty() || !(records.get(0) instanceof 
GenericContainer first)) {
+                throw new IllegalArgumentException(
+                        "parquetAvro marshal needs the unmarshalType option, 
or Avro GenericRecords as the records, to"
+                                                   + " know the schema; the 
body has "
+                                                   + (records.isEmpty()
+                                                           ? "no records" : "a 
" + records.get(0).getClass().getName()));
+            }
+            schema = first.getSchema();
             model = GenericData.get();
         }
 
@@ -116,12 +129,39 @@ public class ParquetAvroDataFormat extends ServiceSupport 
implements DataFormat,
                 .withCompressionCodec(compressionCodecName)
                 .withWriteMode(OVERWRITE)
                 .build()) {
-            for (Object grapElem : list) {
+            for (Object grapElem : records) {
                 writer.write(grapElem);
             }
         }
     }
 
+    /**
+     * The records to write: a List as is, the elements of another Iterable 
(such as a Set), of an array, of an Iterator
+     * or of a Stream, or the body itself as a single record. A Path is a 
single value, although it is an Iterable of
+     * its names.
+     */
+    private static List<?> toRecords(Object graph) {
+        if (graph instanceof List<?> list) {
+            return list;
+        }
+        List<Object> records = new ArrayList<>();
+        if (graph instanceof Iterable<?> iterable && !(graph instanceof Path)) 
{
+            iterable.forEach(records::add);
+        } else if (graph instanceof Object[] array) {
+            Collections.addAll(records, array);
+        } else if (graph instanceof Iterator<?> iterator) {
+            iterator.forEachRemaining(records::add);
+        } else if (graph instanceof Stream<?> stream) {
+            // a Stream can hold a resource, such as the lines of a file
+            try (stream) {
+                stream.forEach(records::add);
+            }
+        } else if (graph != null) {
+            records.add(graph);
+        }
+        return records;
+    }
+
     @Override
     public Object unmarshal(Exchange exchange, InputStream stream) throws 
Exception {
         // unmarshal from the input stream of parquet-avro to Java object or 
GenericRecord (graph)
diff --git 
a/components/camel-parquet-avro/src/test/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroMarshalBodyTypesTest.java
 
b/components/camel-parquet-avro/src/test/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroMarshalBodyTypesTest.java
new file mode 100644
index 000000000000..fc87daf6beaa
--- /dev/null
+++ 
b/components/camel-parquet-avro/src/test/java/org/apache/camel/dataformat/parquet/avro/ParquetAvroMarshalBodyTypesTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.
+ */
+package org.apache.camel.dataformat.parquet.avro;
+
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Stream;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Marshal accepts other bodies than a List: a single record, an array, 
another Collection, an Iterator and a Stream.
+ * Without unmarshalType, it needs Avro GenericRecords to know the schema.
+ */
+class ParquetAvroMarshalBodyTypesTest extends CamelTestSupport {
+
+    private static final Pojo AIRPORT = new Pojo(1, "airport");
+    private static final Pojo PENGUIN = new Pojo(2, "penguin");
+
+    @Test
+    void singleRecord() {
+        assertEquals(List.of(AIRPORT), roundTrip(AIRPORT));
+    }
+
+    @Test
+    void array() {
+        assertEquals(List.of(AIRPORT, PENGUIN), roundTrip(new Pojo[] { 
AIRPORT, PENGUIN }));
+    }
+
+    @Test
+    void collectionThatIsNotAList() {
+        assertEquals(List.of(AIRPORT, PENGUIN), roundTrip(new 
LinkedHashSet<>(List.of(AIRPORT, PENGUIN))));
+    }
+
+    @Test
+    void iterator() {
+        assertEquals(List.of(AIRPORT, PENGUIN), roundTrip(List.of(AIRPORT, 
PENGUIN).iterator()));
+    }
+
+    @Test
+    void stream() {
+        assertEquals(List.of(AIRPORT, PENGUIN), roundTrip(Stream.of(AIRPORT, 
PENGUIN)));
+    }
+
+    @Test
+    void emptyBodyWithoutUnmarshalTypeFails() {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody("direct:noUnmarshalType", 
List.of()));
+        IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertTrue(cause.getMessage().contains("no records"), 
cause.getMessage());
+    }
+
+    @Test
+    void pojoWithoutUnmarshalTypeFails() {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody("direct:noUnmarshalType", 
List.of(AIRPORT)));
+        IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertTrue(cause.getMessage().contains(Pojo.class.getName()), 
cause.getMessage());
+    }
+
+    @Test
+    void streamIsClosed() {
+        AtomicBoolean closed = new AtomicBoolean();
+        roundTrip(Stream.of(AIRPORT, PENGUIN).onClose(() -> closed.set(true)));
+        assertTrue(closed.get());
+    }
+
+    @Test
+    void nullBodyWithoutUnmarshalTypeFails() {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody("direct:noUnmarshalType", (Object) 
null));
+        IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertTrue(cause.getMessage().contains("no records"), 
cause.getMessage());
+    }
+
+    private List<?> roundTrip(Object body) {
+        Object marshalled = template.requestBody("direct:in", body);
+        return template.requestBody("direct:back", marshalled, List.class);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                ParquetAvroDataFormat format = new ParquetAvroDataFormat();
+                format.setUnmarshalType(Pojo.class);
+                from("direct:in").marshal(format);
+                from("direct:back").unmarshal(format);
+                from("direct:noUnmarshalType").marshal(new 
ParquetAvroDataFormat());
+            }
+        };
+    }
+}

Reply via email to