hive

第一步、在/home/hadoop/cdh/test下创建原始数据

touch stu
vi stu
1    zhangsan
2    lisi

第二步、进入hive目录,启动hive服务

bin/hive --service hiveserver -v
Starting Hive Thrift Server
Starting hive server on port 10000 with 100 min worker threads and 2147483647 max worker threads

第三步、在hive里面创建一张表

hive> create table stu (id int, name string) row format delimited  fields terminated by '\t'
stored as textfile;

第四步、编写Java程序,并运行。

public class Demo01_hive_and_mysql {

    private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";    
    
    public static void main(String[] args) throws Exception {

        System.setProperty("hadoop.home.dir", "E:\\cdh\\hadoop-2.5.0-cdh5.3.6");

        try {
             Class.forName(driverName);
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
             System.exit(1);
         }
            //此处的用户名一定是有权限操作HDFS的用户,否则程序会提示"permission deny"异常
        Connection con = DriverManager.getConnection("jdbc:hive://192.168.2.133:10000/default", "user", "password");
                
        Statement stmt = con.createStatement();
        String tableName = "stu";
        String sql = "";
        ResultSet res = null;
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName + " (key int, value string)");
        System.out.println("Create table success!");
        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));
        }
        
        sql = "select * from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println("-------------------");
            System.out.println(res.getString(2)+"\t"+res.getString(1));
        }

结果显示

Running: describe stu
id                      int                 
name                    string              
Running: select * from stu
-------------------
zhangsan     1
-------------------
lisi         2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 介绍 Hive是运行在Hadoop之上的数据仓库,将结构化的数据文件映射为一张数据库表,提供简单类SQL查询语言,...
    syncwt阅读 4,755评论 0 7
  • HIVE是一个基于Hadoop的数据仓库,适用于一些高延迟性的应用。如果对延迟性要求比较高,则可以选择Hbase。...
    夏无忧阳阅读 5,130评论 0 12
  • 个人主页:http://www.linbingdong.com 简介 本文主要记录如何安装配置Hive on Sp...
    Jeffbond阅读 23,796评论 22 30
  • 1.基础环境## 和上一篇 hadoop 相同 http://www.jianshu.com/p/57ffe2e3...
    灼灼2015阅读 2,394评论 0 4
  • 日语韩语在发音还是语法上有很多相似,所以很多时候学了韩语的人会学习日语。久而久之韩语和日语也总有一些比较,都说日语...
    韩语小助手阅读 447评论 0 2