欢迎光临
我们一直在努力

java代码操作hive是怎样的

java代码操作hive是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

 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!
   使用java操作hive,用maven进行管理依赖 

 *     hive –service hiveserver -p 50000 &  

 <dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-jdbc</artifactId>

<version>0.9.0</version>

<exclusions>

<exclusion>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-core</artifactId>

</exclusion>

</exclusions>

</dependency>

   <dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-core</artifactId>

<version>0.20.2</version>

</dependency>

<dependency>

<groupId>org.apache.thrift</groupId>

<artifactId>libthrift</artifactId>

<version>0.8.0</version>

</dependency>

    <dependency>

<groupId>javax.jdo</groupId>

<artifactId>jdo2-api</artifactId>

<version>2.3-eb</version>

</dependency>

  </dependencies>

 */

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

     * 

     */

关于java代码操作hive是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注云搜网行业资讯频道了解更多相关知识。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。