一、创建外部表
CREATE EXTERNAL TABLE iceberg_create1 (i int)
STORED BY 'org.apache.iceberg.mr.hive.HiveIcebergStorageHandler'
TBLPROPERTIES('iceberg.catalog'='iceberg_hive');
describe formatted iceberg_create1;
二、创建内部表
CREATE EXTERNAL TABLE iceberg_create2 (i int)
STORED BY 'org.apache.iceberg.mr.hive.HiveIcebergStorageHandler'
TBLPROPERTIES('iceberg.catalog'='iceberg_hive');
describe formatted iceberg_create2;
三、创建分区表
CREATE EXTERNAL TABLE iceberg_create3 (id int,name string)
PARTITIONED BY (age int)
STORED BY 'org.apache.iceberg.mr.hive.HiveIcebergStorageHandler'
TBLPROPERTIES('iceberg.catalog'='iceberg_hive');
describe formatted iceberg_create3;
- Hive语法创建分区表,不会在元数据创建分区,而是将分区数据转换为Iceberg标识分区。
- 这种情况下不能使用Iceberg的分区转换,例如:days(timestamp),如果想要使用Iceberg格式表的分区转换标识分区,需要使用Spark或者Flink引擎创建表。
insert into iceberg_create3 values(1,'A',18);
四、修改表
只支持HiveCatalog表修改表属性,Iceberg表属性和Hive表属性存储在HMS中是同步的
ALTER TABLE iceberg_create1 SET TBLPROPERTIES('external.table.purge'='FALSE');
添加字段
ALTER TABLE iceberg_create1 ADD COLUMNS(j int);
五、插入数据
INSERT INTO iceberg_test values(1);
insert overwrite table iceberg_create1 select * from iceberg_create2;
六、删除表
DROP TABLE iceberg_create1;
七、完整创建、修改、插入数据、删除Iceberg表的例子
创建表,插入数据:
CREATE TABLE my_table (
id INT,
name STRING,
age INT
)
USING iceberg
PARTITIONED BY (age)
LOCATION 'hdfs:///data/my_table';
INSERT INTO my_table (id, name, age) VALUES (1, 'John Doe', 30);
INSERT INTO my_table (id, name, age) VALUES (2, 'Jane Doe', 28);
INSERT INTO my_table (id, name, age) VALUES (3, 'Bob Smith', 35);
修改添加字段:
ALTER TABLE my_table
ADD COLUMNS (hobby ARRAY<STRING>);
创建一张新表:
CREATE TABLE my_table_new (
id INT,
name STRING,
age INT,
hobby ARRAY<STRING>
)
USING iceberg
PARTITIONED BY (age)
LOCATION 'hdfs:///data/my_table_new/';
使用INSERT INTO语句向该表中插入新数据:
该语句从原表my_table中筛选出年龄为35的记录,并将这些记录的字段值以及新字段hobby的值(使用ARRAY函数)插入到新表my_table_new中。注意,这里使用Iceberg HiveCatalog插入了新数据,而非在Hive中对现有数据进行修改。文章来源:https://www.toymoban.com/news/detail-508019.html
INSERT INTO my_table_new
SELECT id, name, age, ARRAY('fishing', 'reading') AS hobby
FROM my_table
WHERE age = 35;
删除原来的表,重命名新表为旧表的名字:文章来源地址https://www.toymoban.com/news/detail-508019.html
DROP TABLE my_table;
ALTER TABLE my_table_new RENAME TO my_table;
到了这里,关于Iceberg从入门到精通系列之三:创建Iceberg表、修改表结构、插入数据、删除表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!