You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@beam.apache.org by Matthew Ouyang <ma...@gmail.com> on 2021/12/17 22:41:10 UTC

Does JavaBeanUtils.getGetters work with List?
I needed to convert a POJO to a Row for a unit test(sample code below).
Building a Schema from a POJO seems fine, but setting up the values seems
to not accept a List of objects.  To provide more detail, fieldValues
outputs a List<MyField> instead of a List<Row>.  The third line in the
sample is the same line used by the method  JavaBeanSchema.fieldValueGetters.
Is there a pre-existing method that will do this conversion, or do
JavaBeanUtils.getGetters need to be modified to work on List<Object>?  I've
seen now-deleted code in a class called RowFactory but even that appears to
have the same issue of not supporting List<Object>.  I suppose I could set
up a test pipeline of sorts but I'd prefer to avoid doing all of that setup
if I can.

public class MyTable {
    public List<MyField> getFields() {}
}

MyTable myTable = new MyTable();
FieldValueTypeSupplier fieldValueTypeSupplier =
JavaBeanSchema.GetterTypeSupplier.INSTANCE;
Schema schema = JavaBeanUtils.schemaFromJavaBeanClass(myTable.getClass(),
fieldValueTypeSupplier);
List<FieldValueGetter> fieldValueGetters =
JavaBeanUtils.getGetters(pojo.getClass(), schema, fieldValueTypeSupplier,
new ByteBuddyUtils.DefaultTypeConversionsFactory());
List<Object> fieldValues = fieldValueGetters.stream().map(g ->
g.get(myTable)).collect(Collectors.toList());
return Row.withSchema(schema).addValues(fieldValues).build();

java.lang.ClassCastException: MyField cannot be cast to
org.apache.beam.sdk.values.Row ...

Re: Does JavaBeanUtils.getGetters work with List? Posted by Reuven Lax <re...@google.com>.
Generally you shouldn't need to use JavaBeanUtils directly. Can you explain
your use case, and maybe provide the surrounding code?

On Fri, Dec 17, 2021 at 2:41 PM Matthew Ouyang <ma...@gmail.com>
wrote:

> I needed to convert a POJO to a Row for a unit test(sample code below).
> Building a Schema from a POJO seems fine, but setting up the values seems
> to not accept a List of objects.  To provide more detail, fieldValues
> outputs a List<MyField> instead of a List<Row>.  The third line in the
> sample is the same line used by the method
> JavaBeanSchema.fieldValueGetters.  Is there a pre-existing method that
> will do this conversion, or do JavaBeanUtils.getGetters need to be
> modified to work on List<Object>?  I've seen now-deleted code in a class
> called RowFactory but even that appears to have the same issue of not
> supporting List<Object>.  I suppose I could set up a test pipeline of
> sorts but I'd prefer to avoid doing all of that setup if I can.
>
> public class MyTable {
>     public List<MyField> getFields() {}
> }
>
> MyTable myTable = new MyTable();
> FieldValueTypeSupplier fieldValueTypeSupplier =
> JavaBeanSchema.GetterTypeSupplier.INSTANCE;
> Schema schema = JavaBeanUtils.schemaFromJavaBeanClass(myTable.getClass(),
> fieldValueTypeSupplier);
> List<FieldValueGetter> fieldValueGetters =
> JavaBeanUtils.getGetters(pojo.getClass(), schema, fieldValueTypeSupplier,
> new ByteBuddyUtils.DefaultTypeConversionsFactory());
> List<Object> fieldValues = fieldValueGetters.stream().map(g ->
> g.get(myTable)).collect(Collectors.toList());
> return Row.withSchema(schema).addValues(fieldValues).build();
>
> java.lang.ClassCastException: MyField cannot be cast to
> org.apache.beam.sdk.values.Row ...
>