华为云用户手册

  • 示例 --创建测试表Create table show_table1(a int);Create table show_table2(a int);Create table showtable5(a int);Create table intable(a int);Create table fromtable(a int); --匹配单字符'_'show tables in default like 'show_table_'; Table ------------- show_table1 show_table2 (2 rows) --匹配多字符'*','%'show tables in default like 'show%';Table ------------- show_table1 show_table2 showtable5 (3 rows) show tables in default like 'show*';Table ------------- show_table1 show_table2 showtable5 (3 rows) --转义字符使用,第二个示例将'_'作为过滤条件,结果集不包含showtable5 show tables in default like 'show_%'; Table ------------- show_table1 show_table2 showtable5 (3 rows) show tables in default like 'show$_%' ESCAPE '$'; Table ------------- show_table1 show_table2 (2 rows) --同时满足多个条件,查询default中'show_'开头或者'in'开头的表show tables in default like 'show$_%|in%' ESCAPE '$'; Table ------------- intable show_table1 show_table2 (3 rows)
  • 示例 -- 演示数据准备create schema show_schema;use show_schema;create table show_table1(a int,b string);create table show_table2(a int,b string);create table from_table1(a int,b string);create table in_table1(a int,b string);--查询表名以"show"开始的表的详细信息show table extended like 'show*'; tab_name -------------------------------------------------------------------------- tableName:show_table1 owner:admintest location:hdfs://hacluster/user/hive/warehouse/show_schema.db/show_table1 InputFormat:org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat:org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat columns:struct columns {int a,string b} partitioned:false partitionColumns: totalNumberFiles:0 totalFileSize:0 tableName:show_table2 owner:admintest location:hdfs://hacluster/user/hive/warehouse/show_schema.db/show_table2 InputFormat:org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat:org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat columns:struct columns {int a,string b} partitioned:false partitionColumns: totalNumberFiles:0 totalFileSize:0 (1 row)-- 查询表名以"from"或者"show"开头的表的详细信息 show table extended like 'from*|show*'; tab_name -------------------------------------------------------------------------- tableName:show_table1 owner:admintest location:hdfs://hacluster/user/hive/warehouse/show_schema.db/show_table1 InputFormat:org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat:org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat columns:struct columns {int a,string b} partitioned:false partitionColumns: totalNumberFiles:0 totalFileSize:0 tableName:from_table1 owner:admintest location:hdfs://hacluster/user/hive/warehouse/show_schema.db/from_table1 InputFormat:org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat:org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat columns:struct columns {int a,string b} partitioned:false partitionColumns: totalNumberFiles:0 totalFileSize:0 (1 row)-- 查询web schema下的page_views表扩展信息 show table extended from web like 'page*'; tab_name ----------------------------------------------------------------------------- tableName:page_views owner:admintest location:hdfs://hacluster/user/web.db/page_views InputFormat:org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat:org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat columns:struct columns {timestamp view_time,bigint user_id,string page_url} partitioned:true partitionColumns: struct partition_columns {date ds,string country} totalNumberFiles:0 totalFileSize:0 (1 row)
  • 示例 创建示例所需视图: Create schema test1;Use test1;Create table t1(id int, name string);Create view v1 as select * from t1;Create view v2 as select * from t1;Create view t1view as select * from t1;Create view t2view as select * from t1; Show views;Table-------- t1view t2view v1 v2(4 rows) Show views like 'v1'; Table------- v1(1 row) Show views 'v_';Table ------- v1 v2 (2 rows)show views like 't*'; Table-------- t1view t2view Show views in test1; Table-------- t1view t2view v1 v2(4 rows)
  • 示例 将表名从users 修改为 people: ALTER TABLE users RENAME TO people; 在表users中增加名为zip的列: ALTER TABLE users ADD COLUMN zip varchar; 从表users中删除名为zip的列: ALTER TABLE users DROP COLUMN zip; 将表users中列名id更改为user_id: ALTER TABLE users RENAME COLUMN id TO user_id; 给users表添加一个creator属性,值为user1: 可通过DESCRIBE EXTENDED/FORMATTED TABLE查看添加结果: ALTER TABLE users set tblproperties('creator' = 'user1'); --查看新增的属性DESCRIBE FORMATTED users; Describe Formatted Table ------------------------------------------------------------------------------ # col_name data_type comment id integer name varchar # Detailed Table Information Database: default Owner: admintest LastAccessTime: 0 Location: hdfs://hacluster/user/hive/warehouse/users Table Type: MANAGED_TABLE # Table Parameters: STATS_GENERATED_VIA_STATS_TASK workaround for potential lack of HIVE-12730 creator user1 numFiles 0 numRows 0 orc.compress.size 262144 orc.compression.codec GZIP orc.row.index.stride 10000 orc.stripe.size 67108864 presto_query_id 20210308_023136_00031_jiwsq@default@HetuEngine presto_version rawDataSize 0 totalSize 0 transient_lastDdlTime 1615170696 # Storage Information SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat Compressed: No Num Buckets: -1 Bucket Columns: [] Sort Columns: [] serialization.format: 1 (1 row) Query 20210308_032152_00046_jiwsq@default@HetuEngine, FINISHED, 1 nodeSplits: 1 total, 1 done (100.00%)0:00 [0 rows, 0B] [0 rows/s, 0B/s] 修改分区操作: --创建两个分区表CREATE TABLE IF NOT EXISTS hetu_int_table5 (eid int, name String, salary String, destination String, dept String, yoj int) COMMENT 'Employee Names' partitioned by (dt timestamp,country String, year int, bonus decimal(10,3)) STORED AS TEXTFILE; CREATE TABLE IF NOT EXISTS hetu_int_table6 (eid int, name String, salary String, destination String, dept String, yoj int) COMMENT 'Employee Names' partitioned by (dt timestamp,country String, year int, bonus decimal(10,3)) STORED AS TEXTFILE; --添加分区ALTER TABLE hetu_int_table5 ADD IF NOT EXISTS PARTITION (dt='2008-08-08 10:20:30.0', country='IN', year=2001, bonus=500.23) PARTITION (dt='2008-08-09 10:20:30.0', country='IN', year=2001, bonus=100.50) ; --查看分区show partitions hetu_int_table5; dt | country | year | bonus -------------------------|---------|------|--------- 2008-08-09 10:20:30.000 | IN | 2001 | 100.500 2008-08-08 10:20:30.000 | IN | 2001 | 500.230 (2 rows) --删除分区ALTER TABLE hetu_int_table5 DROP IF EXISTS PARTITION (dt=timestamp '2008-08-08 10:20:30.0', country='IN', year=2001, bonus=500.23); --查看分区show partitions hetu_int_table5; dt | country | year | bonus -------------------------|---------|------|--------- 2008-08-09 10:20:30.000 | IN | 2001 | 100.500 (1 row) --迁移分区示例CREATE SCHEMA part_test;CREATE TABLE hetu_exchange_partition1 (a string, b string) PARTITIONED BY (ds string);CREATE TABLE part_test.hetu_exchange_partition2 (a string, b string) PARTITIONED BY (ds string);ALTER TABLE hetu_exchange_partition1 ADD PARTITION (ds='1'); --查看分区 show partitions hetu_exchange_partition1; ds ---- 1 (1 row) show partitions part_test.hetu_exchange_partition2; ds ----(0 rows) --迁移分区,从 T1 到 T2ALTER TABLE part_test.hetu_exchange_partition2 EXCHANGE PARTITION (ds='1') WITH TABLE hetu_exchange_partition1; --再次查看分区,可以看到分区迁移成功show partitions hetu_exchange_partition1; ds ---- (0 row) show partitions part_test.hetu_exchange_partition2; ds ----1(1 rows) --重命名分区CREATE TABLE IF NOT EXISTS hetu_rename_table ( eid int, name String, salary String, destination String, dept String, yoj int) COMMENT 'Employee details' partitioned by (year int) STORED AS TEXTFILE; ALTER TABLE hetu_rename_table ADD IF NOT EXISTS PARTITION (year=2001); SHOW PARTITIONS hetu_rename_table;year ------ 2001 (1 row) ALTER TABLE hetu_rename_table PARTITION (year=2001) rename to partition (year=2020); SHOW PARTITIONS hetu_rename_table;year ------ 2020 (1 row) --修改分区表create table altercolumn4(a integer, b string) partitioned by (c integer); --修改表的文件格式 alter table altercolumn4 SET FILEFORMAT textfile; insert into altercolumn4 values (100, 'Daya', 500); alter table altercolumn4 partition (c=500) change column b empname string comment 'changed column name to empname' first; --修改分区表的存储位置(需要先在hdfs上创建目录,执行语句后,无法查到之前插入的那条数据)alter table altercolumn4 partition (c=500) set Location '/user/hive/warehouse/c500'; --修改列 b 改名为name,同时类型从integer转为string(对列属性修改需要先修改Hive数据源properties属性,新增“hive.orc.use-column-names=true”配置,否则会报错)create table altercolumn1(a integer, b integer); alter table altercolumn1 change column b name string; --修改altercolumn1的存储属性ALTER TABLE altercolumn1 CLUSTERED BY(a, name) SORTED BY(name) INTO 25 BUCKETS; --查看altercolumn1的属性describe formatted altercolumn1; Describe Formatted Table ---------------------------------------------------------------------------------------- # col_name data_type comment a integer name varchar # Detailed Table Information Database: default Owner: admintest LastAccessTime: 0 Location: hdfs://hacluster/user/hive/warehouse/altercolumn1 Table Type: MANAGED_TABLE # Table Parameters: STATS_GENERATED_VIA_STATS_TASK workaround for potential lack of HIVE-12730 numFiles 0 numRows 0 orc.compress.size 262144 orc.compression.codec GZIP orc.row.index.stride 10000 orc.stripe.size 67108864 presto_query_id 20210325_025238_00034_f63xj@default@HetuEngine presto_version rawDataSize 0 totalSize 0 transient_lastDdlTime 1616640758 # Storage Information SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe InputFormat: org.apache.hadoop.mapred.TextInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat Compressed: No Num Buckets: 25 Bucket Columns: [a, name] Sort Columns: [SortingColumn{columnName=name, order=ASCENDING}] Storage Desc Params: serialization.format 1 (1 row) Query 20210325_090522_00091_f63xj@default@HetuEngine, FINISHED, 1 nodeSplits: 1 total, 1 done (100.00%)0:00 [0 rows, 0B] [0 rows/s, 0B/s]
  • SHOW语法使用概要 SHOW语法主要用来查看数据库对象的相关信息,其中LIKE子句用来对数据库对象过滤,匹配规则如下,具体示例可参看SHOW TABLES: 规则1:_可用用来匹配单个任意字符。 规则2:%可以用来匹配0个或者任意个任意字符。 规则3:* 可以用来匹配0个或者任意个任意字符。 规则4:|可以用来配置多种规则,规则之间用“|”分隔。 规则5:当想将“_”作为匹配条件时,可以使用ESCAPE 指定一个转义字符,对“_”进行转义,以免按照规则1对“_”进行解析。 父主题: DDL 语法
  • 限制 EXCHANGE PARTITION: 被迁移的单个或多个分区,迁移前必须都是已存在的分区,并归属于来源表,且在目标表中不包含这些分区; 该操作涉及的表需要有相同的列定义,并且有相同的分区键; 如果表中包含索引,该操作会失败; 来源表和目标表中任意一个为事务表时,不允许Exchange partition操作; 对于目标表,在一次操作中,多个分区要么同时迁移成功,要么全部失败。对于来源表,操作成功后,所有迁移的分区都会被释放; Alter table change column不支持orc格式的表。 ALTER TABLE table_name ADD | DROP col_name命令仅对于ORC/PARQUET存储格式的非分区表可用。
  • 示例 列出当前catalog所有的schemas: SHOW SCHEMAS; 列出指定catalog下的schema_name前缀为"t"的所有schemas: SHOW SCHEMAS FROM hive LIKE 't%';--等价写法:SHOW SCHEMAS IN hive LIKE 't%'; 如果匹配字符串中有字符与通配符冲突,可以指定转义字符来标识,示例为查询hive这个catalog下,schema_name前缀为“pm_”的所有schema,转义字符为“/”: SHOW SCHEMAS IN hive LIKE 'pm/_%' ESCAPE '/';
  • 语法 CREATE FUNCTION qualified_function_name ( parameter_name parameter_type [, ...] ) RETURNS return_type [ COMMENT function_description ] [ LANGUAGE [ JAVA | JDBC ] ] [ SPECIFIC specificName ] [ DETERMINISTIC | NOT DETERMINISTIC ] [ RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT ] [ SYMBOL class_name ] [ URI hdfs_path_to_jar ]
  • 示例 返回一个1列3行的表: VALUES 1, 2, 3 返回一个2列3行的表: VALUES(1, 'a'),(2, 'b'),(3, 'c') 返回具有列名id、name的表: SELECT * FROM ( (1, 'a'), (2, 'b'),(3, 'c')) AS t (id, name); 创建一个具有列名id、name的新表: CREATE TABLE example AS SELECT * FROM (VALUES (1, 'a'), (2, 'b'), (3, 'c')) AS t (id, name);
  • 示例 创建一个新的JAVA函数“example.default.add_two”(需要先构建和部署UDF) CREATE FUNCTION example.default.add_two ( num integer)RETURNS integerLANGUAGE JAVADETERMINIS TICS YMBOL "com.example.functions.AddTwo"URI "hdfs://hacluster/udfs/function-1.0.jar";--执行函数select hetu.default.add_two(2); 创建一个新的JDBC函数“example.namespace02.repeat” CREATE FUNCTION example.namespace02.repeat ( str VARCHAR, times BIGINT)RETURNS VARCHARLANGUAGE JD BCS PECIFIC repeatDETERMINISTIC;--执行函数select example.namespace02.repeat(t1.name,3) from mppdb.test.table1 t1;
  • 描述 通过给定的定义创建一个新的函数。 每一个函数都由其限定函数名称和参数类型列表唯一标识。“qualified_function_name”的格式需要为“catalog.schema.function_name”,函数命名空间(格式为“catalog.schema”)可以自行规划管理,与HetuEngine中的catalog、schema概念无关联;“parameter_type”需要为HetuEngine支持的数据类型。 “return_type”需要为HetuEngine支持的数据类型,要与函数的返回实际类型匹配,不做类型强制转换。 可以指定一组特征来修饰函数并指定其行为,每个特征最多只能指定一次,详情请参考表1。 表1 特征说明 特征 默认值 描述 Language clause JDBC 定义函数的语言。目前支持JAVA、JDBC两种语言。 JAVA函数:需要提供函数实现的JAR文件,并将JAR文件放入HetuEngine可以读取的HDFS中。 JDBC函数:在HetuEngine中映射JDBC数据源的函数。目前仅支持映射JDBC数据源的Scalar UDF(即为结果集的每一行返回一个值)。 Specific name - JDBC函数使用,指定函数在JDBC数据源中的限定函数名称。 Deterministic characteristic NOT DETERMINISTIC 函数是否确定性。 DETERMINISTIC:如果函数在使用相同的输入集调用时总是返回相同的结果集,则该函数被视为确定性。 NOT DETERMINISTIC:如果函数在使用相同的输入集调用时不返回相同的结果集,则该函数将被视为非确定性。 Null-call clause CALLED ON NULL INPUT 函数的行为。 RETURNS NULL ON NULL INPUT:当“NULL”作为函数参数时,返回“NULL”。 CALLED ON NULL INPUT:当“NULL”作为函数参数时调用。 Symbol class_name - JAVA函数使用,指定函数实现的限定类名。 Uri hdfs_path_to_jar - JAVA函数使用,指定函数实现的JAR文件路径。
  • 示例 -- 删除原生/管控表Create table simple(id int, name string); Insert into simple values(1,'abc')(2,'def'); select * from simple; id | name----|------ 1 | abc 2 | def(2 rows) Truncate table simple; select * from simple; id | name----|------(0 rows) --删除表分区Create table tb_truncate_part (id int, name string) partitioned by (age int, state string); Insert into tb_truncate_part values (1,'abc',10,'ap'),(2,'abc',10,'up'),(3,'abc',20,'ap'),(4,'abc',20,'up'); select * from tb_truncate_part; id | name | age | state----|------|-----|------- 2 | abc | 10 | up 3 | abc | 20 | ap 1 | abc | 10 | ap 4 | abc | 20 | up(4 rows Truncate table tb_truncate_part partition (state = 'ap', age = 10); select * from tb_truncate_part;id | name | age | state----|------|-----|------- 4 | abc | 20 | up 2 | abc | 10 | up 3 | abc | 20 | ap(3 rows)
  • 示例 创建一个新表orders,使用子句with指定创建表的存储格式、存储位置、以及是否为外表。 通过“auto.purge”参数可以指定涉及到数据移除操作(如DROP、DELETE、INSERT OVERWRITE、TRUNCATE TABLE)时是否清除相关数据: "auto.purge"='true'时,清除元数据和数据文件。 "auto.purge"='false'时,仅清除元数据,数据文件会移入HDFS回收站。默认值为“false”,且不建议用户修改此属性,避免数据删除后无法恢复。 CREATE TABLE orders (orderkey bigint,orderstatus varchar,totalprice double,orderdate date)WITH (format = 'ORC', location='/user',orc_compress='ZLIB',external=true, "auto.purge"='false');-- 通过DESC FORMATTED 语句,可以查看建表的详细信息desc formatted orders ; Describe Formatted Table ------------------------------------------------------------------------------ # col_name data_type comment orderkey bigint orderstatus varchar totalprice double orderdate date # Detailed Table Information Database: default Owner: admintest LastAccessTime: 0 Location: hdfs://hacluster/user Table Type: EXTERNAL_TABLE # Table Parameters: EXTERNAL TRUE auto.purge false orc.compress.size 262144 orc.compression.codec ZLIB orc.row.index.stride 10000 orc.stripe.size 67108864 presto_query_id 20220812_084110_00050_srknk@default@HetuEngine presto_version 1.2.0-h0.cbu.mrs.320.r1-SNAPSHOT transient_lastDdlTime 1660293670 # Storage Information SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat Compressed: No Num Buckets: -1 Bucket Columns: [] Sort Columns: [] Storage Desc Params: serialization.format 1 (1 row) 创建一个新表,指定Row format: --建表时,指定表的字段分隔符为‘,’号(如果创建外表,要求数据文件中的每条记录的字段是以逗号进行分隔)CREATE TABLE student(id string,birthday string,grade int,memo string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';--建表时,指定字段分隔符为'\t',换行符为'\n'CREATE TABLE test(id int, name string ,tel string)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'LINES TERMINATED BY '\n'STORED AS TEXTFILE; 如果表orders不存在,则创建表orders,并且增加表注释和列注释: CREATE TABLE IF NOT EXISTS orders (orderkey bigint,orderstatus varchar,totalprice double COMMENT 'Price in cents.',orderdate date)COMMENT 'A table to keep track of orders.';insert into new_orders values(202011181113,'online',9527,date '2020-11-11'),(202011181114,'online',666,date '2020-11-11'),(202011181115,'online',443,date '2020-11-11'),(202011181115,'offline',2896,date '2020-11-11'); 使用表orders的列定义创建表bigger_orders: CREATE TABLE bigger_orders (another_orderkey bigint,LIKE orders,another_orderdate date);SHOW CREATE TABLE bigger_orders ; Create Table --------------------------------------------------------------------- CREATE TABLE hive.default.bigger_orders ( another_orderkey bigint, orderkey bigint, orderstatus varchar, totalprice double, ordersdate date, another_orderdate date ) WITH ( external = false, format = 'ORC', location = 'hdfs://hacluster/user/hive/warehouse/bigger_orders', orc_compress = 'GZIP', orc_compress_size = 262144, orc_row_index_stride = 10000, orc_stripe_size = 67108864 ) (1 row) 标号① 建表示例: CREATE EXTERNAL TABLE hetu_test (orderkey bigint, orderstatus varchar, totalprice double, orderdate date) PARTITIONED BY(ds int) SORT BY (orderkey, orderstatus) COMMENT 'test' STORED AS ORC LOCATION '/user' TBLPROPERTIES (orc_compress = 'SNAPPY', orc_compress_size = 6710422, orc_bloom_filter_columns = 'orderstatus,totalprice'); 标号② 建表示例: CREATE EXTERNAL TABLE hetu_test1 (orderkey bigint, orderstatus varchar, totalprice double, orderdate date) COMMENT 'test' PARTITIONED BY(ds int) CLUSTERED BY (orderkey, orderstatus) SORTED BY (orderkey, orderstatus) INTO 16 BUCKETS STORED AS ORC LOCATION '/user' TBLPROPERTIES (orc_compress = 'SNAPPY', orc_compress_size = 6710422, orc_bloom_filter_columns = 'orderstatus,totalprice'); 标号③ 建表示例: CREATE TABLE hetu_test2 (orderkey bigint, orderstatus varchar, totalprice double, orderdate date, ds int) COMMENT 'This table is in Hetu syntax' WITH (partitioned_by = ARRAY['ds'], bucketed_by = ARRAY['orderkey', 'orderstatus'], sorted_by = ARRAY['orderkey', 'orderstatus'], bucket_count = 16, orc_compress = 'SNAPPY', orc_compress_size = 6710422, orc_bloom_filter_columns = ARRAY['orderstatus', 'totalprice'], external = true, format = 'orc', location = '/user');
  • 表配置 HetuEngine连接Hive数据源的表支持如下表配置: 表2 Hive数据源的表配置 Catalog名 orc表参数 默认值 类型 描述 hive orc_compress GZIP varchar 高级别的压缩算法 (NONE, ZLIB, SNAPPY, GZIP, ZSTD, LZ4) hive orc_compress_size 262144 long 每个压缩块中的字节数 hive orc_stripe_size 67108864 long 每个stripe中的字节数 hive orc_row_index_stride 10000 long 索引条目之间的行数 hive orc_bloom_filter_columns '' varchar 逗号分隔的列名称列表,应为其创建布隆过滤器 hive orc_bloom_filter_fpp 0.05 double 布隆过滤器的positive概率(必须大于0.0且小于1.0) 例如: CREATE TABLE format1(orderkey bigint, orderstatus varchar, totalprice double, orderdate date) TBLPROPERTIES (orc_compress = 'SNAPPY', orc_compress_size = 6710422, orc_bloom_filter_columns = 'orderstatus,totalprice');CREATE TABLE format2(orderkey bigint, orderstatus varchar, totalprice double, orderdate date) TBLPROPERTIES (orc_compress = 'LZ4', orc_stripe_size = 33554432, orc_bloom_filter_fpp = 0.1, orc_row_index_stride=5000, orc_compress_size = 6710422, orc_bloom_filter_columns = 'orderstatus,totalprice'); 查看表的配置参数: Show create table format2; Create Table----------------------------------------------------------------------- CREATE TABLE hive.default.format2 ( orderkey bigint, orderstatus varchar, totalprice double, orderdate date ) WITH ( external = false, format = 'ORC', location = '/user/hive/warehouse/format2', orc_bloom_filter_columns = ARRAY['orderstatus','totalprice'], orc_bloom_filter_fpp = 1E-1, orc_compress = 'LZ4', orc_compress_size = 6710422, orc_row_index_stride = 5000, orc_stripe_size = 33554432 )(1 row)
  • 限制 相同序号(①、②、③)标记的表达式/关键字可以同时使用,不同序号标记的表达式/关键字不能同时使用。 session属性可以设置bucket_count,默认值为-1,表示未设置。创建表时,如果bucket_count为-1且建表语句中未设置buckets,则使用默认值16。 默认外部表存储位置/user/hive/warehouse/{schema_name}/{table_name},其中{schema_name}为建表时使用的schema,{table_name}为表名。 指定属性“transactional=true”可以让表支持“原子性、一致性、隔离性、持久性”写入的事务能力,但是将表定义为事务表后,无法通过设置“transactional=false”将其退化为非事务表。 transactional='true'或 '0'在执行过程中不会进行类型转换,所以这种写法会抛出异常: Cannot convert ['true'] to boolean Cannot convert ['0'] to boolean 默认不允许向托管表(表属性external = true)插入数据,如需使用该功能,可参考注意事项,添加hive自定义属性:hive.non-managed-table-writes-enabled=true。 Mppdb有一个限制,数据库的标识符的最大长度为63,如果我们把标识符命名超过了最大长度,那么会被自动截取掉超出的部分,只留下最大长度的标识符。 跨域场景不支持建表。
  • 语法 CREATE [EXTERNAL]①,② TABLE [IF NOT EXISTS] [catalog_name.][db_name.]table_name ( { [col_name data_type [COMMENT col_comment] [ WITH③ ( property_name = expression [, ...] )③ ] | LIKE③ existing_table_name [ {INCLUDING | EXCLUDING}③ PROPERTIES]③ } [, ...]) [[PARTITIONED BY①(col_name data_type, ....)] [SORT BY① ([column [, column ...]])] ]① [COMMENT 'table_comment'] [ WITH ( property_name = expression [, ...] ) ]③ [[PARTITIONED BY② (col_name data_type, .....)] [CLUSTERED BY② (col_name, col_name, ...) [SORTED BY②③ (col_name, col_name, ...)] INTO num_buckets BUCKETS]② ]② [ [ROW FORMAT row_format] [STORED AS file_format] [LOCATION 'hdfs_path'] [TBLPROPERTIES (orc_table_property = value [, ...] ) ] ]①,②
  • 描述 使用CREATE TABLE创建一个具有指定列的、新的空表。使用CREATE TABLE AS创建带数据的表。 使用可选参数IF NOT EXISTS,如果表已经存在则不会报错。 WITH子句可用于在新创建的表或单列上设置属性,如表的存储位置(location)、是不是外表(external)等。 LIKE子句用于在新表中包含来自现有表的所有列定义。可以指定多个LIKE子句,从而允许从多个表中复制列。如果指定了INCLUDING PROPERTIES,则将所有表属性复制到新表中。如果WITH子句指定的属性名称与复制的属性名称相同,则将使用WITH子句中的值。默认是EXCLUDING PROPERTIES属性,而且最多只能为一个表指定INCLUDING PROPERTIES属性。 PARTITIONED BY能够用于指定分区的列;CLUSTERED BY能够被用于指定分桶的列;SORT BY和 SORTED BY能够用于给指定的分桶列进行排序;BUCKETS能够被用于指定分桶数;EXTERNAL可用于指定创建外部表;STORED AS能被用于指定文件存储的格式;LOCATION能被用于指定在HDFS上存储的路径。 想要查看当前数据源支持哪些column属性,可以运行以下命令,会显示当前对接的catalog分别支持哪些列属性。 SELECT * FROM system.metadata.column_properties; 想要查看当前数据源支持哪些table属性,可以运行以下命令: SELECT * FROM system.metadata.table_properties; 下表为catalog为hive时的查询结果。 表1 查询结果 Catalog名 属性名称 默认值 类型 描述 hive avro_schema_url - varchar URI pointing to Avro schema for the table hive bucket_count 0 integer Number of buckets hive bucketed_by [] array(varchar) Bucketing columns hive bucketing_version - integer Bucketing version hive csv_escape - varchar CS V escape character hive csv_quote - varchar CSV quote character hive csv_separator - varchar CSV separator character hive external_location - varchar File system location URI for external table hive format ORC varchar Hive storage format for the table. Possible values: [ORC, PARQUET, AVRO, RCBINARY, RCTEXT, SEQUENCEFILE, JSON, TEXTFILE, CSV] hive orc_compress GZIP varchar Compression codec used. Possible values: [NONE, SNAPPY, LZ4, ZSTD, GZIP, ZLIB] hive orc_compress_size 262144 bigint orc compression size hive orc_row_index_stride 10000 integer no. of row index strides hive orc_stripe_size 67108864 bigint orc stripe size hive orc_bloom_filter_columns [] array(varchar) ORC Bloom filter index columns hive orc_bloom_filter_fpp 0.05 double ORC Bloom filter false positive probability hive partitioned_by [] array(varchar) Partition columns hive sorted_by [] array(varchar) Bucket sorting columns hive textfile_skip_footer_line_count - integer Number of footer lines hive textfile_skip_header_line_count - integer Number of header lines hive transactional false boolean Is transactional property enabled systemremote configs configurations varchar configurations for current query systemremote init_query_id initqueryid varchar init query id systemremote initiator_format initiator format varchar column metadata from initiator
  • 描述 “ALTER VIEW view_name AS select_statement;”用于改变已存在的视图的定义,语法效果与CREATE OR REPLACE VIEW类似。 “ALTER VIEW view_name SET TBLPROPERTIES table_properties;”中table_properties格式为 (property_name = property_value, property_name = property_value, ...)。 视图可以包含Limit和ORDER BY子句,如果关联视图的查询语句也包含了这类子句,则最后执行结果将根据视图的子句运算后得到。例如视图V指定了返回5条数据,而关联查询为select * from V limit 10,则最终只有5条数据返回。
  • 示例 CREATE OR REPLACE VIEW tv_view as SELECT id,name from (values (1, 'HetuEngine')) as x(id,name); SELECT * FROM tv_view; id | name ----|------ 1 | HetuEngine (1 row) ALTER VIEW tv_view as SELECT id, brand FROM (VALUES (1, 'brand_1', 100), (2, 'brand_2', 300) ) AS x (id, brand, price); SELECT * FROM tv_view; id | brand ----|--------- 1 | brand_1 2 | brand_2 (2 rows) ALTER VIEW tv_view SET TBLPROPERTIES ('comment' = 'This is a new comment'); show tblproperties tv_view; SHOW TBLPROPERTIES -------------------------------------------------------------------- comment This is a new comment presto_query_id 20210325_034712_00040_f63xj@default@HetuEngine presto_version presto_view true transient_lastDdlTime 1616644032 (1 row)
  • 描述 修改物化视图的状态,仅支持修改处于“ENABLED”和“SUSPEND”状态的物化视图,且只能修改为其中一种状态。物化视图所有状态包含如下: SUSPEND:暂停使用状态,暂停使用的物化视图不会参与改写 ENABLED:可使用状态 REFRESHING:正在刷新物化视图数据,不可用于改写 DISABLED:关闭使用 UNKNOWN:缓存与数据库不一致,建议执行refresh catalog mv;
  • 示例 创建视图 create view orders_by_date as select * from orders; 删除视图orders_by_date,如果视图不存在则会报错 DROP VIEW orders_by_date; 删除视图orders_by_date,使用参数IF EXISTS,如果视图存在则删除视图,如果视图不存在,也不会报错 DROP VIEW IF EXISTS orders_by_date;
  • 描述 DATABASE和SCHEMA在概念上是等价可互换的。 该语法用于删除数据库databasename,如果目标数据库不存在,将抛出错误提示,但如果使用了IF EXISTS子句则不会抛出错误提示。 可选参数RESTRICT|CASCADE用于指定删除的模式,默认是RESTRICT模式,在这种模式下,数据库必须为空,不包含任何表才能删除,如果是CASCADE模式,表示级联删除,会先删除数据库下面的表 ,再删除数据库。
  • 示例 删除schema web: DROP SCHEMA web; 如果schema sales存在,删除该schema: DROP SCHEMA IF EXISTS sales; 级联删除schema test_drop,schema test_drop中存在表tb_web,会先删除tb_web,再删除test_drop: CREATE SCHEMA test_drop; USE test_drop; CREATE TABLE tb_web(col1 int); DROP DATABASE test_drop CASCADE;
  • CREATE/DROP/SHOW VIRTUAL SCHEMA(S) CREATE HetuEngine中的CREATE语句用来创建SCHEMA映射,通过映射信息对外开放本域数据源。 语法如下: CREATE VIRTUAL SCHEMA [ IF NOT EXISTS ] [ ctlg_dest.]schema_name WITH ( [catalog = ctlg_name,] schema = schm_name, [property_name = expression, ...] ) 创建一个virtual schema,需要在WITH中提供具体映射的schema信息。 ctlg_dest为在哪个数据源创建virtual schema,参数可选,如果不指定则取当前Session中的catalog,如果当前Session中也未指定catalog则会创建失败。 WITH必选,schema参数必选,catalog参数可选(如果不指定则取当前Session中的catalog)。 样例语句: CREATE VIRTUAL SCHEMA hive_default WITH (catalog = 'hive', schema = 'default'); DROP HetuEngine中的DROP语句用来删除SCHEMA映射。 语法如下: DROP VIRTUAL SCHEMA [ IF EXISTS ] schema_name schema_name也可以替换为全限定名(catalogName.virtualSchema)。 样例语句: DROP VIRTUAL SCHEMA hive_default; SHOW HetuEngine中的SHOW语句用来查询所有SCHEMA映射。 语法如下: SHOW VIRTUAL SCHEMAS [ FROM catalog ] [ LIKE pattern ] 样例语句: SHOW VIRTUAL SCHEMAS;
  • 语法 ALTER (DATABASE|SCHEMA) schema_name SET LOCATION hdfs_location ALTER (DATABASE|SCHEMA) database_name SET OWNER USER username ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);
  • 描述 该语法是使用SELECT查询结果创建物化视图。物化视图是一个数据库对象,它包含了一个查询的结果,例如:它可以是远程数据的本地副本,单表查询或者多表join后查询的结果的行或列、行和列的子集,也可以是使用聚合函数的汇总表。 物化视图通常基于对数据表进行聚合和连接的查询结果创建。物化视图支持“查询重写”,这是一种优化技术,它将以原始表编写的用户查询转换为包括一个或多个物化视图的等效请求。 语法支持的属性包括: mv_storage_table:指定存储表表名。 need_autorefresh: 管理计算实例时,预先创建维护实例后,可通过设置need_autorefresh为true,创建具备自动刷新能力的物化视图,它会自动创建并提交物化视图刷新任务,在此基础上,可对refresh_duration,start_refresh_ahead_of_expiry,refresh_priority等属性做进一步配置来调整自动刷新任务。 mv _validity:物化视图生命周期。0表示永久有效,最短为5分钟。need_autorefresh设置为false时,mv _validity默认值为0;设置为true时,默认值为24小时。 refresh_duration:物化视图自动刷新任务的最长等待时间。默认为5分钟,取值范围为1分钟到24小时。若自动刷新任务的等待时间超过设定的最长等待时间,自动化任务界面对应的任务状态显示为"timeout"。 start_refresh_ahead_of_expiry:基于mv _validity设置物化视图自动刷新任务的提交时间,表示达到物化生命周期的指定百分比时,提交自动刷新任务,默认值为0.2,最小值为0.05。 refresh_priority:物化视图提交自动刷新任务的优先级。默认值为3,最大值为3,1表示最高优先级。高优先级的任务会有更大机会先被执行。
  • 示例 创建基础表order01和order02 CREATE TABLE order01(id int,name string,tel string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'STORED AS TEXTFILE;CREATE TABLE order02(sku int, sku_name string, sku_describe string); 创建表orders_like01,它将包含表order01定义的列及表属性 CREATE TABLE orders_like01 like order01 INCLUDING PROPERTIES; 创建表orders_like02,它将包含表order02定义的列,并将表的存储格式设置为‘TEXTFILE’ CREATE TABLE orders_like02 like order02 STORED AS TEXTFILE; 创建表orders_like03,它将包含表order01定义的列及表属性,order02定义的列,以及额外的列c1和c2 CREATE TABLE orders_like03 (c1 int,c2 float,LIKE order01 INCLUDING PROPERTIES,LIKE order02); 创建表orders_like04和orders_like05,它们都会包含同一个表order_partition的定义,但orders_like04不会包含分区键信息,而orders_like05会包含分区键的信息 CREATE TABLE order_partition(id int,name string,tel string) PARTITIONED BY (sku int);CREATE TABLE orders_like04 (like order_partition);CREATE TABLE orders_like05 like order_partition;DESC orders_like04; Column | Type | Extra | Comment --------|---------|-------|--------- id | integer | | name | varchar | | tel | varchar | | sku | integer | | (4 rows)DESC orders_like05; Column | Type | Extra | Comment --------|---------|---------------|--------- id | integer | | name | varchar | | tel | varchar | | sku | integer | partition key | (4 rows)
  • 限制 仅Hive数据源的Catalog支持视图的列描述。 在HetuEngine中创建的视图,视图的定义以编码方式存储在数据源里。在数据源可以查询到该视图,但无法对该视图执行操作。 视图是只读的,不可对它执行LOAD、INSERT操作。 视图可以包含ORDER BY和LIMIT子句,如果关联了该视图的查询语句也包含了这些子句,那么查询语句中的ORDER BY和LIMIT子句将以视图的结果为基础进行运算。
  • 示例 通过表orders创建一个视图test: CREATE VIEW test (oderkey comment 'orderId',orderstatus comment 'status',half comment 'half') ASSELECT orderkey, orderstatus, totalprice / 2 AS half FROM orders; 通过表orders的汇总结果创建视图orders_by_date: CREATE VIEW orders_by_date ASSELECT orderdate, sum(totalprice) AS priceFROM ordersGROUP BY orderdate; 创建一个新视图来替换已经存在的视图: CREATE OR REPLACE VIEW test ASSELECT orderkey, orderstatus, totalprice / 4 AS quarterFROM orders 创建一个视图的同时设置表属性: create or replace view view1 comment 'the first view' TBLPROPERTIES('format'='orc') as select * from fruit;
  • 描述 使用LIKE子句可以在一个新表中包含一个已存在的表所有的列定义。可以使用多个LIKE来复制多个表的列。 如果使用了INCLUDING PROPERTIES,表的所有属性也会被复制到新表,该选项最多只能对一个表生效。 对于从表中复制过来的属性,可以使用WITH子句指定属性名进行修改。 默认使用EXCLUDING PROPERTIES属性。 对于带分区的表,如果用括号包裹like子句,复制的列定义不会包含分区键的信息。
共100000条