Get the App
SLTechnology News&Howtos  ›  Servers  › 

How java code manipulates hive

Shulou Source: shulou.com Published: 2022-06-01 00:26:46 09月20日 Update

How does java code operate hive? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Package cn.com.hivedemo.hivedemo

Import java.sql.Connection

Import java.sql.DriverManager

Import java.sql.ResultSet

Import java.sql.SQLException

Import java.sql.Statement

Import org.apache.hadoop.hive.jdbc.HiveDriver

/ * *

* Hello world!

Use java to operate hive and maven to manage dependencies

* hive-- service hiveserver-p 50000 &

Org.apache.hive

Hive-jdbc

0.9.0

Org.apache.hadoop

Hadoop-core

Org.apache.hadoop

Hadoop-core

0.20.2

Org.apache.thrift

Libthrift

0.8.0

Javax.jdo

Jdo2-api

2.3-eb

, /

Public class App

{

Public static void main (String [] args) throws Exception

{

System.out.println ("Hello World!")

/ / createTableDemo ()

/ / insertTableDemo ()

/ / selectDemo ()

CountDemo ()

ShowTablesDemo ()

}

Private static final String URLHIVE = "jdbc:hive://hadoop0:50000/default"

Private static Connection connection = null

Public static Connection getHiveConnection () {

If (null = = connection) {

Synchronized (HiveDriver.class) {

If (null = = connection) {

Try {

Class.forName ("org.apache.hadoop.hive.jdbc.HiveDriver")

Connection = DriverManager.getConnection (URLHIVE, ",")

} catch (SQLException e) {

E.printStackTrace ()

} catch (ClassNotFoundException e) {

E.printStackTrace ()

}

}

}

}

Return connection

}

Public static void createTableDemo () throws SQLException {

String tweetTableSql = "create TABLE IF not EXISTS student1 (name String,age int)"

System.out.println ("= createTableDemo begin=")

Statement stmt = getHiveConnection () .createStatement ()

Stmt.execute (tweetTableSql)

Stmt.close ()

System.out.println ("= createTableDemo end=")

}

Public static void insertTableDemo () throws SQLException {

String tweetTableSql = "LOAD DATA LOCAL INPATH'/ opt/stu.txt' OVERWRITE INTO TABLE student1"

System.out.println ("= insertTableDemo begin=")

Statement stmt = getHiveConnection () .createStatement ()

Stmt.execute (tweetTableSql)

Stmt.close ()

System.out.println ("= insertTableDemo end=")

}

Public static void selectDemo () throws SQLException {

String tweetTableSql = "select * from student1"

System.out.println ("= selectDemo begin=")

Statement stmt = getHiveConnection () .createStatement ()

ResultSet rs = stmt.executeQuery (tweetTableSql)

While (rs.next ()) {

System.out.println (rs.getString (1) + rs.getString (2))

}

System.out.println ("= selectDemo end=")

}

Public static void selectDemo1 () throws SQLException {

String tweetTableSql = "select * from student1 where name='MM'"

System.out.println ("= selectDemo begin=")

Statement stmt = getHiveConnection () .createStatement ()

ResultSet rs = stmt.executeQuery (tweetTableSql)

While (rs.next ()) {

System.out.println (rs.getString (1) + rs.getString (2))

}

System.out.println ("= selectDemo end=")

}

Public static void countDemo () throws SQLException {

String tweetTableSql = "select count (*) from student1"

System.out.println ("= countDemo begin=")

Statement stmt = getHiveConnection () .createStatement ()

ResultSet rs = stmt.executeQuery (tweetTableSql)

While (rs.next ()) {

System.out.println (rs.getString (1))

}

System.out.println ("= countDemo end=")

}

Public static void showTablesDemo () throws SQLException {

String tweetTableSql = "show tables"

System.out.println ("= showTablesDemo begin=")

Statement stmt = getHiveConnection () .createStatement ()

ResultSet rs = stmt.executeQuery (tweetTableSql)

While (rs.next ()) {

System.out.println (rs.getString (1))

}

System.out.println ("= showTablesDemo end=")

}

/ * *

* JDK uses 1.6.0.x

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/hadoop/hive/metastore/api/MetaException: Unsupported major.minor version 51.0

Change it to 1.8, then OK.

*

, /

The answer to the question about java code operation hive is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Questions codes more help answers easy easy content guys partners methods knowledge articles industry information information channels channels analysis management Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Redmi vpn OPPO Reno