zoukankan      html  css  js  c++  java
  • Hive进阶_汇总

    ===========================================================================
    第2章 Hive数据的导入
    使用Load语句执行数据的导入:
    
    LOAD 导入语句到数据仓库的表当中。
    load data [local] inpath 'filepath' [overwrite]
    into table tablename [partition (partcoll=val1, partcal2=val2...)]
    explain: local是从本地导入,无local则默认从HDFS导入,overwrite是否覆盖表中存在的数据。
    
    本地:
    导入一个文件
    load data local inpath 'localFilePath' into table tableName;
    load data local inpath 'localFilePath' into table tableName partition(gender='M');
    导入一个目录下的文件,覆盖原有数据
    load data local inpath 'localDirPath' overwrite into table tableName;
    
    HDFS:
    导入HDFS文件
    load data inpath 'localFilePath' into table tableName;
    
    ===================
    使用Sqoop进行数据的导入
    
    (1)下载: http://www-us.apache.org/dist/sqoop/1.4.6/
    (2)解压: # tar -zxvf sqoop-1.4.6.bin__hadoop-0.23.tar.gz 
    (3)配置: 
        # export HADOOP_MAPRED_HOME=/usr/local/src/hadoop-2.7.3/
        # export HADOOP_COMMON_HOMW=/usr/local/src/hadoop-2.7.3/
        连接oracle数据库时,ojdbc.jar 放到sqoop的lib目录下。
    (4)进入脚本执行目录下: cd sqoop-1.4.6.bin__hadoop-0.23/bin
    (5)执行脚本:
    ./sqoop import --connect jdbc:oracle:thin:@IP:port:dbName --username user --password password --table tableName --columns '' -m 1 --target-dir '/sqoop/emp'
    
    ===========================================================================
    第3章 Hive的数据查询
    Hive的数据查询_简单查询和fetch task
    过滤
    排序
    set hive.groupby.orderby.position.alias=true;
    
    ===========================================================================
    第4章 Hive的内置函数
    Hive数学函数
    select round(45.926,2),round(45.926,1),round(45.926,0),round(45.926,-1),round(45.926,-2);
    ===================
    Hive字符函数
    select lower('Hello World'), upper('Hello World');
    select length('Hello World'),length('你好');
    select concat('Hello','World');
    select substr('Hello World',3);
    select substr('Hello World',3,4);
    select lpad('abc',10,'*'),lpad('abc',10,'*');
    ===================
    Hive收集函数和转换函数
    size
    cast
    select size(map(1,'Tom',2,'Mary'));
    select cast(1 as bigint);
    select cast(1 as float);
    select cast('2015-04-10' as date);
    ===================
    Hive日期函数
    to_date
    year
    month
    day
    weekofyear
    datediff
    date_add
    date_sub
    select to_date ('2015-04-23 11:23:11');
    select year('2015-04-23 11:23:11'),month('2015-04-23 11:23:11'),day('2015-04-23 11:23:11');
    select weekofyear('2015-04-23 11:23:11');
    select datediff('2015-04-23 11:23:11','2014-04-23 11:23:11');
    select date_add('2015-04-23 11:23:11',2),date_sub('2015-04-23 11:23:11',2);
    ===================
    Hive条件函数
    coalesce:从左到右返回第一个不为null的值。
    case... when...:
    case a when b then c [when d then e]* [else if] end
    i.e.
    select ename,job,sal,
        case job when 'persident' then sal+1000,
                 when 'manager' then sal+800,
                 else sal+400
        end
    from emp;
    ===================
    Hive聚合函数和表生成函数
    聚合函数
    count
    sum
    min
    max
    avg
    表生成函数
    explode
    select count(*),sum(sal),max(sal),min(sal),avg(sal) from emp;
    select explode(map(1,'Tom',2,'Mary',3.'Mike'));
    ===========================================================================
    第5章 Hive的表连接
    等值连接
    select e.empno, d.deptno
    from emp e, dept d
    where e.deptno=d.deptno;
    ===================
    不等值连接
    select e.empno, e.ename, e.sal, s.grade
    from emp e, salgrade s
    where e.sal between s.losal and s.hisal;
    ===================
    外连接
    (取交集)
    select d.deptno, d.dname, count(e.empno)
    from emp e, dept d
    where e.deptno=d.deptno
    group by d.deptno, d.dname;
    (右外连接)
    select d.deptno, d.dname, count(e.empno)
    from emp e right outer join dept d
    on(e.deptno=d.deptno) 
    group by d.deptno, d.dname;
    ===================
    自连接
    select e.ename,b.ename
    from emp e, emp b
    where e.mgr=b.empno;
    
    ===========================================================================
    第6章 Hive的子查询
    集合中如果含null数据,不可使用not in, 可以使用in.
    hive只支持where和from子句中的子查询
    主查询和自查询可以不是同一张表
    
    select e.ename
    from emp e
    where e.deptno in
    (
        select d.deptno from dept d where d.dname='SALES' or d.dname='ACCOUNTING'
    );
    
    select *
    from emp e
    where e.deptno not in
    (
        select e1.mgr from emp e1 where e1.mgr is not null
    );
    
    ===========================================================================
    第7章 Hive的客户端操作
    启动远程客户端 # hive --service hiveserver2
    获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源
    
    ===========================================================================
    第8章 开发Hive的自定义函数
    自定义UDF需要继承org.apache.hadoop.hive.ql.UDF,实现evaluate函数,evaluate函数支持重载。
    进入hive客户端,添加jar包 
    hive> add jar /dir/.jar
    创建临时函数,指向jar包中的类
    hive> create temporary function <函数名> as 'java类名';
    select <函数名> from table;
    drop temporary function <函数名>;
  • 相关阅读:
    linux 常用快捷键
    命令行远程链接
    mybatis参数错误 Parameter '×××' not found. Available parameters are [0, 1, param1, param2]
    mybatis延迟加载
    mybatis跨XML引用
    eclipse修改项目名称
    wait, WIFEXITED, WEXITSTATUS
    进程通信中如何进行值得传递?
    fork新建进程
    Ubuntu安装genymotion模拟器步骤
  • 原文地址:https://www.cnblogs.com/liupuLearning/p/6349857.html
Copyright © 2011-2022 走看看