Hive入門詳解(三)

個人博客原文鏈接

內置函數UDF和內置運算符

  • 取隨機數rand()
    select rand() from t_product;

  • 求a的階乘
    factorial(INT a)
    select factorial(10) from t_product;

  • 求最大值
    greatest(T v1, T v2, …)
    select greatest(10,123,53,34,1,23,502,120) from t_product;

  • 求最小值
    least(T v1, T v2, …)
    select least(10,123,53,34,1,23,502,120) from t_product;

  • 數學常量e
    select e() from t_product;

  • 數學常量pi
    select pi() from t_product;

  • 返回當前時間
    select current_date from t_product;

  • 如果列中有null值,則返回默認值
    nvl(T value, T default_value)
    select id,nvl(name, ‘無名氏’) from t_product;

  • 對於值的不同判斷,取不同的值
    CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
    如果a=b就返回c,a=d就返回e,否則返回f
    如CASE 4 WHEN 5 THEN 5 WHEN 4 THEN 4 ELSE 3 END 將返回4
    select CASE id WHEN 1 THEN ‘正品’ ELSE ‘山寨’ END,name from t_product;

  • 判斷某個文件中是否包含某個字符串
    in_file(string str, string filename)
    select in_file(‘start’,’/home/briup/test’) from t_product;

  • 通過某個符號切割字符串
    split(string str, string pat)
    select split(‘hello,world,briup’, ‘,’) from t_product;

  • 截取字符串
    substr(string|binary A, int start, int len)
    select substr(‘ceo-larry’, 0, 3) from t_product;

  • 在某字符串中查找某個子串第一次出現的位置,位置從1開始
    instr(string str, string substr)
    select instr(‘ceo-larry’, ‘la’) from t_product;

  • 將第一個字符串中的,符合第二個字符串的部分,替換成第三個字符串
    translate(string|char|varchar input, string|char|varchar from, string|char|varchar to)
    select translate(‘hello briup’, ‘briup’, ‘imut’) from t_product;

  • 比較兩個字符串,不同的字符個數
    levenshtein(string A, string B)
    select levenshtein(‘hello’, ‘worldd’) from t_product;

  • 把array中的字符用某個符號拼接起來
    concat_ws(string SEP, array)
    select concat_ws(’#’, split(‘hello,world,briup’, ‘,’)) from t_product;

自定義函數UDF

  1. 寫一個java類extends UDF,定義某個邏輯
  2. 打成jar包上傳到hive所在的節點
  3. 在hive中創建一個函數,和jar中的自定義類建立關聯
    • 導入包
      add jar /home/hadoop/udf.jar
    • 建立關聯
      create temporary function getArea as ‘com.briup.udf.MyUDF’;
    • 查看自定義函數
      show functions;
    • 使用自定義函數
      select id,name,tel,getArea(tel) from t_student_udf;

JDBC連接hive

  1. 修改配置文件hdfs-site.xml
<property>  
<name>dfs.webhdfs.enabled</name>  
<value>true</value>  
</property>  
  1. 修改配置文件core-site.xml
<property>
<name>hadoop.proxyuser.hadoop.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.hadoop.groups</name>
<value>*</value>
</property>

注:以上兩個屬性的第三位指的是hive所在機器的用戶名
3. 開啓hive服務保持持續運行
hive --service hiveserver2 &
4. 編寫代碼測試連接

public class JDBCTest {
    public static void main(String[] args) throws Exception {
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection conn = DriverManager.getConnection("jdbc:hive2://hadoop1:10000/db1803","hadoop","hadoop");
        System.out.println(conn);
        Statement stat = conn.createStatement();
        String hql = "create table t_student_jdbc(id int,name string,tel string)" +
                " row format delimited" +
                " fields terminated by ','" +
                " stored as textfile";
        stat.execute(hql);
        stat.close();
        conn.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章