这个步骤假设所有上述的内容均已经安装完毕。
1. eclipse 中 “文件--新建--其它”,选择 "Maven--Maven Project"。
2. 后续的步骤有点罗嗦,不再截图。
自定义Workspace Location。
在选择Archetype 的时候选择默认的 “maven-archetype-quickstart” "VERSION:1.1" 。
输入artifactId 即可完成。
3. 右键pom.xml文件--打开方式--XML EDITOR。
pom.xml文件内容(中文部分请自行删除):
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
Test
TestHive
0.0.1
jar
TestHive
http://maven.apache.org
UTF-8
org.apache.hadoop
hadoop-common
2.4.0
org.apache.hive
hive-jdbc
2.1.0
install
src/main/java
org.apache.maven.plugins
maven-compiler-plugin
lib
1.5
1.5
UTF-8
org.apache.maven.plugins
maven-jar-plugin
true
com.xx.xx.xx
org.apache.maven.plugins
maven-dependency-plugin
copy
install
copy-dependencies
${project.build.directory}
org.apache.maven.plugins
maven-resources-plugin
2.4
UTF-8
4. 创建Client类。
4.1 在src/main/java上右键,创建“包:cn.sam.test.hive”。
4.2 在 cn.sam.test.hive 上右键,创建“类:Client”。
4.3 输入代码:
package cn.sam.test.hive;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
/**
* 测试 hive 的客户端连接
*
*/
public class Client {
// 注意:hive-server2 引用的driver是 org.apache.hive.* 而 hive-server 是 org.apache.hadoop.hive.*
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
// private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
// hive的默认端口是 10000,如果要修改就修改 hive-site.xml 文件的hive.server2.thrift.port 属性值
// 此处容易遇到坑,网上教程均表示采用默认的 hive 用户和空密码即可,但实际测试是不行的
// 需要使用能连接hive的系统用户名称和系统用户密码,我是把hive装在 hadoop 用户下的。
Connection con = DriverManager.getConnection("jdbc:hive2://VirtualBox:10000/default", "hadoop", "hadoop");
// Connection con = DriverManager.getConnection("jdbc:hive://VirtualBox:10000/default", "hadoop", "hadoop");
Statement stmt = con.createStatement();
// 测试的表名 test
String tableName = "test";
// 如果已经存在就删除
stmt.execute("drop table if exists " + tableName);
// 创建这张表
stmt.execute("create table " + tableName + " (id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'");
// 看下表是否存在
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));
}
// 加载数据到表里面 -- 方式1: 系统的本地文件
// 注意: filepath 是服务器上文件的位置,注意这个不是你的电脑!
String filepath = "/home/hadoop/tmp/testData.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
stmt.execute(sql);
// 加载数据到表里面 -- 方式2: hadoop上的文件
// String filepath = "/user/hive/tmp/testData.txt";
// sql = "load data inpath '" + filepath + "' into table " + tableName;
// System.out.println("Running: " + sql);
// stmt.execute(sql);
// 查询数据
sql = "select * from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
}
}
4.4 别忙着执行这一段代码,先在hive server上创建 /home/hadoop/tmp/testData.txt 文件: