Reading with DataStream

Amoro supports reading data in Batch or Streaming mode through Java API.

Batch mode

Using Batch mode to read the full and incremental data in the FileStore.

  • Non-primary key tables support reading full data in batch mode, snapshot data with a specified snapshot-id or timestamp, and incremental data with a specified snapshot interval.
  • The primary key table temporarily only supports reading the current full amount and later CDC data.
  1. StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();
  2. InternalCatalogBuilder catalogBuilder =
  3. InternalCatalogBuilder
  4. .builder()
  5. .metastoreUrl("thrift://<url>:<port>/<catalog_name>");
  6. TableIdentifier tableId = TableIdentifier.of("catalog_name", "database_name", "test_table");
  7. AmoroTableLoader tableLoader = AmoroTableLoader.of(tableId, catalogBuilder);
  8. Map<String, String> properties = new HashMap<>();
  9. // Default is true.
  10. properties.put("streaming", "false");
  11. DataStream<RowData> batch =
  12. FlinkSource.forRowData()
  13. .env(env)
  14. .tableLoader(tableLoader)
  15. // The primary key table only supports reading the current full amount and later CDC data temporarily, without the properties parameter .
  16. .properties(properties)
  17. .build();
  18. // print All data read
  19. batch.print();
  20. // Submit and execute the task
  21. env.execute("Test Amoro Batch Read");

The map properties contain below keys, currently only valid for non-primary key tables:

Key Default Type Required Description
case-sensitive false Boolean No Case-sensitive
snapshot-id (none) Long No Read the full amount of data of the specified snapshot, only effective when streaming is false or not configured
as-of-timestamp (none) String No Read the last time less than the timestamp The full amount of snapshot data is valid only when streaming is false or not configured
start-snapshot-id (none) String No When streaming is false, end-snapshot-id needs to be used to read the two intervals Incremental data (snapshot1, snapshot2]. When streaming is true, read the incremental data after the snapshot, if not specified, read the incremental data after the current snapshot (not including the current one)
end-snapshot-id (none ) String No Need to cooperate with start-snapshot-id to read incremental data in two intervals (snapshot1, snapshot2]

Streaming mode

Amoro supports reading incremental data in FileStore or LogStore through Java API in Streaming mode

Streaming mode (LogStore)

  1. StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();
  2. InternalCatalogBuilder catalogBuilder =
  3. InternalCatalogBuilder
  4. .builder()
  5. .metastoreUrl("thrift://<url>:<port>/<catalog_name>");
  6. TableIdentifier tableId = TableIdentifier.of("catalog_name", "database_name", "test_table");
  7. AmoroTableLoader tableLoader = AmoroTableLoader.of(tableId, catalogBuilder);
  8. AmoroTable table = AmoroUtils.load(tableLoader);
  9. // Read table All fields. If you only read some fields, you can construct the schema yourself, for example:
  10. // Schema userSchema = new Schema(new ArrayList<Types.NestedField>() {{
  11. // add(Types.NestedField.optional(0, "f_boolean", Types.BooleanType.get()));
  12. // add(Types.NestedField.optional(1, "f_int", Types.IntegerType.get()));
  13. // }});
  14. Schema schema = table.schema();
  15. // -----------Hidden Kafka--------------
  16. LogKafkaSource source = LogKafkaSource.builder(schema, table.properties()).build();
  17. or
  18. // -----------Hidden Pulsar--------------
  19. LogPulsarSource source = LogPulsarSource.builder(schema, table.properties()).build();
  20. DataStream<RowData> stream = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Log Source");
  21. // Print all the read data
  22. stream.print();
  23. // Submit and execute the task
  24. env.execute("Test Amoro Stream Read");

Streaming mode (FileStore)

  1. StreamExecutionEnvironment env = ...;
  2. InternalCatalogBuilder catalogBuilder = ...;
  3. TableIdentifier tableId = ...;
  4. AmoroTableLoader tableLoader = ...;
  5. Map<String, String> properties = new HashMap<>();
  6. // default is true
  7. properties.put("streaming", "true");
  8. DataStream<RowData> stream =
  9. FlinkSource.forRowData()
  10. .env(env)
  11. .tableLoader(tableLoader)
  12. // The primary key table only supports reading the current full amount and later CDC data for the time being, without the properties parameter
  13. .properties(properties)
  14. .build();
  15. // Print All read data
  16. stream.print();
  17. // Submit and execute the task
  18. env.execute("Test Amoro Stream Read");
  19. StreamExecutionEnvironment env = ...;
  20. InternalCatalogBuilder catalogBuilder = ...;
  21. TableIdentifier tableId = ...;
  22. AmoroTableLoader tableLoader = ...;
  23. Map properties = new HashMap<>();
  24. // default is true properties.put("streaming", "true");
  25. DataStream stream =
  26. FlinkSource.forRowData()
  27. .env(env)
  28. .tableLoader(tableLoader)
  29. // The primary key table only supports reading the current full amount and later CDC data for the time being, without the properties parameter
  30. .properties(properties)
  31. .build();
  32. // print All read data
  33. stream.print();
  34. // Submit and execute the task
  35. env.execute("Test Amoro Stream Read");

DataStream API supports reading primary key tables and non-primary key tables. The configuration items supported by properties can refer to Querying With SQL chapter Hint Option

Writing with DataStream

Amoro table supports writing data to LogStore or FileStore through Java API

Overwrite data

Amoro table currently Only supports the existing data in the dynamic Overwrite table of the non-primary key table

  1. DataStream<RowData> input = ...;
  2. InternalCatalogBuilder catalogBuilder = ...;
  3. TableIdentifier tableId = ...;
  4. AmoroTableLoader tableLoader = ...;
  5. TableSchema FLINK_SCHEMA = TableSchema.builder()
  6. .field("id", DataTypes.INT())
  7. .field("name", DataTypes.STRING())
  8. .field("op_time", DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE())
  9. .build();
  10. FlinkSink
  11. .forRowData(input)
  12. .tableLoader(tableLoader)
  13. .overwrite(true)
  14. .flinkSchema(FLINK_SCHEMA)
  15. .build();
  16. // Submit and execute the task
  17. env.execute("Test Amoro Overwrite");
  18. DataStream input = ...; InternalCatalogBuilder catalogBuilder = ...; TableIdentifier tableId = ...; AmoroTableLoader tableLoader = ...; TableSchema FLINK_SCHEMA = TableSchema.builder() .field("id", DataTypes.INT()) .field ("name", DataTypes.STRING()) .field("op_time", DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE()) .build(); FlinkSink .forRowData(input) .tableLoader(tableLoader) .overwrite(true) .flinkSchema(FLINK_SCHEMA) .build(); // Submit and execute the task env.execute(“Test Amoro Overwrite”);

Appending data

For the Amoro table, it supports specifying to write data to FileStore or LogStore through Java API.

  1. DataStream<RowData> input = ...;
  2. InternalCatalogBuilder catalogBuilder = ...;
  3. TableIdentifier tableId = ...;
  4. AmoroTableLoader tableLoader = ...;
  5. TableSchema FLINK_SCHEMA = TableSchema.builder()
  6. .field("id", DataTypes.INT())
  7. .field("name", DataTypes.STRING())
  8. .field("op_time", DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE())
  9. .build();
  10. AmoroTable table = AmoroUtils.loadAmoroTable(tableLoader);
  11. table.properties().put("arctic.emit.mode", "log,file");
  12. FlinkSink
  13. .forRowData(input)
  14. .table(table)
  15. .tableLoader(tableLoader)
  16. .flinkSchema(FLINK_SCHEMA)
  17. .build();
  18. env.execute("Test Amoro Append");

The DataStream API supports writing to primary key tables and non-primary key tables. The configuration items supported by properties can refer to Writing With SQL chapter Hint Options

TIPS

arctic.emit.mode contains log, you need to configure log-store.enabled = true Enable Log Configuration

arctic.emit.mode When file is included, the primary key table will only be written to ChangeStore, and the non-primary key table will be directly written to BaseStore.