您的位置:首页 > 编程语言 > Java开发

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

2015-09-28 00:19 531 查看
参考链接:/article/2652135.html

参考链接: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文件)

安装:

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


安装hive

解压apache-hive-1.2.1.tar.gz: sudo tar -zxvf apache-hive-1.2.1.tar.gz

重命名,并移动到相应地方,并且修改权限。(笔者的用户为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:PATH:HIVE_HOME/bin

:wq(保存退出)

source /etc/profile(使配置生效)

修改配置文件

在/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,将下面东西替换进去。

[code]<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用户)

由于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

将下载的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接口

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,

[code]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上找到刚才上传的文件/article/2652135.html 有相应图片。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: