Flink API

We do not recommend using programming API. Paimon is designed for SQL first, unless you are a professional Flink developer, even if you do, it can be very difficult.

We strongly recommend that you use Flink SQL or Spark SQL, or simply use SQL APIs in programs.

The following documents are not detailed and are for reference only.

Dependency

Maven dependency:

  1. <dependency>
  2. <groupId>org.apache.paimon</groupId>
  3. <artifactId>paimon-flink-1.17</artifactId>
  4. <version>0.8.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.flink</groupId>
  8. <artifactId>flink-table-api-java-bridge</artifactId>
  9. <version>1.17.0</version>
  10. <scope>provided</scope>
  11. </dependency>

Or download the jar file: Paimon Flink.

Please choose your Flink version.

Paimon relies on Hadoop environment, you should add hadoop classpath or bundled jar.

Not only DataStream API, you can also read or write to Paimon tables by the conversion between DataStream and Table in Flink. See DataStream API Integration.

Write to Table

  1. import org.apache.paimon.catalog.Catalog;
  2. import org.apache.paimon.catalog.Identifier;
  3. import org.apache.paimon.flink.sink.FlinkSinkBuilder;
  4. import org.apache.paimon.options.Options;
  5. import org.apache.paimon.table.Table;
  6. import org.apache.flink.api.common.typeinfo.Types;
  7. import org.apache.flink.streaming.api.datastream.DataStream;
  8. import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
  9. import org.apache.flink.table.api.DataTypes;
  10. import org.apache.flink.table.types.DataType;
  11. import org.apache.flink.types.Row;
  12. import org.apache.flink.types.RowKind;
  13. public class WriteToTable {
  14. public static void writeTo() throws Exception {
  15. // create environments of both APIs
  16. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  17. // for CONTINUOUS_UNBOUNDED source, set checkpoint interval
  18. // env.enableCheckpointing(60_000);
  19. // create a changelog DataStream
  20. DataStream<Row> input =
  21. env.fromElements(
  22. Row.ofKind(RowKind.INSERT, "Alice", 12),
  23. Row.ofKind(RowKind.INSERT, "Bob", 5),
  24. Row.ofKind(RowKind.UPDATE_BEFORE, "Alice", 12),
  25. Row.ofKind(RowKind.UPDATE_AFTER, "Alice", 100))
  26. .returns(
  27. Types.ROW_NAMED(
  28. new String[] {"name", "age"}, Types.STRING, Types.INT));
  29. // get table from catalog
  30. Options catalogOptions = new Options();
  31. catalogOptions.set("warehouse", "/path/to/warehouse");
  32. Catalog catalog = FlinkCatalogFactory.createPaimonCatalog(catalogOptions);
  33. Table table = catalog.getTable(Identifier.create("my_db", "T"));
  34. DataType inputType =
  35. DataTypes.ROW(
  36. DataTypes.FIELD("name", DataTypes.STRING()),
  37. DataTypes.FIELD("age", DataTypes.INT()));
  38. FlinkSinkBuilder builder = new FlinkSinkBuilder(table).forRow(input, inputType);
  39. // set sink parallelism
  40. // builder.parallelism(_your_parallelism)
  41. // set overwrite mode
  42. // builder.overwrite(...)
  43. builder.build();
  44. env.execute();
  45. }
  46. }

Read from Table

  1. import org.apache.paimon.catalog.Catalog;
  2. import org.apache.paimon.catalog.Identifier;
  3. import org.apache.paimon.flink.source.FlinkSourceBuilder;
  4. import org.apache.paimon.options.Options;
  5. import org.apache.paimon.table.Table;
  6. import org.apache.flink.streaming.api.datastream.DataStream;
  7. import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
  8. import org.apache.flink.types.Row;
  9. public class ReadFromTable {
  10. public static void readFrom() throws Exception {
  11. // create environments of both APIs
  12. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  13. // get table from catalog
  14. Options catalogOptions = new Options();
  15. catalogOptions.set("warehouse", "/path/to/warehouse");
  16. Catalog catalog = FlinkCatalogFactory.createPaimonCatalog(catalogOptions);
  17. Table table = catalog.getTable(Identifier.create("my_db", "T"));
  18. // table = table.copy(Collections.singletonMap("scan.file-creation-time-millis", "..."));
  19. FlinkSourceBuilder builder = new FlinkSourceBuilder(table).env(env);
  20. // builder.sourceBounded(true);
  21. // builder.projection(...);
  22. // builder.predicate(...);
  23. // builder.limit(...);
  24. // builder.sourceParallelism(...);
  25. DataStream<Row> dataStream = builder.buildForRow();
  26. // use this datastream
  27. dataStream.executeAndCollect().forEachRemaining(System.out::println);
  28. // prints:
  29. // +I[Bob, 12]
  30. // +I[Alice, 12]
  31. // -U[Alice, 12]
  32. // +U[Alice, 14]
  33. }
  34. }

Cdc ingestion Table

Paimon supports ingest data into Paimon tables with schema evolution.

  • You can use Java API to write cdc records into Paimon Tables.
  • You can write records to Paimon’s partial-update table with adding columns dynamically.

Here is an example to use RichCdcSinkBuilder API:

  1. import org.apache.paimon.catalog.Catalog;
  2. import org.apache.paimon.catalog.CatalogContext;
  3. import org.apache.paimon.catalog.CatalogFactory;
  4. import org.apache.paimon.catalog.Identifier;
  5. import org.apache.paimon.flink.sink.cdc.RichCdcRecord;
  6. import org.apache.paimon.flink.sink.cdc.RichCdcSinkBuilder;
  7. import org.apache.paimon.fs.Path;
  8. import org.apache.paimon.options.Options;
  9. import org.apache.paimon.schema.Schema;
  10. import org.apache.paimon.table.Table;
  11. import org.apache.paimon.types.DataTypes;
  12. import org.apache.flink.streaming.api.datastream.DataStream;
  13. import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
  14. import static org.apache.paimon.types.RowKind.INSERT;
  15. public class WriteCdcToTable {
  16. public static void writeTo() throws Exception {
  17. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  18. // for CONTINUOUS_UNBOUNDED source, set checkpoint interval
  19. // env.enableCheckpointing(60_000);
  20. DataStream<RichCdcRecord> dataStream =
  21. env.fromElements(
  22. RichCdcRecord.builder(INSERT)
  23. .field("order_id", DataTypes.BIGINT(), "123")
  24. .field("price", DataTypes.DOUBLE(), "62.2")
  25. .build(),
  26. // dt field will be added with schema evolution
  27. RichCdcRecord.builder(INSERT)
  28. .field("order_id", DataTypes.BIGINT(), "245")
  29. .field("price", DataTypes.DOUBLE(), "82.1")
  30. .field("dt", DataTypes.TIMESTAMP(), "2023-06-12 20:21:12")
  31. .build());
  32. Identifier identifier = Identifier.create("my_db", "T");
  33. Options catalogOptions = new Options();
  34. catalogOptions.set("warehouse", "/path/to/warehouse");
  35. Catalog.Loader catalogLoader =
  36. () -> FlinkCatalogFactory.createPaimonCatalog(catalogOptions);
  37. Table table = catalogLoader.load().getTable(identifier);
  38. new RichCdcSinkBuilder(table)
  39. .forRichCdcRecord(dataStream)
  40. .identifier(identifier)
  41. .catalogLoader(catalogLoader)
  42. .build();
  43. env.execute();
  44. }
  45. }