III. The use of hive--jdbc
Jdbc connection mysql code example 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"; / / this is a new driver for mysql.connector in version 8.x, and com.mysql.jdbc.Driver has deprecated final static String DRIVER = "com.mysql.cj.jdbc.Driver" in this version Public static void main (String [] args) {Connection connection = null; Statement statement = null; ResultSet resultSet = null; try {/ / load driver class Class.forName (DRIVER); / / get connection object connection = DriverManager.getConnection (URL, USER, PASSWORD) / / get the connected proxy object, which is used to pass the sql request statement = connection.createStatement (); boolean result = statement.execute ("show databases;"); resultSet = statement.getResultSet () / / resultSet.next () to see whether there is any data in the returned result. If there are many acquisition methods for true while (resultSet.next ()) {/ / resultSet, getInt,getString, etc., depending on the data type System.out.println (resultSet.getString ("Database")) of the field. } 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 ();} II. Jdbc connection hive code example
First add maven dependencies:
Org.apache.hive hive-jdbc 1.2.1
Code:
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 {/ / this is the driver name of hive private static String driverName= "org.apache.hive.jdbc.HiveDriver"; / / the database connection string 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 ("number" + "\ t" + "name"); 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 ("whether the table was deleted successfully:" + bool);} @ After public void destory () throws Exception {if (rs! = null) {rs.close ();} if (stmt! = null) {stmt.close ();} if (conn! = null) {conn.close () }}}
The basic use is similar to jdbc connecting to mysql.