It’s possible to read an iceberg table either from an hdfs path or from a hive table. It’s also possible to use a custom metastore in place of hive. The steps to do that are as follows.

Custom table operations implementation

Extend BaseMetastoreTableOperations to provide implementation on how to read and write metadata

Example:

  1. class CustomTableOperations extends BaseMetastoreTableOperations {
  2. private String dbName;
  3. private String tableName;
  4. private Configuration conf;
  5. private FileIO fileIO;
  6. protected CustomTableOperations(Configuration conf, String dbName, String tableName) {
  7. this.conf = conf;
  8. this.dbName = dbName;
  9. this.tableName = tableName;
  10. }
  11. // The doRefresh method should provide implementation on how to get the metadata location
  12. @Override
  13. public void doRefresh() {
  14. // Example custom service which returns the metadata location given a dbName and tableName
  15. String metadataLocation = CustomService.getMetadataForTable(conf, dbName, tableName);
  16. // When updating from a metadata file location, call the helper method
  17. refreshFromMetadataLocation(metadataLocation);
  18. }
  19. // The doCommit method should provide implementation on how to update with metadata location atomically
  20. @Override
  21. public void doCommit(TableMetadata base, TableMetadata metadata) {
  22. String oldMetadataLocation = base.location();
  23. // Write new metadata using helper method
  24. String newMetadataLocation = writeNewMetadata(metadata, currentVersion() + 1);
  25. // Example custom service which updates the metadata location for the given db and table atomically
  26. CustomService.updateMetadataLocation(dbName, tableName, oldMetadataLocation, newMetadataLocation);
  27. }
  28. // The io method provides a FileIO which is used to read and write the table metadata files
  29. @Override
  30. public FileIO io() {
  31. "%s/%s.db/%s", tableLocation,
  32. tableIdentifier.namespace().levels()[0],
  33. tableIdentifier.name());
  34. }
  35. @Override
  36. public boolean dropTable(TableIdentifier identifier, boolean purge) {
  37. // Example service to delete table
  38. CustomService.deleteTable(identifier.namespace().level(0), identifier.name());
  39. }
  40. @Override
  41. public void renameTable(TableIdentifier from, TableIdentifier to) {
  42. Preconditions.checkArgument(from.namespace().level(0).equals(to.namespace().level(0)),
  43. "Cannot move table between databases");
  44. // Example service to rename table
  45. CustomService.renameTable(from.namespace().level(0), from.name(), to.name());
  46. }
  47. // implement this method to read catalog name and properties during initialization
  48. public void initialize(String name, Map<String, String> properties) {
  49. }
  50. }

Catalog implementations can be dynamically loaded in most compute engines. For Spark and Flink, you can specify the catalog-impl catalog property to load it. Read the Configuration section for more details. For MapReduce, implement org.apache.iceberg.mr.CatalogLoader and set Hadoop property iceberg.mr.catalog.loader.class to load it. If your catalog must read Hadoop configuration to access certain environment properties, make your catalog implement org.apache.hadoop.conf.Configurable.

Custom file IO implementation

Extend FileIO and provide implementation to read and write data files

Example:

  1. public class CustomFileIO implements FileIO {
  2. // must have a no-arg constructor to be dynamically loaded
  3. // initialize(Map<String, String> properties) will be called to complete initialization
  4. public CustomFileIO() {
  5. }
  6. @Override
  7. public InputFile newInputFile(String s) {
  8. // you also need to implement the InputFile interface for a custom input file
  9. return new CustomInputFile(s);
  10. }
  11. @Override
  12. public OutputFile newOutputFile(String s) {
  13. // you also need to implement the OutputFile interface for a custom output file
  14. return new CustomOutputFile(s);
  15. }
  16. @Override
  17. public void deleteFile(String path) {
  18. Path toDelete = new Path(path);
  19. FileSystem fs = Util.getFs(toDelete);
  20. try {
  21. fs.delete(toDelete, false /* not recursive */);
  22. } catch (IOException e) {
  23. throw new RuntimeIOException(e, "Failed to delete file: %s", path);
  24. }
  25. }
  26. // implement this method to read catalog properties during initialization
  27. public void initialize(Map<String, String> properties) {
  28. }
  29. }

If you are already implementing your own catalog, you can implement TableOperations.io() to use your custom FileIO. In addition, custom FileIO implementations can also be dynamically loaded in HadoopCatalog and HiveCatalog by specifying the io-impl catalog property. Read the Configuration section for more details. If your FileIO must read Hadoop configuration to access certain environment properties, make your FileIO implement org.apache.hadoop.conf.Configurable.

Custom location provider implementation

Extend LocationProvider and provide implementation to determine the file path to write data

Example:

  1. public class CustomLocationProvider implements LocationProvider {
  2. private String tableLocation;
  3. // must have a 2-arg constructor like this, or a no-arg constructor
  4. public CustomLocationProvider(String tableLocation, Map<String, String> properties) {
  5. this.tableLocation = tableLocation;
  6. }
  7. @Override
  8. public String newDataLocation(String filename) {
  9. // can use any custom method to generate a file path given a file name
  10. return String.format("%s/%s/%s", tableLocation, UUID.randomUUID().toString(), filename);
  11. }
  12. @Override
  13. public String newDataLocation(PartitionSpec spec, StructLike partitionData, String filename) {
  14. // can use any custom method to generate a file path given a partition info and file name
  15. return newDataLocation(filename);
  16. }
  17. }

If you are already implementing your own catalog, you can override TableOperations.locationProvider() to use your custom default LocationProvider. To use a different custom location provider for a specific table, specify the implementation when creating the table using table property write.location-provider.impl

Example:

  1. CREATE TABLE hive.default.my_table (
  2. id bigint,
  3. data string,
  4. category string)
  5. USING iceberg
  6. OPTIONS (
  7. 'write.location-provider.impl'='com.my.CustomLocationProvider'
  8. )
  9. PARTITIONED BY (category);

Custom IcebergSource

Extend IcebergSource and provide implementation to read from CustomCatalog

Example:

  1. public class CustomIcebergSource extends IcebergSource {
  2. @Override
  3. protected Table findTable(DataSourceOptions options, Configuration conf) {
  4. Optional<String> path = options.get("path");
  5. Preconditions.checkArgument(path.isPresent(), "Cannot open table: path is not set");
  6. // Read table from CustomCatalog
  7. CustomCatalog catalog = new CustomCatalog(conf);
  8. TableIdentifier tableIdentifier = TableIdentifier.parse(path.get());
  9. return catalog.loadTable(tableIdentifier);
  10. }
  11. }

Register the CustomIcebergSource by updating META-INF/services/org.apache.spark.sql.sources.DataSourceRegister with its fully qualified name