Utility function to create an iceberg table from a directory.
(impala_client, unique_database, table_name,
file_format, table_location="${IMPALA_HOME}/testdata/data/iceberg_test",
warehouse_prefix=os.getenv("FILESYSTEM_PREFIX"))
| 31 | |
| 32 | |
| 33 | def create_iceberg_table_from_directory(impala_client, unique_database, table_name, |
| 34 | file_format, table_location="${IMPALA_HOME}/testdata/data/iceberg_test", |
| 35 | warehouse_prefix=os.getenv("FILESYSTEM_PREFIX")): |
| 36 | """Utility function to create an iceberg table from a directory.""" |
| 37 | |
| 38 | if not warehouse_prefix and unique_database: |
| 39 | warehouse_prefix = os.getenv("DEFAULT_FS", WAREHOUSE_PREFIX) |
| 40 | |
| 41 | # Only orc, parquet, and avro tested/supported |
| 42 | assert file_format in ["orc", "parquet", "avro"] |
| 43 | |
| 44 | table_location = os.path.expandvars(table_location) |
| 45 | local_dir = os.path.join(table_location, table_name) |
| 46 | assert os.path.isdir(local_dir) |
| 47 | |
| 48 | # Rewrite iceberg metadata to use the warehouse prefix and use unique_database |
| 49 | tmp_dir = tempfile.mktemp(table_name) |
| 50 | # Need to create the temp dir so 'cp -r' will copy local dir with its original name |
| 51 | # under the temp dir. rewrite_metadata() has the assumption that the parent directory |
| 52 | # of the 'metadata' directory bears the name of the table. |
| 53 | check_call(['mkdir', '-p', tmp_dir]) |
| 54 | check_call(['cp', '-r', local_dir, tmp_dir]) |
| 55 | local_dir = os.path.join(tmp_dir, table_name) |
| 56 | rewrite_metadata(warehouse_prefix, unique_database, os.path.join(local_dir, 'metadata')) |
| 57 | |
| 58 | # Put the directory in the database's directory (not the table directory) |
| 59 | hdfs_parent_dir = os.path.join(get_fs_path("/test-warehouse"), unique_database + ".db") |
| 60 | hdfs_dir = os.path.join(hdfs_parent_dir, table_name) |
| 61 | |
| 62 | # Purge existing files if any |
| 63 | check_call(['hdfs', 'dfs', '-rm', '-skipTrash', '-f', '-r', hdfs_dir]) |
| 64 | |
| 65 | # Note: -d skips a staging copy |
| 66 | check_call(['hdfs', 'dfs', '-mkdir', '-p', hdfs_parent_dir]) |
| 67 | check_call(['hdfs', 'dfs', '-put', '-d', local_dir, hdfs_parent_dir]) |
| 68 | |
| 69 | # Create external table |
| 70 | qualified_table_name = '{0}.{1}'.format(unique_database, table_name) |
| 71 | impala_client.execute("""create external table {0} stored as iceberg location '{1}' |
| 72 | tblproperties('write.format.default'='{2}', 'iceberg.catalog'= |
| 73 | 'hadoop.tables')""".format(qualified_table_name, hdfs_dir, |
| 74 | file_format)) |
| 75 | |
| 76 | # Automatic clean up after drop table |
| 77 | impala_client.execute("""alter table {0} set tblproperties ('external.table.purge'= |
| 78 | 'True');""".format(qualified_table_name)) |
| 79 | |
| 80 | |
| 81 | def create_table_from_parquet(impala_client, unique_database, table_name): |