三、hive--jdbc的使用

一、jdbc连接mysql代码示例

public class TestConnector {
    final static String USER = "root";
    final static String PASSWORD = "wjt86912572";
    final static String URL = "jdbc:mysql://bigdata121:3306/metastore?useSSL=false&serverTimezone=UTC&useUnicode=true";
    //这是 mysql.connector 在8.x版本中新驱动,com.mysql.jdbc.Driver在此版本中已弃用
    final static String DRIVER = "com.mysql.cj.jdbc.Driver";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //加载驱动类
            Class.forName(DRIVER);
            //获取连接对象
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            //获取连接的代理对象,用于传递sql请求
            statement = connection.createStatement();
            boolean result = statement.execute("show databases;");
            resultSet = statement.getResultSet();

            //resultSet.next()看返回的结果是否还有数据,如果有为true
            while (resultSet.next()) {
                //resultSet提供了很多获取的方法,getInt,getString等,具体用哪个,看字段的数据类型
                System.out.println(resultSet.getString("Database"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {resultSet.close();}
                if (statement != null) {statement.close();}
                if (connection != null) {connection.close();}
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
}

二、jdbc连接hive代码示例

首先添加maven依赖:

<!-- 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>

代码:

package com.zy.hivejdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HiveJDBC {

    //这是hive的driver名字
    private static String driverName="org.apache.hive.jdbc.HiveDriver";
    //数据库连接字符串
    private static String url = "jdbc:hive2://192.168.134.153:10000/mydb";
    private static String user = "root";
    private static String password="root";

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

    @Before
    public void init() throws Exception{
        Class.forName(driverName);
        conn = DriverManager.getConnection(url, user, password);
        stmt = conn.createStatement();
    }
    @Test
    public void createDatabase() throws Exception{
        String sql = "create database hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.executeQuery(sql);
    }
    @Test
    public void dropDatabase() throws Exception {
        String sql = "drop database if exists hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    @Test
    public void showDatabases() throws Exception {
        String sql = "show databases";
        System.out.println("Running: " + sql + "\n");
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) );
        }
    }

    @Test
    public void createTable() throws Exception {
        String sql = "create table t2(id int ,name String) row format delimited fields terminated by ‘,‘;";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    @Test
    public void loadData() throws Exception {
         String filePath = "/usr/tmp/student";
         String sql = "load data local inpath ‘" + filePath + "‘ overwrite into table t2";
         System.out.println("Running: " + sql);
         stmt.execute(sql);
    }

    @Test
    public void selectData() throws Exception {
        String sql = "select * from t2";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        System.out.println("编号" + "\t" + "姓名" );
        while (rs.next()) {
            System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
        }
    }
    @Test
    public static void drop(Statement stmt) throws Exception {
        String dropSQL = "drop table t2";
        boolean bool = stmt.execute(dropSQL);
        System.out.println("删除表是否成功:" + bool);
        }
    @After
    public void destory() throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}

基本使用和jdbc连接mysql类似。

原文地址:https://blog.51cto.com/kinglab/2447323

时间: 2024-11-08 23:35:29

三、hive--jdbc的使用的相关文章

Hive JDBC——深入浅出学Hive

第一部分:搭建Hive JDBC开发环境 搭建:Steps ?新建工程hiveTest ?导入Hive依赖的包 ?Hive  命令行启动Thrift服务 ?hive --service hiveserver & 第二部分:基本操作对象的介绍 Connection ?说明:与Hive连接的Connection对象 ?Hive 的连接 ?jdbc:hive://IP:10000/default" ?获取Connection的方法 ?DriverManager.getConnection(&q

hive jdbc 调用

HIVE学习总结 Hive只需要装载一台机器上,可以通过webui,console,thrift接口访问(jdbc,odbc),仅适合离线数据分析,降低数据分析成本(不用编写mapreduce). Hive优势 1.      简单易上手,类sql的hql. 2.      有大数据集的计算和扩展能力,mr作为计算引擎,hdfs作为存储系统 3.      统一的元数据管理(可与pig.presto)等共享 Hive缺点 1.      Hive表达能力有限.迭代和复杂运算不易表达 2.    

hive jdbc connection refused

hive jdbc 连接时抛异常: Exception in thread "main" java.sql.SQLException: Could not open client transport with JDBC Uri: jdbc:hive2://192.168.206.128:10000/default: java.net.ConnectException: Connection refused: connect     at org.apache.hive.jdbc.Hiv

HIVE JDBC连接详解

package org.conan.myhadoop.mr; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class HiveJDBCConnection {     private static String driverName = "or

Hive JDBC 操作 例子

pom.xml配置 <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>0.13.1</version> </dependency> 测试例程 1 import org.junit.Test; 2 3 import java.sql.SQLException; 4 import j

Hive JDBC 连接hiveserver2

1.启动hiveserver2 nohup /home/hadoop/hive-1.1.0-cdh5.5.2/bin/hiveserver2 >> /home/hadoop/gtq_dir/logs/hiveserver.log 2>&1 & 2.代码如下: package cn.hive; import java.sql.*; /** * Created by jieyue on 2017/12/18. */ public class HiveJdbcTest { pr

Hive JDBC:java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate anonymous

今天使用JDBC来操作Hive时,首先启动了hive远程服务模式:hiveserver2 &(表示后台运行),然后到eclipse中运行程序时出现错误: java.sql.SQLException: Could not open client transport with JDBC Uri: jdbc:hive2://192.168.182.11:10000/default: Failed to open new session: java.lang.RuntimeException: org.

Hive 8、Hive2 beeline 和 Hive jdbc

1.Hive2 beeline  Beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式 启动beeline 打开两个Shell窗口,一个启动Hive2 一个beeline连接hive2 #启动HiverServer2 , ./bin/hiveserver2 [[email protected] ~]# hiveserver2 16/02/23 22:55:25 WARN conf.HiveConf: HiveConf of name hive.metastore.local

javaweb三、JDBC访问数据库

JDBC是J2SE的内容,是由java提供的访问数据库的接口,但没有提供具体的实现方法,需要数据库厂商提供,就是对应的数据库驱动. 这样的好处是可以方便的更换数据库,提高了扩展性.这也是面向接口编程的一个优点. 1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.St

hive jdbc 例子及遇到问题

呼呼,解决hive的jdbc问题花了将近一天,而且解决办法竟然是这么的简单 遇到问题 select * from flag where 1 =1 and cust_no = 'A3325221981121080410' limit 5java.sql.SQLException: Error while processing statement: FAILED: RuntimeException org.apache.hadoop.security.AccessControlException: