Json data cannot be fetched in flink when reading data from postgresql

血红的双手。 提交于 2020-01-06 06:05:55

问题


I was trying to fetch data from postgre using flink. The following is the code:

dbData =env.createInput(JDBCInputFormat.buildJDBCInputFormat()
.setDrivername(Utils.properties_fetch("drivername"))
.setDBUrl(Utils.properties_fetch("dbURL"))
.setUsername(Utils.properties_fetch("username"))
.setPassword(Utils.properties_fetch("password"))
.setQuery(sourcequery)
.setRowTypeInfo(newRowTypeInfo(BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.DATE_TYPE_INFO,BasicTypeInfo.DATE_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO))
.finish());

The third BasicTypeInfo.STRING_TYPE_INFO is fetching a jsonb data type from postgre.

I get the following error:

06/28/2018 14:02:09 Job execution switched to status FAILING.
java.lang.ClassCastException: org.postgresql.util.PGobject cannot be 
cast to java.lang.String at 
org.apache.flink.api.common.typeutils.base.StringSerializer
.serialize(StringSerializer.java:28) at 
org.apache.flink.api.java.typeutils.runtime
.RowSerializer.serialize(RowSerializer.java:160) at 
org.apache.flink.api.java.typeutils.runtime.RowSerializer
.serialize(RowSerializer.java:46) at 
org.apache.flink.runtime.plugable.SerializationDelegate
.write(SerializationDelegate.java:54) at 
org.apache.flink.runtime.io.network.api.serialization
.SpanningRecordSerializer.addRecord(SpanningRecordSerializer
.java:93) at 
org.apache.flink.runtime.io.network.api.writer
.RecordWriter.sendToTarget(RecordWriter.java:114) at 
org.apache.flink.runtime.io.network.api.writer
.RecordWriter.emit(RecordWriter.java:89) at 
org.apache.flink.runtime.operators.shipping.OutputCollector
.collect(OutputCollector.java:65) at 
org.apache.flink.runtime.operators.util.metrics
.CountingCollector.collect(CountingCollector.java:35) at 
org.apache.flink.runtime.operators.DataSourceTask
.invoke(DataSourceTask.java:168)
at org.apache.flink.runtime.taskmanager.Task.run(Task.java:718)
at java.lang.Thread.run(Thread.java:748)

回答1:


It seems like one of the field returned from your query is a PGobject, where flink expected a string.

You can change the BasicTypeInfo.STRING_TYPE_INFO for this field to TypeInformation.of(PGobject.class)

Later you can add a map function to call PGobject#value to get the underlying string value of this field



来源:https://stackoverflow.com/questions/51078537/json-data-cannot-be-fetched-in-flink-when-reading-data-from-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!