hive基础知识or基本操作命令


MySQL的密码是:123456

1、hive创建标准表(以后均可以按照这样创建):

  create [external] table [if not exists] records

     (year STRING [comment "year备注消息"],

   temperature INT [comment "temperature 备注消息"],

        quality INT [comment "quality 备注消息"]

   )

  [comment "表records备注消息"]

  [partitioned by (col_name,col_name, ... )]

  [clustered by (col_name,....) into nums buckets]

  [ sorted by (col_name [ASC | DESC],...) into num_buckets BUCKETS ]

  row format delimited fields terminated by ‘\t’;

  [stored as file_format]

  [location hdfs_path]

解析:第一行声明一个records表,包括三列(3个字段):year,temperature,quality。分别指定数据类型为:string,int,int。//第一行解析与MySQL创建表一样

   第二行是HiveQL所特有,数据文件中每一行都是由制表符分隔的文本。若将文本导入由本地或者hdfs导入hive时必须按照此格式即每行三个字段,每个字段由制表符分隔,每行由换行符分隔

注意:row format delimited  分隔符设置的开始语句

     fields terminated by ‘\t’  设置字段与字段之间的分隔符,此时使用制表符分隔

     collection items terminated by "."  设置一个复杂类型(array,struct)字段中的各个item之间的分隔符,此时用点表示

     map keys terminated by "."  设置复杂类型map字段的key和value之间的分隔符,此时用电表示

     lines terminated by   设置行与行之间的分隔符

 例如:row format delimited
    fields terminated by '\t'
    collection items terminated by ','
    map keys terminated by ':'
    lines terminated by '\n';
    OK
    Time taken: 0.287 seconds
    ROW FORMAT DELIMITED 必须在其它分隔设置之前,也就是分隔符设置语句的最前
    LINES TERMINATED BY必须在其它分隔设置之后,也就是分隔符设置语句的最后,否则会报错

  PS:表名records在hdfs中其实是目录名;

2、将数据文件导入hive

  load data local inpath “/home/hdc/sample.txt”

  overwrite into table records;

解析:将本地文件sample导入records表中,其中overwrite可加可不加,如果加表示hive删除表对应目录中已有的所有文件,再将sample.txt文件送到records下;

   若不加表示hive简单的把sample.txt文件加入目录(若此目录有同名的文件则自动修改文件存储)。

3、--将HDFS中 /input/student.txt 导入到t3(此种方法是移动即student.txt文件将在t3文件夹下)

  load data inpath '/input/student.txt' overwrite into table t3;

4、将数据导入分区表:

  load data local inpath '/home/hdc/data_1.txt' into table partition_table partition(gender='M');

5、导出数据本地或者hdfs上:

  insert overwrite local directory ‘/home/hdc’  //除掉local,把路径改成hdfs上的路径就可以导入到hdfs上

  select * from student

6、通过查询语句向表中插入输入(从另外一个中的数据复制到另外一个表,两个表的字段要相同,并且其分区字段也相同即两表结构要一样)

  静态插入:

    insert into table employee  //insert overwrite table emplyee

    partition(country='US',state='OR')

    select * from staged_emplyee se

    where se.cnty='US' and se.st='OR';

  动态插入(以select语句后面的两个字段为分区字段):

    insert into table employee

    partition(country,state)

    select ....,se.cnty,se.st

    from staged_emplyee se;

  可以混合使用静态插入和动态插入(静态分区间键必须在动态分区键的前面)

    insert overwrite table emplyee

    partition(country='US',state)

    select ....,se.cnty,se.st

    from staged_emplyee se

    where se.cnty='US'

 7.创建外部表及其指定数据位置

  create table stu(name string,score int)

  row format delimited fields terminated by '\t'

  location '/data'(外部表关联hdfs上的文件)

8.外部表与内部表的关系

 1)未被external修饰的是内部表【managed table】,被external修饰的为外部表【external table】。

 2)内部表数据由Hive自身管理,外部表数据由HDFS管理。

 3)内部表数据存储在hive.metastore.warehouse.dir【默认:/user/hive/warehouse】,外部表数据存储位置由用户自己决定。

 4)删除内部表会直接删除元数据【metadata】及存储数据,删除外部表仅仅删除元数据,HDFS上的文件不会被删除。

 5)对内部表的修改会直接同步到元数据,而对外部表的表结构和分区进行修改,则需要修改【MSCK REPAIR TABLE table_name】。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM