问题导读:
1.hive启动远程服务接口的命令是什么?
2.如何实现hive嵌入Java编程?
1,hive首先要启动远程服务接口,命令:
nohup hive –service hiveserver &
2,java工程中导入相应的需求jar包,列表如下:
antlr-runtime-3.0.1.jar
hive-exec-0.7.1.jar
hive-jdbc-0.7.1.jar
hive-metastore-0.7.1.jar
hive-service-0.7.1.jar
jdo2-api-2.3-ec.jar
libfb303.jar
3,简单测试代码,已经测试成功操作
package com.javabloger.hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class HiveTestCase {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
String dropSQL="drop table javabloger";
String createSQL="create table javabloger (key int, value string)";
//hive插入数据支持两种方式一种:load文件,令一种为从另一个表中查询进行插入(感觉这是个鸡肋)
//hive是不支持insert into...values(....)这种操作的
String insterSQL="LOAD DATA LOCAL INPATH '/work/hive/examples/files/kv1.txt' OVERWRITE INTO TABLE javabloger";
String querySQL="SELECT a.* FROM javabloger a";
Connection con = DriverManager.getConnection("jdbc:hive://192.168.X.X:10000/default", "", "");
Statement stmt = con.createStatement();
stmt.executeQuery(dropSQL); // 执行删除语句
stmt.executeQuery(createSQL); // 执行建表语句
stmt.executeQuery(insterSQL); // 执行插入语句
ResultSet res = stmt.executeQuery(querySQL); // 执行查询语句
while (res.next()) {
System.out.println("Result: key:"+res.getString(1) +" –> value:" +res.getString(2));
}
}
}
转自:http://blog.csdn.net/a221133/article/details/6734762
|
|