mirror of
https://github.com/wahyd4/pinpoint.git
synced 2026-08-25 12:46:21 +10:00
[강운덕] [LUCYSUS-1744] hbase template 개선.
git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-commons/trunk@702 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.profiler.common.hbase;
|
||||
|
||||
import org.apache.hadoop.hbase.client.Put;
|
||||
import org.springframework.data.hadoop.hbase.HbaseOperations;
|
||||
import org.springframework.data.hadoop.hbase.RowMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface HbaseOperations2 extends HbaseOperations {
|
||||
/**
|
||||
* Gets an individual row from the given table. The content is mapped by the given action.
|
||||
*
|
||||
* @param tableName target table
|
||||
* @param rowName row name
|
||||
* @param mapper row mapper
|
||||
* @return object mapping the target row
|
||||
*/
|
||||
<T> T get(String tableName, byte[] rowName, final RowMapper<T> mapper);
|
||||
|
||||
/**
|
||||
* Gets an individual row from the given table. The content is mapped by the given action.
|
||||
*
|
||||
* @param tableName target table
|
||||
* @param rowName row name
|
||||
* @param familyName column family
|
||||
* @param mapper row mapper
|
||||
* @return object mapping the target row
|
||||
*/
|
||||
<T> T get(String tableName, byte[] rowName, byte[] familyName, final RowMapper<T> mapper);
|
||||
|
||||
/**
|
||||
* Gets an individual row from the given table. The content is mapped by the given action.
|
||||
*
|
||||
* @param tableName target table
|
||||
* @param rowName row name
|
||||
* @param familyName family
|
||||
* @param qualifier column qualifier
|
||||
* @param mapper row mapper
|
||||
* @return object mapping the target row
|
||||
*/
|
||||
<T> T get(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final RowMapper<T> mapper);
|
||||
|
||||
|
||||
void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final byte[] value);
|
||||
|
||||
void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final Long timestamp, final byte[] value);
|
||||
|
||||
<T> void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final T value, final ValueMapper<T> mapper);
|
||||
|
||||
<T> void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final Long timestamp, final T value, final ValueMapper<T> mapper);
|
||||
|
||||
<T> void put(String tableName, final Put put);
|
||||
|
||||
<T> void put(String tableName, final List<Put> puts);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.profiler.common.hbase;
|
||||
|
||||
import org.apache.hadoop.hbase.client.*;
|
||||
import org.springframework.data.hadoop.hbase.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class HbaseTemplate2 extends HbaseTemplate implements HbaseOperations2 {
|
||||
|
||||
@Override
|
||||
public <T> T get(String tableName, byte[] rowName, RowMapper<T> mapper) {
|
||||
return get(tableName, rowName, null, null, mapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String tableName, byte[] rowName, byte[] familyName, RowMapper<T> mapper) {
|
||||
return get(tableName, rowName, familyName, null, mapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final RowMapper<T> mapper) {
|
||||
return execute(tableName, new TableCallback<T>() {
|
||||
@Override
|
||||
public T doInTable(HTable htable) throws Throwable {
|
||||
Get get = new Get(rowName);
|
||||
if (familyName != null) {
|
||||
if (qualifier != null) {
|
||||
get.addColumn(familyName, qualifier);
|
||||
}
|
||||
else {
|
||||
get.addFamily(familyName);
|
||||
}
|
||||
}
|
||||
Result result = htable.get(get);
|
||||
return mapper.mapRow(result, 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final byte[] value) {
|
||||
put(tableName, rowName, familyName, qualifier, null, value);
|
||||
}
|
||||
|
||||
public void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final Long timestamp, final byte[] value) {
|
||||
execute(tableName, new TableCallback() {
|
||||
@Override
|
||||
public Object doInTable(HTable htable) throws Throwable {
|
||||
Put put = new Put(rowName);
|
||||
if (familyName != null) {
|
||||
if(timestamp == null) {
|
||||
put.add(familyName, qualifier, value);
|
||||
} else {
|
||||
put.add(familyName, qualifier, timestamp, value);
|
||||
}
|
||||
}
|
||||
htable.put(put);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public <T> void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final T value, final ValueMapper<T> mapper) {
|
||||
put(tableName, rowName, familyName, qualifier, null, value, mapper);
|
||||
}
|
||||
|
||||
public <T> void put(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final Long timestamp, final T value, final ValueMapper<T> mapper) {
|
||||
execute(tableName, new TableCallback<T>() {
|
||||
@Override
|
||||
public T doInTable(HTable htable) throws Throwable {
|
||||
Put put = new Put(rowName);
|
||||
byte[] bytes = mapper.mapValue(value);
|
||||
if (familyName != null) {
|
||||
if(timestamp == null) {
|
||||
put.add(familyName, qualifier, bytes);
|
||||
} else {
|
||||
put.add(familyName, qualifier, timestamp, bytes);
|
||||
}
|
||||
}
|
||||
htable.put(put);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public <T> void put(String tableName, final Put put) {
|
||||
execute(tableName, new TableCallback<T>() {
|
||||
@Override
|
||||
public T doInTable(HTable htable) throws Throwable {
|
||||
htable.put(put);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public <T> void put(String tableName, final List<Put> puts) {
|
||||
execute(tableName, new TableCallback<T>() {
|
||||
@Override
|
||||
public T doInTable(HTable htable) throws Throwable {
|
||||
htable.put(puts);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.profiler.common.hbase;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface ValueMapper<T> {
|
||||
byte[] mapValue(T value);
|
||||
}
|
||||
Reference in New Issue
Block a user