Hive安装与使用

简介

hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行。

安装

API

hive ddl

 如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCE 

使用

//启动
cd /
//进入命令行环境
/opt/hive/bin/hive
//语句
show tables;
create table test(key string);
insert into test values("123");
select * from test;

java操作

//依赖
<dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>2.1.0</version>
</dependency>
<dependency>  
    <groupId>org.apache.hadoop</groupId>  
    <artifactId>hadoop-common</artifactId>  
    <version>2.6.0</version>  
</dependency>
//启动服务
 hive --service hiveserver2

java示例程序

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveJdbcClient {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver"; 
    public static void main(String[] args) throws SQLException {  
       try {  
          Class.forName(driverName);  
        } catch (ClassNotFoundException e) {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
          System.exit(1);  
        }  
    
        //hive的默认端口是 10000,如果要修改就修改 hive-site.xml 文件的hive.server2.thrift.port 属性值          
        Connection con = DriverManager.getConnection("jdbc:hive2://192.168.1.150:10000/default", "hadoop", "123456");  
          
        Statement stmt = con.createStatement();

        //测试的表名 testhivedrivertable  
        String tableName = "testhivedrivertable";  
          
        //如果已经存在就删除  
        stmt.execute("drop table if exists " + tableName);  
          
        //创建这张表  
        stmt.execute("create table " + tableName + " (key int, value string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\054'");  
        //看下创建是否成功  
        String sql = "show tables '" + tableName + "'";  
        System.out.println("Running: " + sql);  
        ResultSet res = stmt.executeQuery(sql);  
        if (res.next()) {  
          System.out.println(res.getString(1));  
        }  
          
        //看下表结构  
        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));  
        }    
    }
}

示例程序运行结果

《Hive安装与使用》 Paste_Image.png

报错

  1. hadoop is not allowed to impersonate anonymous
    API参考
#hadoop:core-site.xml
#这里不能设置为root
<property><name>hadoop.proxyuser.hadoop.hosts</name><value>*</value></property>
<property><name>hadoop.proxyuser.hadoop.groups</name><value>*</value></property>
#重启hadoop
#java程序
#设置用户名hadoop,不使用空
Connection con = DriverManager.getConnection("jdbc:hive2://192.168.1.100:10000/default", "hadoop", "123456");

参考文章

Hive:用Java代码通过JDBC连接Hiveserver
hadoop is not allowed to impersonate anonymous

    原文作者:紫玥迩
    原文地址: https://www.jianshu.com/p/1fc8a4e14970
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞