默认开启10000端口
开启前,编辑hive-site.xml设置impersonation,防止hdfs权限问题,这样hive server会以提交用户的身份去执行语句,如果设置为false,则会以起hive server daemon的admin user来执行语句
<property>
<name>hive.server2.enable.doAs</name>
<value>false</value>
</property>

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.</modelVersion> <groupId>xinwei</groupId>
<artifactId>Hive</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>Hive</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>0.14.</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.4.</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
</project>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//Hive0.11.0版本提供了一个全新的服务:HiveServer2,这个很好的解决HiveServer存在的安全性、并发性等问题。这个服务启动程序在${HIVE_HOME}/bin/hiveserver2里面,你可以通过下面的方式来启动HiveServer2服务:
//nohup ./hive --service hiveserver2 >/dev/null 2>1 &
//默认开启10000端口
//开启前,编辑hive-site.xml设置impersonation,防止hdfs权限问题,这样hive server会以提交用户的身份去执行语句,如果设置为false,则会以起hive server daemon的admin user来执行语句
public class HiveTest {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://192.168.231.137:10000/default";
private static String username = "hadoop";
private static String password = "hadoop";
private static Connection conn = null;
private static Statement stmt = null;
private static String sql = "";
private static ResultSet res = null;
static {
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) throws Exception {
dropTable("hivetest");
createTable("hivetest");
showTables("hivetest");
describeTables("hivetest");
insert("hivetest", new String[]{"","tom",""});
insert("hivetest", new String[]{"","zhangshan",""});
insert("hivetest", new String[]{"","lisi",""});
insert("hivetest", new String[]{"","lucy",""});
selectData("hivetest");
// dropTable("hivetest");
} // 查询数据
public static void selectData(String tableName) throws SQLException {
sql = "select * from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getInt() + "\t" + res.getString());
}
} // 添加数据
public static void insert(String tableName, String[] datas) throws SQLException {
sql = "insert into table " + tableName + " values ('" + datas[] + "','" + datas[] + "'," + Integer.valueOf(datas[]) + ")";
stmt.execute(sql);
} // 查询表结构
public static void describeTables(String tableName) throws SQLException {
sql = "describe " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString() + "\t" + res.getString());
}
} // 查看表
public static void showTables(String tableName) throws SQLException {
sql = "show tables '" + tableName + "'";
res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString());
}
} // 创建表
public static void createTable(String tableName) throws SQLException {
sql = "create table " + tableName + " (id string, name string,age int) row format delimited fields terminated by '\t'";
stmt.execute(sql);
} // 删除表
public static String dropTable(String tableName) throws SQLException {
// 创建的表名
sql = "drop table " + tableName;
stmt.execute(sql);
return tableName;
}
} //
//hivetest
//id string
//name string
//age int
//10000 tom
//10001 zhangshan
//10002 lisi
//10003 lucy

hive javaapi 002的更多相关文章

  1. 三、hive JavaAPI示例

    在上文中https://www.cnblogs.com/lay2017/p/9973370.html 我们通过hive shell去操作hive,本文我们以Java代码的示例去对hive执行加载数据和 ...

  2. 调用javaAPI访问hive

    jdbc远程连接hiveserver2 2016-04-26 15:59 本站整理 浏览(425)     在之前的学习和实践Hive中,使用的都是CLI或者hive –e的方式,该方式仅允许使用Hi ...

  3. Hive笔记--sql语法详解及JavaAPI

    Hive SQL 语法详解:http://blog.csdn.net/hguisu/article/details/7256833Hive SQL 学习笔记(常用):http://blog.sina. ...

  4. hive创建索引

    索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...

  5. hive导入数据

    替换分隔符为\ sed -i 's/\t/\x1/g;s/;/\x1/g' test1.txt gz压缩 gzip -r test1.txt 查看文件 hdfs dfs -ls /hive/wareh ...

  6. hive学习3(hive基本操作)

    hive基本操作 hive的数据类型 1)基本数据类型 TINYINT,SMALLINT,INT,BIGINT FLOAT/DOUBLE BOOLEAN STRING 2)复合类型 ARRAY:一组有 ...

  7. Hive DDL DML SQL操作

    工作中经常要用到的一些东西,一直没整理,用的多的记住了,用的不多的每次都是去查,所以记录一下. DDL(数据定义语言),那就包括建表,修改表结构等等了 建表:create hive table hiv ...

  8. Hive(四):c#通过odbc访问hive

    hive odbc 驱动配置成功后,通过c#访问就变得比较简单了,分为查询与更新操作,直接附上测试代码.在此过程中需要注意c#工程编译的目标平台 读写访问代码示例: public class Hive ...

  9. Hive[4] 数据定义 HiveQL

    HiveQL 是 Hive 查询语言,它不完全遵守任一种 ANSI SQL 标准的修订版,但它与 MySQL 最接近,但还有显著的差异,Hive 不支持行级插入,更新和删除的操作,也不支持事务,但 H ...

随机推荐

  1. day4:数据结构list

    1,一直输入用户名,输入Q退出,注意用户的输入别忘了加strip,和upper不区分大小写,list最后一位添加append li = [] while 1: name = input("& ...

  2. 抽屉之Tornado实战(5)--点赞与评论树

    点赞 点赞的过程:数字增加,并在后台点赞表记录数据 需要发过去的数据:用户id,新闻id 用户id从session里获得,那新闻id怎么获取呢?这想到分页是循环新闻列表来展示内容,循环的新闻id可以做 ...

  3. kubernetes微服务部署

    1.哪些服务适合单独成为一个pod?哪些服务适合在一个pod中? message消息服务被很多服务调用   单独一个pod dubbo服务和web服务交互很高放在同一个pod里 API网关调用很多服务 ...

  4. Asp.net Mvc post表单提交多个实体模型

    上一遍说道用Tuple实现Asp.net Mvc action返回多个模型实体给view,此篇发过来,实现view表单提交多个实体模型到action. 1.view代码: @{ Layout = nu ...

  5. Innodb buffer 相关参数

    buffer相关参数: show GLOBAL VARIABLES LIKE 'innodb_buffer_pool_instances'; show GLOBAL VARIABLES LIKE 'i ...

  6. 第三节:Windows下检出项目和提交项目

    1.将项目下载到本地: 2.在客户端中,右键点击test,选择show in explorer: 然后修改日志文件并保存: 3.在客户端中填写备注并提交: 4.提交到github中:

  7. loadrunner压测工具

    https://www.cnblogs.com/xxyBlogs/p/4579509.html https://www.cnblogs.com/jaazz/p/8668214.html

  8. 教你一步步composer安装Magento2.3

    以前外贸建站一直用zencart,这段时间ytkah比较有时间,就决定用magento来创建一下站点.magento不像普通的程序一样下载就可以直接安装,需要借助composer安装,还没没compo ...

  9. android仿支付宝输入车牌号

    这个是iOS的效果图,差异不大,楼主主攻OC,见谅 需要用到的xml文件 需要用到的类 number_or_letters.xml <?xml version="1.0" e ...

  10. poi api工具

    ------------官网api地址 http://poi.apache.org/apidocs/index.html ------------官方下载地址 http://poi.apache.or ...