hive java 实例

下载  jdo2-api-2.3-ec hive hdfs 所需jar 

http://download.csdn.net/download/knight_black_bob/9725194

常见命令

hive 常见命令
create table test(uid string,name string)row format delimited fields terminated by '/t' 见表语句
desc formatted test;        表的约束  
desc  test;        表的约束  
LOAD DATA local INPATH '/root/test3.log' OVERWRITE INTO TABLE test;  本地文件添加数据到hive
LOAD DATA   INPATH '/user/hadoop/test5.log' OVERWRITE INTO TABLE test;       hdfs 添加数据到hive
select * from test;        查询数据  
select count(1) from test; 做mapreduce操作运算需要 hadoop 权限


hdfs 常见命令
hdfs dfs -copyFromLocal test3.log /user/hadoop/test5.log 拷贝
hdfs dfs -cat /user/hadoop/test5.log   查看文件内容
hdfs dfs -lsr /user/hadoop        遍历目录  
hdfs dfs -rmr /user/hadoop/storm/ 删除
hdfs dfs -appendToFile test3.log /user/hadoop/test5.log 添加

pom

 <dependency>
	 <groupId>org.apache.hive</groupId>
			<artifactId>hive-jdbc</artifactId> 
			<version>0.11.0</version>
 </dependency>

 <dependency>
	 <groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId> 
			<version>2.2.0</version>
 </dependency>
/**
 * @Type HiveTest.java
 * @Desc 
 * @author hadoop
 * @date 2016年12月29日 下午2:20:46
 * @version 
 */
public class HiveTest {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";

    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://172.23.27.120:10000/default", "hive", "hive");

        Statement stmt = con.createStatement();
        String tableName = "test";
        //stmt.execute("drop table if exists " + tableName);
        //stmt.execute("create table " + tableName + " (key int, value string) row format delimited fields terminated by ','");
        // show tables
        String sql = "show tables '" + tableName + "'"; 
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
            System.out.println(res.getString(1));
        }

        System.out.println("==================================");
        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));
        }

        System.out.println("==================================");
        sql = "select * from " + tableName;
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
        }

        System.out.println("==================================");
        sql = "select  * from " + tableName +" where  key ='1' ";
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
        }
        
        sql = "select count(1) from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1));
        }
        System.out.println("==================================");
    }
}



 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 谢谢您的赞助,我会做的更好!

猜你喜欢

转载自knight-black-bob.iteye.com/blog/2348155