码迷,mamicode.com
首页 > 数据库 > 详细

HiveJdbc

时间:2015-11-11 10:11:18      阅读:228      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!