java代码导入hive_java代码操作hive

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

使用java操作hive,用maven进行管理依赖

*     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使用1.6.0.x

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

换成1.8就OK

*

*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值