Hive的安装,以及Hive在eclipse中JDBC接口实现(ubuntu)

参考链接:http://blog.csdn.net/yeruby/article/details/17591481

参考链接:http://blog.163.com/gibby_l/blog/static/83003161201402410204518/

材料:
ubuntu 15.04、hadoop 2.7.1
apache-hive-1.2.1.tar.gz(或者hive-1.2.1-bin.jar)
mysql-connector-java-5.1.6.tar.gz(或者mysql-connector-java-5.1.6-bin.jar文件)

安装:

1.网上下载上面所缺的文件。
(复制相应下载链接,在终端窗口使用sudo wget http://down1.chinaunix.net/distfiles/mysql-connector-java-5.1.6.tar.gz 可下载。)

安装hive

  1. 解压apache-hive-1.2.1.tar.gz: sudo tar -zxvf apache-hive-1.2.1.tar.gz
  2. 重命名,并移动到相应地方,并且修改权限。(笔者的用户为hadoop)
    sudo mv apache-hive-1.2.1 hive-1.2.1
    sudo mv hive-1.2.1 /usr/local/
    sudo chown hadoop:hadoop -R -f /usr/local/hive-1.2.1/

3.配置环境变量
sudo vim /etc/profile
在后面添加
export HIVE_HOME=/usr/local/hive-1.2.1
export PATH= PATH: HIVE_HOME/bin
:wq(保存退出)
source /etc/profile(使配置生效)

  1. 修改配置文件
    在/usr/local/hive-1.2.1/conf 里面有很多配置文件(都是以template结尾),将这些文件统统复制一份并重命(将.template去掉)
    进入该conf文件夹里面
    cp hive-env.sh.template hive-env.sh
    cp hive-default.xml.template hive-site.xml
    cp hive-exec-log4j.properties.template hive-exec-log4j.properties
    cp hive-log4j.properties.template hive-log4j.properties
    5.修改配置文件hive-site.xml
    conf文件夹里面。打开hive-site.xml,将下面东西替换进去。
<property>
  <name>javax.jdo.option.ConnectionURL</name>  
  <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
  <description>JDBC connect string FOR a JDBC metastore</description>
</property>
<property>  
  <name>javax.jdo.option.ConnectionDriverName</name>  
  <value>com.mysql.jdbc.Driver</value>
  <description>Driver class name FOR a JDBC metastore</description></property>
<property>  
  <name>javax.jdo.option.ConnectionUserName</name>  
  <value>hive</value>
  <description>username TOUSE against metastore database</description>
</property>
<property>
  <name>javax.jdo.option.ConnectionPassword</name>  
  <value>hive</value>
  <description>password TOUSE against metastore database</description>
</property>

(用户名和密码都是hive,这个用户是在mysql中创建的hive用户)

  1. 由于Hive0.11.0默认使用Derby数据库作为存储元数据的数据库,我们可以将此默认的数据库改为mysql,并修改hive-site.xml文件
    用root用户登录:mysql -u root -p
    然后创建用户hive并赋予root权限。
    mysql> CREATE USER ‘hive’ IDENTIFIED BY ‘hive’;
    mysql> GRANT ALL PRIVILEGES ON . TO ‘hive’@’%’ WITH GRANT OPTION;
    mysql> flush privileges;

由于mysql默认是本地登录,因此还需要修改mysql的配置文件把本地绑定注释掉:
命令:sudo gedit /etc/mysql/my.cnf
将”# bind-address = 127.0.0.1 “ 这行注释掉。
重启mysql服务:sudo service mysql restart

  1. 将下载的mysql-connector-java-5.1.6.tar.gz解压,复制里面的mysql-connector-java-5.1.6-bin.jar到HIVE_HOME/lib目录下。

8.启动hive
sudo hive
或者进入$HIVE_HOME中,输入hive
自动进入hive命令行接口后,输入show tables;(这时候可以像mysql一样创建表、插入数据等,不过速度非常慢)
如果正常输出,则说明hive配置成功。

二:eclipse中创建项目实现hive的jdbc接口

  1. eclipse中创建新的java项目,右键properties->build path->configure build path->Libraries->add variable
    ,然后将$HIVE_HOME/lib中的所有jar都添加进去,还有hadoop-core-xxx.jar添加进去,APPLY
    然后再 在order and export 中select All,APPLY。确定。
    2.在hive中开启端口监听用户的连接。hive –service hiverservice

**如果之后有提示,则按着提示走。
我这儿是接着输入hive –service hiveserver
报错,接着再输入hive –service hiveserver2 成功。

接着在项目中新建class,

package hadoop_hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public class HiveJdbcClient {
 private static String driverName = "com.mysql.jdbc.Driver";
 private static String url = "jdbc:mysql://localhost:3306/hive";
 private static String user = "root";
 private static String password = "123456";
 private static String sql = "";
 private static ResultSet res;
 private static final Logger log = Logger.getLogger(HiveJdbcClient.class);
 public static void main(String[] args) {
  try {

   Class.forName(driverName);
   Connection conn = DriverManager.getConnection(url, user, password);
   Statement stmt = conn.createStatement();

    // 创建的表名
    String tableName = "testHiveDriverTable";
    /** 第一步:存在就先删除 **/
    sql = "drop table if exists " + tableName ;
    stmt.execute(sql);

    /** 第二步:不存在就创建 **/
    sql = "create table " + tableName+ " (key int, value varcahr(30)) ";
    stmt.execute(sql);

    // 执行“show tables”操作
    sql = "show tables '" + tableName + "'";
    System.out.println("Running:" + sql);
    res = stmt.executeQuery(sql);
    System.out.println("执行“show tables”运行结果:");
    if (res.next()) {
    System.out.println(res.getString(1));
    }

    // 执行“describe table”操作
    sql = "describe " + tableName;
    System.out.println("Running:" + sql);
    res = stmt.executeQuery(sql);
    System.out.println("执行“describe table”运行结果:");
    while (res.next()) {
    System.out.println(res.getString(1) + "\t" + res.getString(2));
    }

    // 执行“load data into table”操作
    String filepath = "/home/hadoop/ziliao/userinfo.txt";
    sql = "load data local inpath '" + filepath + "' into table " +
    tableName;
    System.out.println("Running:" + sql);
    res = stmt.executeQuery(sql);

    // 执行“select * query”操作
    sql = "select * from " + tableName;
    System.out.println("Running:" + sql);
    res = stmt.executeQuery(sql);
    System.out.println("执行“select * query”运行结果:");
    while (res.next()) {
    System.out.println(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);
    System.out.println("执行“regular hive query”运行结果:");
    while (res.next()) {
    System.out.println(res.getString(1));

    }
   conn.close();
   conn = null;
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
   log.error(driverName + " not found!", e);
   System.exit(1);
  } catch (SQLException e) {
   e.printStackTrace();
   log.error("Connection error!", e);
   System.exit(1);
  }
 }
}

即可运行成功!(上面代码并没有调试,可以自己写一个简单的操作,然后再在数据库观察。)
(我们还可以在hdfs上找到刚才上传的文件http://blog.csdn.net/yeruby/article/details/17591481 有相应图片。)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值