参考https://www.iteblog.com/archives/846.html

1、hive依赖hadoop,将hdfs当作文件存储介质,那是否意味着hive需要知道namenode的地址?

实际上在hive的hive-env.sh 中配置了 HADOOP_HOME=/home/install/hadoop-2.5.

2、hive的本地模式和远程模式有什么区别?

hive本质上是将sql语法解析为mapreduce的过程,既然如此它就必须提交mapreduce任务到resoucemanager,那么它如何提交?就是通过hadoop提供的命令hadoop jar命令来提交。

本地模式:简单的理解,hive客户端仅供本地使用,直接使用hive命令,不需要指定IP 端口

远程模式:简单的理解,将hive发布成一个服务进程,通过hive --service hiveserver命令,那么其他hive客户端就可以连接hive的服务进程

其他客户端可以是jdbc方式、hive提供的beeline命令等,既然要连接远端的hive服务进程,那自然需要指定 IP 端口,这里的IP指的是hive服务进程所在的IP,端口自然也是,也自然与hadoop无关。所以不要混淆

HiveServer2提供了一个新的命令行工具Beeline,它是基于SQLLine CLI的JDBC客户端。

Beeline工作模式有两种,即本地嵌入模式和远程模式。嵌入模式情况下,它返回一个嵌入式Hive(类似于Hive CLI)。而远程模式则是通过Thrift协议与某个单独的HiveServer2进程进行连接通信。

hive的三种连接方式

1、hive 命令行模式,直接输入/hive/bin/hive的执行程序,或者输入 hive --service cli
用于linux平台命令行查询,查询语句基本跟mysql查询语句类似
2、 hive web界面的 (端口号9999) 启动方式
hive –service hwi &
       用于通过浏览器来访问hive,感觉没多大用途
3、 hive 远程服务 (端口号10000) 启动方式
hive --service hiveserver &
或者
hive --service hiveserver 10000>/dev/null 2>/dev/null &
    beeline方式连接:beeline -u jdbc:hive2//localhost:10000/default -n root -p 123456
    或者
    java client方式连接
备注:
连接Hive JDBC URL:jdbc:hive://192.168.6.116:10000/default (Hive默认端口:10000 默认数据库名:default)

第一步:开启hive 远程服务

bin/hive --service hiveserver -p 10002
   Starting Hive Thrift Server
第二步:添加依赖
 <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.7.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.7.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.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-metastore -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.2.1</version>
    </dependency>
   
 
第三步:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager; public class HiveJdbcTest { 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);
} Connection con = DriverManager.getConnection(
"jdbc:hive2://localhost:10002/default", "wyp", "");
Statement stmt = con.createStatement();
String tableName = "wyphao";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName +
" (key int, value string)");
System.out.println("Create table success!");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
} // describe table
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));
} sql = "select * from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t"
+ res.getString(2));
} sql = "select count(1) from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
}
}

Hive:用Java代码通过JDBC连接Hiveserver的更多相关文章

  1. 用Java代码通过JDBC连接Hiveserver2

    1.在终端启动hiveserver2#hiveserver2 2.使用beeline连接hive另外打开一个终端,输入如下命令(xavierdb必须是已经存在的数据库)#beeline -u jdbc ...

  2. java代码实现JDBC连接MySql以及引用驱动程序包

    JDBC链接MySql     JDBC链接MySql的话题已经老掉牙了,这次我只想通过使用简洁的代码实现,采用封装的思想,将链接MySql的代码封装在类的静态方法中,供一次性调用返回java.sql ...

  3. Java基础93 JDBC连接MySQL数据库

    本文知识点(目录): 1.什么是jdbc     2.jdbc接口的核心API     3.使用JDBC技术连接MySQL数据库的方法    4.使用Statement执行sql语句(DDL.DML. ...

  4. 【JDBC】java程序通过jdbc连接oracle数据库方法

    版权声明:本文为博主原创文章(原文:blog.csdn.net/clark_xu 徐长亮的专栏).未经博主同意不得转载. https://blog.csdn.net/u011538954/articl ...

  5. 大数据系列-java用官方JDBC连接greenplum数据库

    这个其实非常简单,之所以要写此文是因为当前网上搜索到的文章都是使用PostgreSQL的驱动,没有找到使用greenplum官方驱动的案例,两者有什么区别呢? 一开始我也使用的是PostgreSQL的 ...

  6. java 命令行JDBC连接Mysql

    环境:Windows10 + java8 + mysql 8.0.15 + mysql-connector-java-8.0.15.jar mysql驱动程序目录 项目目录 代码: //package ...

  7. Java是用JDBC连接MySQL数据库

    首先要下载Connector/J地址:http://www.mysql.com/downloads/connector/j/ 这是MySQL官方提供的连接方式: 解压后得到jar库文件,需要在工程中导 ...

  8. java代码获取jdbc链接properties

    public static String getDirPath() { Resource resource = null; Properties props = null; String driver ...

  9. 第3节 sqoop:7、通过java代码远程连接linux执行shell命令

    数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...

随机推荐

  1. 面试题之实现系统函数系列一:实现memmove函数

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [算法编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  2. opencv python基本操作

    Python usage crop frame: croppedframe = frame[ymin:ymax, xmin:xmax] resize frame: reszframe = cv2.re ...

  3. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  4. 网站页面SEO的三个标签怎么写有利【转载】

    转载自:代明博客 在SEO界,自从夫唯老师提出“四处一词”的概念以来,不管是搜索引擎还是SEOer,都格外重视页面的三个标签.三个标签书写是否成功,在很大程度上决定了网页是否能有好的排名.今天代明博客 ...

  5. BC-NFS部署报错节点间磁盘wwid冲突

    1.在部署公司的BC-NFS产品时,填好IP选好磁盘后,点击“创建”,就抛出如下提示: 2.关闭虚机,再打开虚机的控制台,点击磁盘,自定义serial number 3.启动虚机,再次登陆,查看磁盘w ...

  6. centos系统服务管理

    系统服务管理工具: chkconfig(所有linux发行版都有),用法很简单,如下: usage:   chkconfig --list [name]          chkconfig --ad ...

  7. JSON字符串转对象

    JSON(JavaScript Object Notation) JavaScript对象标记法:JSON是与JavaScript高度契合的:JSON 语法:    --数组(Array)用" ...

  8. ubuntu16.04 安装composer和 laravel

    一.安装composer $ sudo apt-get update $ sudo apt-get install wget 下载composer.phar $ wget https://getcom ...

  9. ubuntu下做柯老师lab19-lab20实验问题总结

    前两篇文章告诉了大家如何将无线封包传输遗失模型和myevalvid添加到ns2.35中,已经成功验证了,这个没有问题.但是本人在做lab19和lab20实验时又发现了一些关于myevalvid工具集的 ...

  10. Codeforces 1099 B. Squares and Segments-思维(Codeforces Round #530 (Div. 2))

    B. Squares and Segments time limit per test 1 second memory limit per test 256 megabytes input stand ...