重要:在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口。使用下面命令进行开启:hive -service hiveserver &
1). 测试数据
userinfo.txt文件内容(每行数据之间用tab键隔开):
1   xiapi2    xiaoxue3    qingqing
2). 程序代码
 packagecom.ljq.hive;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;
public class HiveJdbcClient {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private staticString url = "jdbc:hive://192.168.11.157:10000/default";
private staticString user = "hive";
private static String password = "mysql";
private staticString sql = "";
private static ResultSet res;
private static finalLogger log = Logger.getLogger(HiveJdbcClient.class);
public static voidmain(String[] args) {
try {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,user, password);
Statement stmt =conn.createStatement();
// 创建的表名
String tableName ="testHiveDriverTable";
/** 第一步:存在就先删除 **/
sql = "drop table" + tableName;
stmt.executeQuery(sql);
/** 第二步:不存在就创建 **/
sql = "createtable " + 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("执行“showtables”运行结果:");
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("执行“describetable”运行结果:");
while (res.next()) {
System.out.println(res.getString(1) + "\t" +res.getString(2));
}
// 执行“load data into table”操作
String filepath ="/home/hadoop/ziliao/userinfo.txt";
sql = "load datalocal 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 = "selectcount(1) from " + tableName;
System.out.println("Running:" + sql);
res =stmt.executeQuery(sql);
System.out.println("执行“regularhive 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);
}
}
}
3). 运行结果(右击-->Run as-->Run on Hadoop)
Running:show tables'testHiveDriverTable'执行“show tables”运行结果:
testhivedrivertable
Running:describe testHiveDriverTable
执行“describe table”运行结果:
key   intvalue    string
Running:load data local inpath'/home/hadoop/ziliao/userinfo.txt' into table testHiveDriverTable
Running:select * fromtestHiveDriverTable
执行“select * query”运行结果:1    xiapi2    xiaoxue3   qingqing
Running:select count(1) fromtestHiveDriverTable
执行“regular hive query”运行结果:3

Hive和Jdbc示例的更多相关文章

  1. Hive:JDBC示例

    1)本地目录/home/hadoop/test下的test4.txt文件内容(每行数据之间用tab键隔开)如下所示: [hadoop@master test]$ sudo vim test4.txt ...

  2. Hive的JDBC使用&并把JDBC放置后台运行

    使用JDBC访问HIVE: 首先启动hive的JDBC服务. 进入hive的bin目录: 这样启动是启动到前台.如果 要想启动到后台需要用到Linux的相关命令. 我们先把其放到前台看下效果,之后再把 ...

  3. Apache Hive处理数据示例

    继上一篇文章介绍如何使用Pig处理HDFS上的数据,本文将介绍使用Apache Hive进行数据查询和处理. Apache Hive简介 首先Hive是一款数据仓库软件 使用HiveQL来结构化和查询 ...

  4. [Hive_5] Hive 的 JDBC 编程

    0. 说明 Hive 的 JDBC 编程 1. hiveserver2 介绍 hiveserver2 是 Hive 的 JDBC 接口,用户可以连接此端口来连接 Hive 服务器 JDBC 驱动类为 ...

  5. Hive的JDBC

    Hive 的JDBC 包含例子 https://cwiki.apache.org/confluence/display/Hive/HiveClient#HiveClient-JDBC HiveServ ...

  6. hive安装 jdbc链接hive

    1. 下载hive安装包 2. 进入 conf 中  :  cp hive-default.xml.template hive-site.xml,  vi hive-site.xml 1)首行添加: ...

  7. Spring JDBC 示例

    在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常, ...

  8. hive的jdbc使用

    ①新建maven项目,加载依赖包  在pom.xml中添加 <dependency> <groupId>jdk.tools</groupId> <artifa ...

  9. SPARK_sql加载,hive以及jdbc使用

    sql加载 格式  或者下面这种直接json加载 或者下面这种spark的text加载 以及rdd的加载 上述记得配置文件加入.mastrt("local")或者spark://m ...

随机推荐

  1. WildFly8.1(JBoss)+mod_cluster(Apache)群集配置

    继上次使用mod_jk传导Apache+JBoss群集配置后,.因为JBoss5.1启动太慢,于是我开始尝试用最新的WildFly8.1构造(WildFly那是,JBoss.在JBoss7之后改名). ...

  2. 检索算法 -- 数据结构与算法的javascript描述 第13章

    检索算法-如何在列表中查找特定的值. 顺序查找 从列表的第一个元素开始对列表元素逐个进行判断,直到找到了想要的结果,它属于暴力查找技巧的一种,在执行查找时可能会访问到数据结构里的所有元素. 代码: / ...

  3. Semantic UI基础使用教程

    自己今后要使用Semantic UI进行项目开发了,一步步的记录下来,供大家参考,也让自己去简单的学习一下,有空了就会更新一点东西,大家有什么问题可以相互交流一下,文采不是很好,希望大家要多多见谅,这 ...

  4. MVC理解

    1:MVC 中的@是什么意思?   类似于<% %>只不过它没有闭合的,这是MVC3.0的新特性2:关于ASP.NET MVC的Html.BeginForm()方法Html.BeginFo ...

  5. mysql安装常见问题(系统找不到指定的文件、发生系统错误 1067 进程意外终止)

    在安装mysql时总是会遇到这样那样的问题,每次重新安装都会花很多时间来排查.在网上其实有很多相关的文章,但很多都只讲了方法,但没讲具体细节问题,导致无法解决问题.其实有时候知道问题的原因,但总是因为 ...

  6. ORA-04031: 无法分配 共享内存

    今天现场项目oracle系统定时器插入数据报错: --ORA-04031: 无法分配 3936 字节的共享内存 ("shared pool","truncate tabl ...

  7. source、sh、bash、./执行脚本的区别

    1.source命令用法: source FileName 作用:在当前bash环境下读取并执行FileName中的命令.该filename文件可以无"执行权限" 注:该命令通常用 ...

  8. KMP字符串匹配

    #include<iostream> using namespace std; #define MAX 255 typedef unsigned char BYTE; typedef BY ...

  9. 区分IE9/IE8/IE7/IE6及其他浏览器-CSS hack

    记录一下这些浏览器的hack如下: 一.IE9以及以下版本浏览器 对于IE8及其以下版本的浏览器,就是使用本文标题所提到的”\9″ hack.如下代码: .ie8_9{ color:blue; /*所 ...

  10. linux select函数 shutdown函数

    #include<sys/select.h> #include<sys/time.h> int select(int maxfdp1,fd_set *readset,fd_se ...