Hive-Java Thrift Client 연동은 약간의 문제가 있다. 버전 이슈가 크다.

Hadoop-Hive-lib Thrift 간의 궁합이 맞지 않으면, 계속 에러가 발생한다. 이 궁합의 버전으로 계속 테스트해보아도 잘 안맞아서 다양한 Exception과 Code 분석 끝에.. 요즘 나온 상위버전으로는 해결이 안된다는 사실을 파악했다. 오픈 소스의 가장 큰 단점은 이 호환성 문제인듯..


구글 검색해보니. 아래 분의 블로그가 나온다. 

http://odysseymoon.tistory.com/m/post/view/id/36

이 훌륭하신 분이 Hive-Thrift 궁합버전을 잘 정리해주셨다. 

(또한 Mybatis-Hive 코드는 신선함까지 전달된다. 쵝오~) 



Pom.xml 파일의 dependecy와 아래와 같이 설정하면 잘 동작 됨.


<properties>

<hadoop.version>2.0.0-cdh4.0.0</hadoop.version>

<hive.version>0.8.1-cdh4.0.0</hive.version>

</properties>

...

<repositories>

<repository>

<id>com.cloudera.cdh4</id>

<name>Cloudera CDH4 Maven Repository</name>

<url> https://repository.cloudera.com/artifactory/cloudera-repos</url>

</repository>

</repositories>


....

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-common</artifactId>

<version>${hadoop.version}</version>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-common</artifactId>

<version>${hive.version}</version>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-jdbc</artifactId>

<version>${hive.version}</version>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-metastore</artifactId>

<version>${hive.version}</version>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-service</artifactId>

<version>${hive.version}</version>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-serde</artifactId>

<version>${hive.version}</version>

</dependency>

<dependency>

<groupId>org.apache.thrift</groupId>

<artifactId>libthrift</artifactId>

<version>0.6.1</version>

</dependency>




 Hive 서버쪽 설정 계정 확인 (hive는 metastore 저장을 위해서 mysql을 사용하고 있다.)

$ vi hive-site.xml 

<property>

  <name>javax.jdo.option.ConnectionURL</name>

  <value>jdbc:mysql://localhost:20306/hive?createDatabaseIfNotExist=true</value>

  <description>JDBC connect string for a JDBC metastore</description>

</property>


<property>

  <name>javax.jdo.option.ConnectionDriverName</name>

  <value>com.mysql.jdbc.Driver</value>

  <description>Driver class name for a JDBC metastore</description>

</property>


<property>

  <name>javax.jdo.option.ConnectionUserName</name>

  <value>hive</value>

  <description>username to use against metastore database</description>

</property>


<property>

  <name>javax.jdo.option.ConnectionPassword</name>

  <value>hive</value>

  <description>password to use against metastore database</description>

</property>


Hive-Thrift Server서버 실행

$ HIVE_PORT=20180 hive --service hiveserver &

[1] 111993

Starting Hive Thrift Server




Java Client 테스트 코드 



import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class LanguageJob3 {

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


private static String tableName = "test";

/**

* @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:hive://1.1.1.1:20180", "hive", "hive");

Statement stmt = con.createStatement();

// 1. drop table

stmt.executeQuery("drop table " + tableName);

// 2. create table

ResultSet res = stmt.executeQuery("create table " + tableName + " (key string, value string)");


// 3. describe table

String 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));

   }


// 4. select table

sql = "select * from " + tableName + " limit 1";

System.out.println("Running: " + sql);

res = stmt.executeQuery(sql);

while (res.next()) {

System.out.println(res.getString(1));

}

}

}



테스트 결과

Running: describe test

key string

value string

Running: select * from test limit 1

(데이터가 없으니 안나오는 게 맞음)



잘 실행됨. 



Posted by '김용환'
,