hbase를 mac local에 설치했다. 

http://knight76.tistory.com/entry/Hbase-mac-install-mac%EC%97%90-hbase-mac-%EC%84%A4%EC%B9%98


hbase api를 이용해서 위의 예제를 shell에서 진행하듯이 동작 여부를 확인했다. 



package com.google.sample.hbase;



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.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 HBaseTest2 {


public static void main(String[] args) {

Configuration config = HBaseConfiguration.create();

config.set("hbase.master", "127.0.0.1"); 

try {

HBaseAdmin hBaseAdmin = new HBaseAdmin(config);


if (hBaseAdmin.isTableAvailable("key1") == false) {

HTableDescriptor tableDs = new HTableDescriptor("key1");

tableDs.addFamily(new HColumnDescriptor("cf"));

hBaseAdmin.createTable(tableDs);

} else {

hBaseAdmin.disableTable("key1");

hBaseAdmin.deleteTable("key1");


HTableDescriptor tableDs = new HTableDescriptor("key1");

tableDs.addFamily(new HColumnDescriptor("cf"));

hBaseAdmin.createTable(tableDs);

}

HTable hTable = new HTable(config, "key1");

Put p = new Put(Bytes.toBytes("row1"));

p.add(Bytes.toBytes("cf"), Bytes.toBytes("a"), Bytes.toBytes("value1"));

hTable.put(p);

p = new Put(Bytes.toBytes("row2"));

p.add(Bytes.toBytes("cf"), Bytes.toBytes("b"), Bytes.toBytes("value2"));

hTable.put(p);

p = new Put(Bytes.toBytes("row3"));

p.add(Bytes.toBytes("cf"), Bytes.toBytes("c"), Bytes.toBytes("value3"));

hTable.put(p);

Scan s = new Scan();

ResultScanner scanner = hTable.getScanner(s);

try {

for (Result rowResult = scanner.next(); rowResult != null; rowResult = scanner.next()) {

System.out.println("row: " + rowResult);

}

} finally {

scanner.close();

}

hTable.close();

hBaseAdmin.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}



결과 

row: keyvalues={row1/cf:a/1365065659856/Put/vlen=6/ts=0}

row: keyvalues={row2/cf:b/1365065659858/Put/vlen=6/ts=0}

row: keyvalues={row3/cf:c/1365065659859/Put/vlen=6/ts=0}



* 주의 사항

Spring data hadoop(Hbase) example 코드 (https://github.com/SpringSource/spring-hadoop-samples)를 참조해서 테스트를 진행했지만 쉽지 않다. 간단하게 설치한 만큼 spring data를 쓰기가 쉽지 않다. 

역시 hdfs를 기준으로 설치하고 테스트하는  것으로 가야할듯 싶다. 



Posted by '김용환'
,