网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码操作hive是怎样的

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

10年积累的成都网站设计、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有海城免费网站建设让你可以放心的选择与我们合作。

 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 &  

 

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

     * 

     */

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


分享题目:java代码操作hive是怎样的
本文URL:http://bjjierui.cn/article/goeogd.html

其他资讯