public class JDBCInputFormat
extends org.apache.flink.api.common.io.RichInputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>
implements org.apache.flink.api.java.typeutils.ResultTypeQueryable
TypeInformation>[] fieldTypes = new TypeInformation>[] {
BasicTypeInfo.INT_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.DOUBLE_TYPE_INFO,
BasicTypeInfo.INT_TYPE_INFO
};
RowTypeInfo rowTypeInfo = new RowTypeInfo(fieldTypes);
JDBCInputFormat jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
.setDrivername("org.apache.derby.jdbc.EmbeddedDriver")
.setDBUrl("jdbc:derby:memory:ebookshop")
.setQuery("select * from books")
.setRowTypeInfo(rowTypeInfo)
.finish();
In order to query the JDBC source in parallel, you need to provide a
parameterized query template (i.e. a valid PreparedStatement) and
a ParameterValuesProvider which provides binding values for the
query parameters. E.g.:
Serializable[][] queryParameters = new String[2][1];
queryParameters[0] = new String[]{"Kumar"};
queryParameters[1] = new String[]{"Tan Ah Teck"};
JDBCInputFormat jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
.setDrivername("org.apache.derby.jdbc.EmbeddedDriver")
.setDBUrl("jdbc:derby:memory:ebookshop")
.setQuery("select * from books WHERE author = ?")
.setRowTypeInfo(rowTypeInfo)
.setParametersProvider(new GenericParameterValuesProvider(queryParameters))
.finish();
Row,
ParameterValuesProvider,
PreparedStatement,
DriverManager,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
static class |
JDBCInputFormat.JDBCInputFormatBuilder |
| Constructor and Description |
|---|
JDBCInputFormat() |
| Modifier and Type | Method and Description |
|---|---|
static JDBCInputFormat.JDBCInputFormatBuilder |
buildJDBCInputFormat()
A builder used to set parameters to the output format's configuration in a fluent way.
|
void |
close()
Closes all resources used.
|
void |
closeInputFormat() |
void |
configure(org.apache.flink.configuration.Configuration parameters) |
org.apache.flink.core.io.InputSplit[] |
createInputSplits(int minNumSplits) |
org.apache.flink.core.io.InputSplitAssigner |
getInputSplitAssigner(org.apache.flink.core.io.InputSplit[] inputSplits) |
org.apache.flink.api.table.typeutils.RowTypeInfo |
getProducedType() |
org.apache.flink.api.common.io.statistics.BaseStatistics |
getStatistics(org.apache.flink.api.common.io.statistics.BaseStatistics cachedStatistics) |
org.apache.flink.api.table.Row |
nextRecord(org.apache.flink.api.table.Row row)
Stores the next resultSet row in a tuple
|
void |
open(org.apache.flink.core.io.InputSplit inputSplit)
Connects to the source database and executes the query in a parallel
fashion if
this
InputFormat is built using a parameterized query (i.e. |
void |
openInputFormat() |
boolean |
reachedEnd()
Checks whether all data has been read.
|
public org.apache.flink.api.table.typeutils.RowTypeInfo getProducedType()
getProducedType in interface org.apache.flink.api.java.typeutils.ResultTypeQueryablepublic void configure(org.apache.flink.configuration.Configuration parameters)
configure in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>public void openInputFormat()
openInputFormat in class org.apache.flink.api.common.io.RichInputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>public void closeInputFormat()
closeInputFormat in class org.apache.flink.api.common.io.RichInputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>public void open(org.apache.flink.core.io.InputSplit inputSplit)
throws IOException
InputFormat is built using a parameterized query (i.e. using
a PreparedStatement)
and a proper ParameterValuesProvider, in a non-parallel
fashion otherwise.open in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>inputSplit - which is ignored if this InputFormat is executed as a
non-parallel source,
a "hook" to the query parameters otherwise (using its
splitNumber)IOException - if there's an error during the execution of the querypublic void close()
throws IOException
close in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>IOException - Indicates that a resource could not be closed.public boolean reachedEnd()
throws IOException
reachedEnd in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>IOExceptionpublic org.apache.flink.api.table.Row nextRecord(org.apache.flink.api.table.Row row)
throws IOException
nextRecord in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>row - row to be reused.RowIOExceptionpublic org.apache.flink.api.common.io.statistics.BaseStatistics getStatistics(org.apache.flink.api.common.io.statistics.BaseStatistics cachedStatistics)
throws IOException
getStatistics in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>IOExceptionpublic org.apache.flink.core.io.InputSplit[] createInputSplits(int minNumSplits)
throws IOException
createInputSplits in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>createInputSplits in interface org.apache.flink.core.io.InputSplitSource<org.apache.flink.core.io.InputSplit>IOExceptionpublic org.apache.flink.core.io.InputSplitAssigner getInputSplitAssigner(org.apache.flink.core.io.InputSplit[] inputSplits)
getInputSplitAssigner in interface org.apache.flink.api.common.io.InputFormat<org.apache.flink.api.table.Row,org.apache.flink.core.io.InputSplit>getInputSplitAssigner in interface org.apache.flink.core.io.InputSplitSource<org.apache.flink.core.io.InputSplit>public static JDBCInputFormat.JDBCInputFormatBuilder buildJDBCInputFormat()
Copyright © 2014–2016 The Apache Software Foundation. All rights reserved.