In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.