필요한 jar 파일은 아래와 같습니다.
commons-configuration-1.6.jar
commons-lang-2.5.jarcommons-logging-1.1.1.jar
guava-r09.jar
hadoop-auth-0.23.1.jar
hadoop-core-1.0.1.jar
hbase-0.92.0.jar
log4j-1.2.16.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
zookeeper-3.4.2.jar
테스트 소스는 아래와 같습니다.
package hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseExample2 {
/**
* @param args
*/
public static void main(String[] args) {
// connection
Configuration config = HBaseConfiguration.create();
config.set("hbase.master", "umx1"); // master info
config.set("hbase.zookeeper.quorum", "umx1");
try {
HBaseAdmin ha = new HBaseAdmin(config);
// test라는 table이 없으면 생성함
if (ha.isTableAvailable("test") == false) {
System.out.println("'test' not exist!");
System.out.println("create 'test'");
HTableDescriptor tableDs = new HTableDescriptor("test");
tableDs.addFamily(new HColumnDescriptor("cf"));
ha.createTable(tableDs);
} else { // test라는 table이 있으면 삭제함
System.out.println("'test' exist!");
System.out.println("disable 'test'");
ha.disableTable("test");
System.out.println("drop 'test'");
ha.deleteTable("test");
System.out.println("create 'test'");
HTableDescriptor tableDs = new HTableDescriptor("test");
tableDs.addFamily(new HColumnDescriptor("cf"));
ha.createTable(tableDs);
}
HTable table = new HTable(config, "test");
// table에 데이터 넣기
System.out.println('\n' + "put 'test', 'row1', 'cf:qf1', 'value1'");
Put p = new Put(Bytes.toBytes("row1"));
p.add(Bytes.toBytes("cf"), Bytes.toBytes("qf1"), Bytes.toBytes("value1"));
table.put(p);
System.out.println("put 'test', 'row2', 'cf:qf2', 'value2'");
p = new Put(Bytes.toBytes("row2"));
p.add(Bytes.toBytes("cf"), Bytes.toBytes("qf2"), Bytes.toBytes("value2"));
table.put(p);
System.out.println("put 'test', 'row3', 'cf:qf1', 'value3'");
p = new Put(Bytes.toBytes("row3"));
p.add(Bytes.toBytes("cf"), Bytes.toBytes("qf1"), Bytes.toBytes("value3"));
table.put(p);
// table의 모든 row 가져오기
Scan s = new Scan();
System.out.println('\n' + "scan 'test'");
//s.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qf1"));
//s.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qf2"));
ResultScanner scanner = table.getScanner(s);
try {
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
System.out.println("Found row: " + rr);
}
} finally {
scanner.close();
}
// table에 데이터 넣기
System.out.println('\n' + "put 'test', 'row4', 'cf:qf2', 'value4'");
p = new Put(Bytes.toBytes("row4"));
p.add(Bytes.toBytes("cf"), Bytes.toBytes("qf2"), Bytes.toBytes("value4"));
table.put(p);
// 특정 row의 value 가져오기
System.out.println('\n' + "get 'test', 'row4', 'cf:qf2'");
Get g = new Get(Bytes.toBytes("row4"));
Result r = table.get(g);
byte[] value = r.getValue(Bytes.toBytes("cf"), Bytes.toBytes("qf2"));
String valueStr = Bytes.toString(value);
System.out.println("GET: " + valueStr);
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}