Hive的JDBC方式编程


原文 http://user.qzone.qq.com/165162897/blog/1276050795#!app=2&pos=1276050795


1.启动hiveServer

./hive  --service hiveserver

2.编写jdbc代码(hiveExample.java)

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

public class hiveExample {
 private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
 /**
  * @param args
  */
 public static void main(String[] args)throws SQLException {
  try {
       Class.forName(driverName);
     } catch (ClassNotFoundException e) {
      System.out.println("classForNameError");
       System.exit(1);
     }
     Connection con = DriverManager.getConnection("jdbc:hive://hadoopJobTacker:10000/default", "", "");
     Statement stmt = con.createStatement();
    
     // select * query
     String sql = "select * from dayrawlog limit 5" ;
     System.out.println("Running: " + sql);
     ResultSet res = stmt.executeQuery(sql);
     while (res.next()) {
      System.out.println(String.valueOf(res.getString(1)) + "\t" + res.getString(2));
     }
 }
}

3.编译

javac hiveExample.java

4.编写执行脚本并设置classPath

cat run.sh 
#!/bin/bash
HIVE_HOME=/usr/local/hive
HADOOP_HOME=/usr/local/hadoop
echo -e '1\x01foo' > /tmp/a.txt
echo -e '2\x01bar' >> /tmp/a.txt

HADOOP_CORE=`ls $HADOOP_HOME/hadoop-*-core.jar`
CLASSPATH=.:$HADOOP_CORE:$HIVE_HOME/conf

for i in ${HIVE_HOME}/lib/*.jar ; do
    CLASSPATH=$CLASSPATH:$i
done
java -cp $CLASSPATH hiveExample

 

5.运行

./run.sh

将会产生如下输出
Hive history file=/tmp/hadoop/hive_job_log_hadoop_201006091025_228147173.txt
10/06/09 10:25:53 INFO exec.HiveHistory: Hive history file=/tmp/hadoop/hive_job_log_hadoop_201006091025_228147173.txt
Running: select * from dayrawlog limit 5
poderr1 /pod/201006070000.10.1.120.37.hash0
pod     /pod/201006070000.10.1.120.37.hash0
poderr1 /pod/201006070000.10.1.120.37.hash0
pod     /pod/201006070000.10.1.120.37.hash0
pod     /pod/201006070000.10.1.120.37.hash0

你可能感兴趣的:(sql,编程,hadoop,jdbc,String,2010)