hive操作

作者: 紫玥迩 | 来源:发表于2016-07-08 11:40 被阅读107次
    1. Hive没有行级别的插入,更新和删除操作,往表中插入数据的唯一方法就是使用成批载入操作.
      数据是以load的方式加载到建立好的表中,数据一旦导入就不可以修改.
    2. 如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCE
    3. java依赖
    <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.1.0</version>
    </dependency>
    
     Class.forName(driverName);  
    Connection con = DriverManager.getConnection("jdbc:hive2://192.168.60.1:10000/default", "hadoop", "123456");          
    

    建表(导入数据表的数据格式是:字段之间是tab键分割,行之间是断行)

    create table weblogin(id int,webuser_id int) 
    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' lines terminated by '\n'
    STORED AS TEXTFILE
    

    分区

    1. 一个表可以拥有一个或者多个分区,每个分区以文件夹的形式单独存在表文件夹的目录下
    2. 分区是以字段的形式在表结构中存在,通过describe table命令可以查看到字段存在,但是该字段不存放实际的数据内容,仅仅是分区的表示
    3. 分区建表分为2种,一种是单分区,也就是说在表文件夹目录下只有一级文件夹目录。另外一种是多分区,表文件夹下出现多文件夹嵌套模式
    4. 0.7版本以后不存在的分区会自动创建,0.6之前的版本官方文档上说必须要预先创建好分区:
    #单分区建表语句
    create table day_table (id int, content string) partitioned by (dt string);
    #双分区建表语句
    create table day_hour_table (id int, content string) partitioned by (dt string, hour string)
    
    1. 静态分区列和动态分区列
    #
    

    load数据
    加载的目标可以是一个表或者分区。如果表包含分区,必须指定每一个分区的分区名。
    filepath可以是文件或目录
    •指定了OVERWRITE
    •目标表(或者分区)中的内容(如果有)会被删除,然后再将 filepath 指向的文件/目录中的内容添加到表/分区中。
    •如果目标表(分区)已经有一个文件,并且文件名和 filepath 中的文件名冲突,那么现有的文件会被新文件所替代。

    LOAD DATA [LOCAL] INPATH 'filepath' 
    [OVERWRITE] INTO TABLE tablename 
    [PARTITION (partcol1=val1, partcol2=val2 ...)]
    
    String filepath = "/home/hadoop/test/hive.txt";
    sql = "load data local inpath '" + filepath + "' into table webuser " ;  
    stmt.execute(sql);
    

    查询

    sql = "select * from weblogin where webuser_id=1";  
    System.out.println("Running: " + sql);  
    res = stmt.executeQuery(sql);  
    while (res.next()) {  
        System.out.println("Id: "+res.getInt(1)+ "\twebuserId: " + res.getString(2));  
    }
    

    建索引ds

    CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);
    

    复制表结构

    CREATE TABLE empty_key_value_store
    LIKE key_value_store
    

    删除列

    CREATE TABLE test_change (a int, b int, c int);
    ALTER TABLE test_change REPLACE COLUMNS (a int, b int)
    

    删除表

    DROP TABLE pokes;
    

    join查询,只支持等值连接
    LEFT/RIGHT/FULL OUTER JOIN
    LEFT SEMI JOIN 的限制是:JOIN 子句中右边的表只能在 ON 子句中设置过滤条件,在 WHERE 子句、SELECT 子句或其他地方过滤都不行
    union all

    select * from dual a join dual b on a.key = b.key;
    select * from weblogin join webuser on weblogin.webuser_id=webuser.id where webuser.id=1
    

    分号转码

    select concat(key,concat('\073',key)) from dual;
    

    其他

    SHOW TABLES '.*s';
    ALTER TABLE pokes ADD COLUMNS (new_col INT);
    ALTER TABLE events RENAME TO 3koobecaf;
    show tables;
    show databases;
    show partitions ;
    show functions;
    describe extended table_name dot col_name
    
    1. 参考
      官网API
      hive基础sql语法
      hive中partition的使用
      静态分区列和动态分区列

    相关文章

      网友评论

        本文标题:hive操作

        本文链接:https://www.haomeiwen.com/subject/eayljttx.html