久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java代碼操作hive是怎樣的

156次閱讀
沒有評論

共計 4234 個字符,預計需要花費 11 分鐘才能閱讀完成。

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 是怎樣的問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注丸趣 TV 行業資訊頻道了解更多相關知識。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-25發表,共計4234字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 肥乡县| 郑州市| 唐山市| 县级市| 获嘉县| 焉耆| 长寿区| 江源县| 垫江县| 乐至县| 绵竹市| 灌阳县| 阿克| 嘉鱼县| 开原市| 三都| 岑巩县| 龙泉市| 象山县| 武清区| 龙南县| 泸西县| 临潭县| 板桥市| 叶城县| 杭州市| 武平县| 尼木县| 腾冲县| 鄱阳县| 威海市| 青岛市| 昌图县| 怀安县| 冀州市| 乐至县| 梧州市| 中卫市| 泗洪县| 佛山市| 高青县|