zoukankan      html  css  js  c++  java
  • Hive 简明指南

    Hive 简明指南

    原文http://www.cnblogs.com/end/archive/2012/06/18/2553683.html

    1. 基本数据类型

    tinyint , smallint, int, bigint, float, double, boolean: true/false, string

    2. 基础运算符与函数

    A IS NULL 空
    A IS NOT NULL 非空
    A LIKE B 模糊匹配
    A RLIKE B 正则表达式匹配
    A REGEXP B 正则表达式匹配

    3. 类型转换

    cast(expr as <type>)

    例如:

    cast('1' as BIGINT) 将字符串'1'转化成bigint型

    4. 日期函数 (返回值类型 名称 描述 )

    string from_unixtime(int unixtime) 将时间戳(unix epoch秒数)转换为日期时间字符串,例如from_unixtime(0)="1970-01-01 00:00:00"
    
    bigint unix_timestamp() 获得当前时间戳
    
    bigint unix_timestamp(string date) 获得date表示的时间戳
    
    bigint to_date(string timestamp) 返回日期字符串,例如to_date("1970-01-01 00:00:00") = "1970-01-01"
    
    string year(string date) 返回年,例如year("1970-01-01 00:00:00") = 1970,year("1970-01-01") = 1970
    
    int month(string date)
    
    int day(string date) dayofmonth(date)
    
    int hour(string date)
    
    int minute(string date)
    
    int second(string date)
    
    int weekofyear(string date)
    
    int datediff(string enddate, string startdate) 返回enddate和startdate的天数的差,例如datediff('2009-03-01', '2009-02-27') = 2
    
    int date_add(string startdate, int days) 加days天数到startdate: date_add('2008-12-31', 1) = '2009-01-01'
    
    int date_sub(string startdate, int days) 减days天数到startdate: date_sub('2008-12-31', 1) = '2008-12-30'


    5. 条件函数 ( 返回值类型 名称 描述 )

    - if(boolean testCondition, T valueTrue, T valueFalseOrNull) 当testCondition为真时返回valueTrue,testCondition为假或NULL时返回valueFalseOrNull
    
    - COALESCE(T v1, T v2, ...) 返回列表中的第一个非空元素,如果列表元素都为空则返回NULL
    
    - CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END a = b,返回c;a = d,返回e;否则返回f
    
    - CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END a 为真,返回b;c为真,返回d;否则e

    例如:

    ( case 

      when category = '1512' then reserve_price > cast(1000 as double) 

      when category = '1101' then reserve_price > cast(2500 as double)

      else reserve_price > cast(10 as double) 

     end )

    6. 常用字符串函数 (返回值类型 名称 描述 )

    int length(string A) 返回字符串长度
    
    string reverse(string A) 反转字符串
    
    string concat(string A, string B...) 合并字符串,例如concat('foo', 'bar')='foobar'。注意这一函数可以接受任意个数的参数
    
    string substr(string A, int start) substring(string A, int start) 返回子串,例如substr('foobar', 4)='bar',详见 [4]
    
    string substr(string A, int start, int len) substring(string A, int start, int len) 返回限定长度的子串,例如substr('foobar', 4, 1)='b',详见[5]
    
    string upper(string A) ucase(string A) 转换为大写
    
    string lower(string A) lcase(string A) 转换为小写
    
    string trim(string A)
    
    string ltrim(string A)
    
    string rtrim(string A)
    
    string regexp_extract(string subject, string pattern, int intex) 返回使用正则表达式提取的子字串。
    
    例如,regexp_extract('foothebar', 'foo(.*?)(bar)', 2)='bar'。注意使用特殊字符的规则:
    
    使用'\s'代表的是字符's';空白字符需要使用'\\s',以此类推。
    
    string space(int n) 返回一个包含n个空格的字符串
    
    string repeat(string str, int n) 重复str字符串n遍
    
    string ascii(string str) 返回str中第一个字符的ascii码
    
    string lpad(string str, int len, string pad) 左端补齐str到长度为len。补齐的字符串由pad指定。
    
    string rpad(string str, int len, string pad) 右端补齐str到长度为len。补齐的字符串由pad指定。
    
    array split(string str, string pat) 返回使用pat作为正则表达式分割str字符串的列表。例如,split('foobar', 'o')[2] = 'bar'。

    7. 创建表 

    CREATE TABLE IF NOT EXISTS table_name (
      --field def
    )
    PARTITIONED BY (pt string)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY '\t'
    STORED AS TEXTFILE
    LOCATION '...';

    注意:如果不是外表部,drop table的时候会将HDFS上文件删除。

    8. 创建外部表

    CREATE EXTERNAL TABLE dm_all_cpv_assoc (
      --field def
    )
    PARTITIONED BY (pt string)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY '\1' 字段分隔符
    LINES TERMINATED BY '\2' 行分隔符
    STORED AS TEXTFILE
    LOCATION '...';

    注意:在删除外部表的时候,不会删除HDFS上的关联文件。

    9. 添加分区

    ALTER TABLE table_name ADD PARTITION (dt='2008-08-08', country='us')
    location '/path/to/us/part080808' PARTITION (dt='2008-08-09', country='us')
    location '/path/to/us/part080809';

    10. 删除分区

    ALTER TABLE table_name DROP PARTITION (dt='2008-08-08', country='us');

    11. 导入数据

    insert overwrite table table_name partition (pt = '20110323000000') select ... from ...
    
    LOAD DATA LOCAL INPATH 'test.dat' OVERWRITE INTO table yahoo_music partition (pt=xxx);

    12. 查询数据

    SELECT, JOIN, LIMIT

    13. 添加UDF

    add jar /home/hive/jar/my_udf.jar;
    
    create temporary function sys_date as 'com.taobao.hive.udf.UDFDateSysdate';

    14. 设置reducer数量

    限制最大reducer数:

    set hive.exec.reducers.max=15;

    设置固定的reducer数:

    set mapred.reduce.tasks=15;
  • 相关阅读:
    GUI基础学习
    常用类string的用法
    类。对象和包--补上周
    类.对象和包
    调用函数的注意事项
    函数的简单运用
    一维数组基础
    java中scanner类的用法
    数据库——DQL(语句查询)
    数据库——JDBC
  • 原文地址:https://www.cnblogs.com/rilley/p/2835308.html
Copyright © 2011-2022 走看看