三、hive--jdbc的使用
发表于:2024-10-05 作者:千家信息网编辑
千家信息网最后更新 2024年10月05日,一、jdbc连接mysql代码示例public class TestConnector { final static String USER = "root"; final static
千家信息网最后更新 2024年10月05日三、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依赖:
org.apache.hive hive-jdbc 1.2.1
代码:
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类似。
代码
数据
对象
版本
示例
这是
驱动
成功
名字
姓名
字段
字符
字符串
数据库
方法
类型
结果
中新
代理
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库只读事务
网络技术有哪8大类
山东省建筑网络安全教育
计算机网络技术专业看学历吗
汉滨区互联网科技
南大建设的数据库
软件开发工具什么用途
软件开发工作细分
怎么连接hsql数据库吗
滕讯服务器在哪
水量服务器需要做回水吗
麦粒软件开发有限公司
涉密系统软件开发业务
网络技术可以转行开发
网络安全风险和管控目标
怎样修改数据库中的应用锁
招商银行软件开发员薪酬
张家口网络技术有限公司
昆山纬视晶光电软件开发
终焉誓约什么时候能进服务器
php数据库写在哪里
梦幻诛仙手游服务器管理文件
千时计算机软件开发工作室
合肥高新区网络安全会展
管理应用服务器
畅捷通数字服务器没有响应
设计数据库的表结构数学建模
网络安全的小学生画
广州悦恒互联网科技
电力行业网络安全的重要性