ARCHIVES

HBase에 대하여 2 – 기능

(주)비트나인 2017. 2. 1. 18:59

HBase에 대하여 2 – 기능

 

저번 part1에서는 HBase의 간단한 소개와 특징들에 대해 살펴보았다. 이번에는 클라이언트 API 기본 기능 중 CRUD Operation에 대해서 살펴보고자 한다.

 

Points

  • HBase에 접근하는  주요 client interface는 HTable class이다. (org.apache.hadoop.hbase.client.HTable)

  • 데이터를 변경하는 row 단위 작업은 원자성이 보장된다.

  • 일반적으로 HTable instance는 application을 시작할 때 단 한번만 생성한다.

Put Method

  • Single Puts

    • Put instance를 생성하기 위해서는 row key를 제공해야 한다.

    • HBase의 row는 고유한 row key로 식별하며, row key 타입은 java 데이터 타입인 byte array로 저장된다.

    • Column을 추가하려면 add( )를 사용한다.

    • 특정 cell이 존재하는지 알아보려면 has( )를 사용한다.

    • 클라이언트 코드에서 설정 파일에 접근하려면 HBaseConfiguration class를 사용한다.

  •  KeyValue class

 

    • Row 단위가 아닌 특정 cell 단위의 모든 정보를 반환한다.

    • 좌표계처럼 row key, column family, column qualifier, timestamp가 3차원 공간의 한 지점을 가리키는 모습이다.

    • 주로 KEY 데이터간의 비교 / 검증 / 복제 등에 사용한다.

    • 저장공간을 최소화하여 효율적으로 데이터 저장하고, 빠른 데이터 연산을 제공하기 위해 byte array 타입을 사용한다.

  • List of Puts

    •  

    • 연산을 일괄로 한데 묶어서 처리한다.

    • 리스트 기반의 입력은 서버 측에서 입력 연산이 적용 되는 순서를 제어 할 수 없다.

    • 데이터 입력 순서를 보장해야 하는 경우에는 작게 나눈 후 Write cache를 명시적으로 flush해야 한다.

Get Method

  • Single Gets

    • 특정 row 하나를 대상으로 수행되지만, row 내에서는 column/cell 제한이 없다.

    • 버전 개수는 1로 최근 값만 리턴 받고, setMaxVersions( ) 를 통하여 지정 가능하다.

 

  • Result class

    • get( )을 통하여 데이터를 읽어 들일 때 get 조건에 만족하는 모든 cell을 담고 있는 Result class의 instance를 반환한다.

    • 서버에서 반환한 모든 column family, column qualifier, timestamp 접근 수단을 제공한다.

  • List of Gets

    •  하나의 요청으로 여러 개의 row를 요구할 때 사용된다.

    • get instance와 동일한 배열을 반환, 예외발생 중 하나로만 동작한다.

Delete Method

  • Single Deletes

 

    • delete class를 생성하려면 삭제 대상 row key를 입력해야 한다.

    • rowlock 파라미터를 추가 선택하여 동일한 row를 두 번 이상 변경하고자 할 때 사용자 자신의 락을 설정한다.

    • 전체 family 및 그에 속한 모든 column 삭제, timestamp 지정이 가능하다.

  • List of Deletes

 

    • put list 와 유사하게 동작한다.

    • Remote 서버에서는 데이터가 삭제되는 순서를 보장할 수 없다는 것을 주의한다.

    • table.delete(deletes) 수행 시 실패한 작업은 deletes에 남게 되며, Exception 처리는 try/catch 구문을 이용한다.

Example

  • Put Example(Create)

 

 public class InsertData {

public static void main(String[] args) throws IOException {

Configuration config = HBaseConfiguration.create();

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

 

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

 

p.add(Bytes.toBytes("personal"),

Bytes.toBytes("name"),Bytes.toBytes("raju"));

p.add(Bytes.toBytes("personal"),

Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));

p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),

Bytes.toBytes("manager"));

p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),

Bytes.toBytes("50000"));

 

hTable.put(p);

System.out.println("data inserted");

 

hTable.close();

   }

}

  • Put Example(Update)

 public class UpdateData {

public static void main(String[] args) throws IOException {

Configuration config = HBaseConfiguration.create();

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

 

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

 

p.add(Bytes.toBytes("personal"),

Bytes.toBytes("city"),Bytes.toBytes("Delih"));

 

hTable.put(p);

System.out.println("data Updated");

 

hTable.close();

}

}

  • Get Example(Read)

public class RetriveData{

public static void main(String[] args) throws IOException, Exception{

Configuration config = HBaseConfiguration.create();

HTable table = new HTable(config, "emp");

 

Get g = new Get(Bytes.toBytes("row1"));

 

Result result = table.get(g);

byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));

byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));

 

String name = Bytes.toString(value);

String city = Bytes.toString(value1);

System.out.println("name: " + name + " city: " + city);

}

}

  • Delete Example(Delete)

 public class DeleteData {

public static void main(String[] args) throws IOException {

Configuration conf = HBaseConfiguration.create();

HTable table = new HTable(conf, "employee");

 

Delete delete = new Delete(Bytes.toBytes("row1"));

 

delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));

delete.deleteFamily(Bytes.toBytes("professional"));

 

table.delete(delete);

 

table.close();

System.out.println("data deleted.....");

   }

}

 

 

 

BITNINE GLOBAL INC., THE COMPANY SPECIALIZING IN GRAPH DATABASE
비트나인, 그래프 데이터베이스 전문 기업