`

hive JAVA API

    博客分类:
  • HIVE
 
阅读更多
package com.hqgf.hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HiveJDBC {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://172.69.1.221:10000/hqgf";
    private static String user = "hive";
    private static String password = "";

    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;

    
    public static void main(String[] args) throws Exception {
    	init();
    	showDatabases();
//    	createDatabase();
    	createTable();
    	showTables();
//    	selectData();
//    	Thread.sleep(1000*60);
//    	loadData();
//    	descTable();
//    	deopTable();
//    	dropDatabase();
    	destory();
    	
	}
    
    
    // 加载驱动、创建连接
    public static void init() throws Exception {
        Class.forName(driverName);
        conn = DriverManager.getConnection(url,user,password);
        stmt = conn.createStatement();
    }

    // 创建数据库
    public static void createDatabase() throws Exception {
        String sql = "create database hqgf";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 查询所有数据库
    public static void showDatabases() throws Exception {
        String sql = "show databases";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }

    // 创建表
    public static void createTable() throws Exception {
        String sql = "create table bill(BILL_ID string,TEMPLATE_NAME string,RECORD_XML string) row format delimited fields terminated by ';'";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 查询所有表
    public static void showTables() throws Exception {
        String sql = "show tables";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }

    // 查看表结构
    public static void descTable() throws Exception {
        String sql = "desc bill";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + "\t" + rs.getString(2));
        }
    }

    // 加载数据
    public static void loadData() throws Exception {
    	SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
	    String filename = format.format(new Date()).concat(".txt");
        String filePath = "/usr/tools/".concat(filename);
        String sql = "load data local inpath '" + filePath + "' overwrite into table bill";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 查询数据
    public static void selectData() throws Exception {
        String sql = "select * from bill";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString("RECORD_XML"));
        }
    }

    // 统计查询(会运行mapreduce作业)
    public static void countData() throws Exception {
        String sql = "select count(1) from bill";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getInt(1) );
        }
    }

    // 删除数据库
    public static void dropDatabase() throws Exception {
        String sql = "drop database if exists hqgf";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 删除数据库表
    public static void deopTable() throws Exception {
        String sql = "drop table if exists bill";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 释放资源
    public static void destory() throws Exception {
        if ( rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

 

分享到:
评论

相关推荐

    hive metastore java api使用

    同样hive metastore也提供了java接口。 使用 import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; import org.apache.hadoop.hive.metastore.api.Field...

    hive-java开发驱动包

    hive java开发驱动包列表hive-common-2.3.4.jarhive-exec-2.3.4.jarhive-jdbc-2.3.4.jarhive-llap-client-2.3.4.jarhive-llap-common-2.3.4.jarhive-llap-server-2.3.4.jarhive-llap-tez-2.3.4.jarhive-metastore-...

    HiveAPI:用Java编写的Hive Rest API

    HiveAPI 用Java编写的Hive Rest API

    hadoop+hive+mapreduce的java例子

    基于hadoop的Hive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI CLI,即Shell命令行 JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似 Web...

    javaApi_sparkhiveAPI_hbaseAPI.zip

    2019-8____2019-10工作期间的java版hiveApi,hbaseApi和spark版本的hiveApi,hbaseApi

    java解决hive快速导数据到Hbase代码

    基于hadoop的,java实现hive快速导数据到Hbase解决方案。

    java访问hive数据库所需全部jar包

    内网开发的,不使用maven做项目管理的,直接拿来用,包括了所有需要的jar包(如基础包httpclient),hive是1.2版本的

    hive-metastore-2.3.6.jar

    在hive java api的时候 : 需要导入hive运行所需的jar包,导入jar包有两种方式可实现 1.使用maven依赖直接修改配置文件,以下为配置内容:(不推荐,会下载额外很多无用包,而且时间很长在1小时左右) 2.挑选必须...

    hive-storage-api-2.6.0.jar

    java运行依赖jar包

    java jdbc连接hive所需要所有jar包,请测有效。

    jdbc连接hive代码所需要jar包,测试有效

    Java通过JDBC连接Hive

    压缩包中包含Java通过JDBC连接Hive所需的jar包。包含有,antlr-runtime-3.0.1.jar;commons-logging-1.0.4.jar;hadoop-core-1.2.1.jar;hive-exec-0.10.0.jar;hive-jdbc-0.13.1.jar;hive-metastore-0.9.0.jar;hive-...

    java链接hive需要的jar

    antlr-runtime-3.0.1.jar hive-exec-0.7.1.jar hive-jdbc-0.7.1.jar hive-metastore-0.9.0.jar hive-service-0.9.0.jar jdo2-api-2.3-ec.jar libfb303.jar

    flink-connector-hive-2.11-1.12.7-API文档-中文版.zip

    标签:apache、flink、connector、hive、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    flink-connector-hive_2.11-1.10.0-API文档-中文版.zip

    标签:flink、11、apache、connector、hive_2、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,...

    tw-hive:学习Hive API

    ###这是一个学习hive的库 PS:Hive版本为0.10.0-cdh4.3.2 ###Tips 1.使用jdbc方式连接hiveserver,只能使用默认的数据库即default。官方文档说明如下: "jdbc:hive://localhost:10000/default". Currently, the only...

    flink-connector-hive_2.11-1.13.2-API文档-中英对照版.zip

    标签:apache、flink、connector、hive、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    flink-connector-hive-2.11-1.13.2-API文档-中文版.zip

    标签:apache、flink、connector、hive、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    hive函数参考手册,包含常用的hive内置函数.doc

    hive常用内置函数使用文档,类似于java的api doc文档

    JDBC_Hive.rar

    java连接jdbc使用到的13个jar包:commons-logging-1.0.4,hadoop_fat,hadoop-common-2.0.0-cdh4.7.0,hive-common-0.10.0-cdh4.7.0,hive-jdbc-0.10.0-cdh4.7.0,hive-metastore-0.10.0-cdh4.7.0,hive-service-0.10.0-...

    scala-hive-HBASE-Api.7z

    2019-08----2019-10工作期间所用到的hive和HBASE的jar包

Global site tag (gtag.js) - Google Analytics