java操作hive和beeline的使用

一、java操作hive

1、启动服务:hiveserver2,让hive开启与外部连接的服务

nohup hiveserver2 1>/dev/null 2>/dev/null &
2、加入hive的依赖包
<dependency>
  <groupId>org.apache.hive</groupId>
  <artifactId>hive-jdbc</artifactId>
  <version>2.1.1</version>
</dependency>
在运行代码之前:配置:• 修改hadoop 配置文件 etc/hadoop/core-site.xml,加入如下配置项
<property>
  <name>hadoop.proxyuser.root.hosts</name>
  <value>*</value>
  </property>
<property>
  <name>hadoop.proxyuser.root.groups</name>
  <value>*</value>
</property>
不然会报错:
User: root is not allowed to impersonate root
配置完成重启一下namenode服务 
3、代码
public class hiveTest {
  public static void main(String[] args) throws Exception {
    //加载驱动
    Class.forName("org.apache.hive.jdbc.HiveDriver");
    //获取连接
    Connection connection = DriverManager.getConnection("jdbc:hive2://192.168.200.10:10000/db04", "root", "");
    //执行语句
    PreparedStatement preparedStatement = connection.prepareStatement("select * from t08");
    //处理结果
    ResultSet resultSet = preparedStatement.executeQuery();
    while (resultSet.next()){
      System.out.println(resultSet.getString(1));
  }  
    //关闭资源
    resultSet.close();
    preparedStatement.close();
    connection.close();
  }
}

二、beeline的使用

1、beeline是hive中提供给我们的一个远程操作工具,可以通过jdbc的连接信息来连接远程的hive
但是在使用之前需要将服务器上的hiveserver2服务开启

nohup hiveserver2 1>/dev/null 2>/dev/null &

2、进入到apache-hive-2.1.1-bin/bin开启

beeline
连接到数据库:!connect jdbc:hive2://192.168.200.10:10000/db04
用户:
密码:
3、输入sql语句

 三、shell下进入hive 

1、启动hive metastore

如果启动hive时遇到异常:如下,

解决:

./hive --service metastore &

 然后在启动:

./hive
原文地址:https://www.cnblogs.com/yfb918/p/10416968.html