Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

时间:2023-03-09 05:28:49
Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

写在前面的话

    可详细参考,一定得去看

HBase 开发环境搭建(Eclipse\MyEclipse + Maven)

Zookeeper项目开发环境搭建(Eclipse\MyEclipse + Maven)

  我这里,相信,能看此博客的朋友,想必是有一定基础的了。我前期写了大量的基础性博文。可以去补下基础。

步骤一:File  ->  New  -> Project   ->  Maven Project

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

步骤二:自行设置,待会创建的myHBase工程,放在哪个目录下。

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

步骤三:

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

步骤四:自行设置

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

步骤五:修改jdk

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

省略,很简单!

步骤六:修改pom.xml配置文件

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

官网Maven的zookeeper配置文件内容:

地址:http://www.mvnrepository.com/search?q=hive

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

1、

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

2、

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

3、

  Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

4、

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

5、

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

6、

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

  暂时这些吧,以后需要,可以自行再加呢!

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

  最后的pom.xml配置文件为

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>zhouls.bigdata</groupId>
<artifactId>myHive</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myHive</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-metastore</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-common -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-common</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-service -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
</project>

当然,这只是初步而已,最简单的,以后可以自行增删。

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

参考:Hive的JDBC接口实现(Eclipse环境配置)

hive里的JDBC编程入门

 在使用JDBC链接Hive之前,首先要开启Hive监听用户的连接。即在运行代码前,得

开启Hive服务的方法如下:

  hive --service hiveserver2 >/dev/null  2>/dev/null&

步骤七:这里,给大家,通过一组简单的Hive应用程序实例来向大家展示Hive的某些功能。

  类名为HiveTestCase.java

 package zhouls.bigdata.myHive;

 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; /**
* Handle data through hive on eclipse
* @author zhouls
* @time 2016\11\12 22:14
*/
public class HiveTestCase {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://djt002:10000/default";
private static String user = "";
private static String password = "";
private static String sql = "";
private static ResultSet res;
private static final Logger log = Logger.getLogger(HiveTestCase.class); public static void main(String[] args) {
try {
Class.forName(driverName); // 注册JDBC驱动
// Connection conn = DriverManager.getConnection(url, user, password); //默认使用端口10000, 使用默认数据库,用户名密码默认
Connection conn = DriverManager.getConnection("jdbc:hive2://djt002:10000/default", "", "");
// Connection conn = DriverManager.getConnection("jdbc:hive://HadoopSlave1:10000/default", "", "");
//当然,若是3节点集群,则HadoopMaster或HadoopSlave1或HadoopSlave2都可以呢。前提是每个都安装了Hive,当然只安装一台就足够了。 // Statement用来执行SQL语句
Statement stmt = conn.createStatement(); // 创建的表名
String tableName = "testHiveDriverTable"; /** 第一步:存在就先删除 **/
sql = "drop table " + tableName;
stmt.executeQuery(sql); /** 第二步:不存在就创建 **/
sql = "create table " + tableName +
"(userid int , " +
"movieid int," +
"rating int," +
"city string," +
"viewTime string)" +
"row format delimited " +
"fields terminated by '\t' " +
"stored as textfile"; // sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t'";
stmt.executeQuery(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 = "/usr/local/data/test2_hive.txt"; //因为是load data local inpath,所以是本地路径
sql = "load data local inpath '" + filepath + "' into table " + tableName; // String filepath = "/hive/data/test2_hive.txt"; //因为是load data inpath,所以是集群路径,即hdfs://djt002/9000/hive/data/下
// sql = "load data 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);
} }
}

  或者

 package com.dajangtai.Hive;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class Demo {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";//hive驱动名称
private static String url = "jdbc:hive2://djt002:10000/default";//连接hive2服务的连接地址,Hive0.11.0以上版本提供了一个全新的服务:HiveServer2
private static String user = "hadoop";//对HDFS有操作权限的用户
private static String password = "";//在非安全模式下,指定一个用户运行查询,忽略密码
private static String sql = "";
private static ResultSet res;
public static void main(String[] args) {
try {
Class.forName(driverName);//加载HiveServer2驱动程序
Connection conn = DriverManager.getConnection(url, user, password);//根据URL连接指定的数据库
Statement stmt = conn.createStatement(); //创建的表名
String tableName = "testHiveDriverTable"; /** 第一步:表存在就先删除 **/
sql = "drop table " + tableName;
stmt.execute(sql); /** 第二步:表不存在就创建 **/
sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t' STORED AS TEXTFILE";
stmt.execute(sql); // 执行“show tables”操作
sql = "show tables '" + tableName + "'";
res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
} // 执行“describe table”操作
sql = "describe " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
} // 执行“load data into table”操作
String filepath = "/usr/local/data/djt.txt";//hive服务所在节点的本地文件路径
sql = "load data local inpath '" + filepath + "' into table " + tableName;
stmt.execute(sql); // 执行“select * query”操作
sql = "select * from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getInt(1) + "\t" + res.getString(2));
} // 执行“regular hive query”操作,此查询会转换为MapReduce程序来处理
sql = "select count(*) from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
conn.close();
conn = null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
}
}

  若是MyEclipse里,需要注意一下

MyEclipse *的安装步骤和破解(32位和64位皆适用)

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)