首页 > 数据库技术 > 详细

HiveJdbc

时间:2015-11-11 10:08:47      阅读:262      评论:0      收藏:0      [点我收藏+]

Jdbc访问需要启动server服务:
hive --service hiveserver

package cn.sniper.hive.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveJdbc {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";

      /**
     * @param args
     * @throws SQLException
       */ 
    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        
        Connection con = DriverManager.getConnection("jdbc:hive2://hadoop20:10000/d1", "", "");
        Statement stmt = con.createStatement();
        
        String tableName = "t_user1";
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName + " (userid int, name string)");
        
        // show tables
        String sql = "show tables ‘" + tableName + "‘";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
          System.out.println(res.getString(1));
        }

        sql = "describe " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1) + "\t" + res.getString(2));
        }
    }
}


HiveJdbc

原文:http://my.oschina.net/sniperLi/blog/528758

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!