hive学习001 hive的使用

来源:互联网 发布:国外数据采集 的方法 编辑:程序博客网 时间:2024/06/12 18:14

概念

  hive是一个数据仓库(数据库与数据仓库概念我也不是很清楚),不过他可以建很多数据库,很多数据库下又会有很多的表。提供了一种HIVE QL的查询语言。

结构

 

(图选自hive 用户指导1.0,版本不同会有差异http://wenku.baidu.com/link?url=Oe4fznW4mkt5yS418-oPSFwGFEJYrco9NkGH-2LBMsdSztQ_jloUg6xKcGD3_pXewnNTMUA6X-UWAuIg8HioNu8kSwjyAnQRapUoCkz5vki点击打开链接)

使用

   HIVE提供了dbc、webgui、cli这样的接口:

1.CLI的使用

 使用的是Bitvise SSH Client的Linux远程桌面程序:步骤如图示


WEB-GUI使用

JDBC方式使用

   java程序中绑定hive数据库(类似于mysql、oracle数据库一类),wiki地址:https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC点击打开链接

 

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient {
  privatestaticString driverName ="org.apache.hive.jdbc.HiveDriver";
 
  /**
   * @param args
   * @throws SQLException
   */
  publicstaticvoid main(String[] args)throws SQLException {
      try{
      Class.forName(driverName);
    }catch(ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default","hive","");//获取数据源
    Statement stmt = con.createStatement();
    String tableName ="testHiveDriverTable";
    stmt.execute("drop table if exists "+ tableName);
    stmt.execute("create table "+ tableName +" (key int, value string)");
    // show tables
    String sql ="show tables '"+ tableName + "'";
    System.out.println("Running: "+ sql);
    ResultSet res = stmt.executeQuery(sql);
    if(res.next()) {
      System.out.println(res.getString(1));
    }
       // describe table
    sql ="describe "+ tableName;
    System.out.println("Running: "+ sql);
    res = stmt.executeQuery(sql);
    while(res.next()) {
      System.out.println(res.getString(1) +"\t"+ res.getString(2));
    }
 
    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath ="/tmp/a.txt";
    sql ="load data local inpath '"+ filepath + "' into table "+ tableName;
    System.out.println("Running: "+ sql);
    stmt.execute(sql);
 
    // select * query
    sql ="select * from "+ tableName;
    System.out.println("Running: "+ sql);
    res = stmt.executeQuery(sql);
    while(res.next()) {
      System.out.println(String.valueOf(res.getInt(1)) +"\t"+ res.getString(2));
    }
 
    // regular hive query
    sql ="select count(1) from "+ tableName;
    System.out.println("Running: "+ sql);
    res = stmt.executeQuery(sql);
    while(res.next()) {
      System.out.println(res.getString(1));
    }
  }
}

接下来一篇就是SQL方言各类特性


0 0
原创粉丝点击