apache / beam

Apache Beam is a unified programming model for Batch and Streaming data processing.
https://beam.apache.org/
Apache License 2.0
7.88k stars 4.26k forks source link

[Bug]: Using string in PartitionColumn throws error, tries to convert it to string #31419

Closed ssaurav-redhat closed 4 months ago

ssaurav-redhat commented 5 months ago

What happened?

Referencing JdbcIo's doc in the section for Parallel reading from a JDBC datasource, It mentions to use either of these types of column for paritioning Beam supports partitioned reading of all data from a table. Automatic partitioning is supported for a few data types: Long, [DateTime](https://static.javadoc.io/joda-time/joda-time/2.10.10/org/joda/time/DateTime.html?is-external=true), String.

But when I am passing a string field into it, it tries to convert it into Long. As a result the code fails. Can anyone help me with this?

Beam version -> 2.56.0 JdbcIo version -> 2.56.0

Code PCollection<LogTable> dbReadData = p.apply("ReadFromMySQL", JdbcIO.<LogTable>readWithPartitions() .withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create( "com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3307/exampledb") .withUsername("exampleuser") .withPassword("examplepass")) .withTable("logs_table") .withCoder(SerializableCoder.of(LogTable.class)) .withPartitionColumn("uuid") .withRowMapper(new JdbcIO.RowMapper<LogTable>() { @Override public LogTable mapRow(ResultSet resultSet) throws Exception { String uuid = resultSet.getString("uuid"); Timestamp time = resultSet.getTimestamp("time"); return new LogTable(uuid, time); } }));

table structure -> CREATE TABLElogs_table( uuidvarchar(255) NOT NULL, timetimestamp NOT NULL )

Runner used was Flink Runner

Issue Priority

Priority: 3 (minor)

Issue Components

liferoad commented 5 months ago

@Abacn is this expected?

albmargareugen commented 4 months ago

I'm facing this error using Spark Runner. The field id is also a String:

Exception in thread "main" java.lang.NullPointerException: readWithPartitions only supports the following types: [class java.lang.Long, class org.joda.time.DateTime] at org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:1010) at org.apache.beam.sdk.io.jdbc.JdbcIO$ReadWithPartitions.expand(JdbcIO.java:1355) at org.apache.beam.sdk.io.jdbc.JdbcIO$ReadWithPartitions.expand(JdbcIO.java:1189)

public static JdbcIO.ReadWithPartitions<TableDB, String> generateTableWithPartition(JdbcIO.DataSourceConfiguration ds) { return JdbcIO.<TableDB, String>readWithPartitions(TypeDescriptor.of(String.class)) .withDataSourceConfiguration(ds) .withTable("table") .withPartitionColumn("id") .withRowMapper(new RowMapperGenerator.CreateTableSource()); }

I had took a look into here and line 492 defines public static final Map<Class<?>, JdbcReadWithPartitionsHelper<?>> PRESET_HELPERS, which includes the types that the partition can actually handle. The only class types defined there are Long and DateTime, not the String...

However the documentation says explicitly that it can handle the type String in here at line 120!

Abacn commented 4 months ago

Read the code, as early as when this documentation was entered #15848, it never supported String type as partition type.

albmargareugen commented 4 months ago

Thanks Abacn! Sad it's not implemented, but at least now I know why.

Abacn commented 4 months ago

dup #27120