Class SingleStoreIO
- java.lang.Object
-
- org.apache.beam.sdk.io.singlestore.SingleStoreIO
-
public class SingleStoreIO extends java.lang.ObjectIO to read and write data on SingleStoreDB.Reading from SingleStoreDB datasource
SingleStoreIO source returns a bounded collection of
Tas aPCollection<T>. T is the type returned by the providedSingleStoreIO.RowMapper.To configure the SingleStoreDB source, you have to provide a
SingleStoreIO.DataSourceConfigurationusingSingleStoreIO.DataSourceConfiguration.create(String)(endpoint). Optionally,SingleStoreIO.DataSourceConfiguration.withUsername(String)andSingleStoreIO.DataSourceConfiguration.withPassword(String)allows you to define username and password.For example:
pipeline.apply(SingleStoreIO.<KV<Integer, String>>read() .withDataSourceConfiguration(DataSourceConfiguration.create("hostname:3306") .withUsername("username") .withPassword("password")) .withQuery("select id, name from Person") .withRowMapper(new RowMapper<KV<Integer, String>>() { public KV<Integer, String> mapRow(ResultSet resultSet) throws Exception { return KV.of(resultSet.getInt(1), resultSet.getString(2)); } }) );Query parameters can be configured using a user-provided
SingleStoreIO.StatementPreparator. For example:pipeline.apply(SingleStoreIO.<KV<Integer, String>>read() .withDataSourceConfiguration(DataSourceConfiguration.create("hostname:3306")) .withQuery("select id,name from Person where name = ?") .withStatementPreparator(new StatementPreparator() { public void setParameters(PreparedStatement preparedStatement) throws Exception { preparedStatement.setString(1, "Darwin"); } }) .withRowMapper(new RowMapper<KV<Integer, String>>() { public KV<Integer, String> mapRow(ResultSet resultSet) throws Exception { return KV.of(resultSet.getInt(1), resultSet.getString(2)); } }) );Parallel reading from a SingleStoreDB datasource
SingleStoreIO supports partitioned reading of all data from a table. To enable this, use
readWithPartitions(). This way of data reading is preferred because of the performance reasons.The following example shows usage of
readWithPartitions()pipeline.apply(SingleStoreIO.<Row>readWithPartitions() .withDataSourceConfiguration(DataSourceConfiguration.create("hostname:3306") .withUsername("username") .withPassword("password")) .withTable("Person") .withRowMapper(new RowMapper<KV<Integer, String>>() { public KV<Integer, String> mapRow(ResultSet resultSet) throws Exception { return KV.of(resultSet.getInt(1), resultSet.getString(2)); } }) );Writing to SingleStoreDB datasource
SingleStoreIO supports writing records into a database. It writes a
PCollectionto the database by converting data to CSV and sending it to the database with LOAD DATA query.Like the source, to configure the sink, you have to provide a
SingleStoreIO.DataSourceConfiguration.{@code pipeline .apply(...) .apply(SingleStoreIO.>write() .withDataSourceConfiguration(DataSourceConfiguration.create("hostname:3306") .withUsername("username") .withPassword("password")) .withStatement("insert into Person values(?, ?)") .withUserDataMapper(new UserDataMapper >() {
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSingleStoreIO.DataSourceConfigurationA POJO describing a SingleStoreDBDataSourceby providing all properties needed to create it.static classSingleStoreIO.Read<T>APTransformfor reading data from SingleStoreDB.static classSingleStoreIO.ReadWithPartitions<T>APTransformfor reading data from SingleStoreDB.static interfaceSingleStoreIO.RowMapper<T>An interface used bySingleStoreIO.ReadandSingleStoreIO.ReadWithPartitionsfor converting each row of theResultSetinto an element of the resultingPCollection.static interfaceSingleStoreIO.RowMapperWithCoder<T>A RowMapper that provides a Coder for resulting PCollection.static interfaceSingleStoreIO.RowMapperWithInit<T>A RowMapper that requires initialization.static interfaceSingleStoreIO.StatementPreparatorAn interface used by the SingleStoreIOSingleStoreIO.Readto set the parameters of thePreparedStatement.static interfaceSingleStoreIO.UserDataMapper<T>An interface used by the SingleStoreIOSingleStoreIO.Writeto map a data from each element ofPCollectionto a List of Strings.static classSingleStoreIO.Write<T>APTransformfor writing data to SingleStoreDB.
-
Constructor Summary
Constructors Constructor Description SingleStoreIO()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> SingleStoreIO.Read<T>read()Read data from a SingleStoreDB datasource.static SingleStoreIO.Read<org.apache.beam.sdk.values.Row>readRows()Read BeamRows from a SingleStoreDB datasource.static <T> SingleStoreIO.ReadWithPartitions<T>readWithPartitions()Likeread(), but executes multiple instances of the query on the same table for each database partition.static SingleStoreIO.ReadWithPartitions<org.apache.beam.sdk.values.Row>readWithPartitionsRows()LikereadRows(), but executes multiple instances of the query on the same table for each database partition.static <T> SingleStoreIO.Write<T>write()Write data to a SingleStoreDB datasource.static SingleStoreIO.Write<org.apache.beam.sdk.values.Row>writeRows()Write BeamRows to a SingleStoreDB datasource.
-
-
-
Method Detail
-
read
public static <T> SingleStoreIO.Read<T> read()
Read data from a SingleStoreDB datasource.- Type Parameters:
T- Type of the data to be read.
-
readRows
public static SingleStoreIO.Read<org.apache.beam.sdk.values.Row> readRows()
Read BeamRows from a SingleStoreDB datasource.
-
readWithPartitions
public static <T> SingleStoreIO.ReadWithPartitions<T> readWithPartitions()
Likeread(), but executes multiple instances of the query on the same table for each database partition.- Type Parameters:
T- Type of the data to be read.
-
readWithPartitionsRows
public static SingleStoreIO.ReadWithPartitions<org.apache.beam.sdk.values.Row> readWithPartitionsRows()
LikereadRows(), but executes multiple instances of the query on the same table for each database partition.
-
write
public static <T> SingleStoreIO.Write<T> write()
Write data to a SingleStoreDB datasource.- Type Parameters:
T- Type of the data to be written.
-
writeRows
public static SingleStoreIO.Write<org.apache.beam.sdk.values.Row> writeRows()
Write BeamRows to a SingleStoreDB datasource.
-
-