package cn.sniper.hbase.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
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.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
*
* @author sniper
*
*/
public class HBaseUtil {
private static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.rootdir", "hdfs://sniper5:9000/hbase");
conf.set("hbase.zookeeper.quorum", "sniper5,sniper6,sniper7");
//conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
}
/**
* 创建表(单个列族)
* @param tableName
* @param columnFamily
* @throws IOException
*/
public static void create(String tableName, String columnFamily) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)) {
System.out.println("table exists...");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("table create successful... ");
}
}
/**
* 创建表(多个列族)
* @param tableName
* @param columnFamily
* @throws IOException
*/
public static void create(String tableName, String[] columnFamily) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)) {
System.out.println("table exists...");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for(String family:columnFamily) {
tableDesc.addFamily(new HColumnDescriptor(family));
}
admin.createTable(tableDesc);
System.out.println("table create successful... ");
}
}
/**
* 添加一条记录
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void put(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
HTable table = new HTable(conf, tableName);
Put p1 = new Put(Bytes.toBytes(rowKey));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(p1);
}
/**
* 添多条记录
* @param tableName
* @param rowKey
* @param columnFamily
* @param colValueMap
* @throws IOException
*/
public static void put(String tableName, String rowKey, String columnFamily, Map<String, String> colValueMap) throws IOException {
HTable table = new HTable(conf, tableName);
List<Put> putList = new ArrayList<Put>();
Set<String> set = colValueMap.keySet();
for(String column : set) {
String value = colValueMap.get(column);
Put p = new Put(Bytes.toBytes(rowKey));
p.add(columnFamily.getBytes(), column.getBytes(), value.getBytes());
putList.add(p);
}
table.put(putList);
System.out.println("put " + "table:"+tableName+" rowKey:"+rowKey+" columnFamily:"+columnFamily+" colValueMap:"+colValueMap);
}
/**
* 读取一条记录
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @return
* @throws IOException
*/
public static String get(String tableName, String rowKey, String columnFamily, String column) throws IOException {
HTable table = new HTable(conf, tableName);
//拿到rowKey对应的所有的列,result0.list().get(0),result0.list().get(1)...
Get get0 = new Get(Bytes.toBytes(rowKey));
Result result0 = table.get(get0);
//根据rowkey取得记录,打印记录的字段名,字段值
List<KeyValue> keyValueList = result0.list();
for(KeyValue keyValue : keyValueList) {
System.out.println("key:" + Bytes.toString(keyValue.getKey()) + " value:" + Bytes.toString(keyValue.getValue()));
}
//System.out.println("get:" + result0.size() + " " + result0.list() + " " + Bytes.toString(result0.list().get(0).getValue()));
//拿到rowKey中,某个列的数据
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
Result result = table.get(get);
System.out.println(result);
System.out.println(result.list());
System.out.println(result.list().get(0));
System.out.println(result.list().get(0).getValue());
System.out.println("size:" + result.size() + " value:" + Bytes.toString(result.list().get(0).getValue()));
Get g = new Get(rowKey.getBytes());
g.addColumn(columnFamily.getBytes(), column.getBytes());//可以不加该条件
Result get1 = table.get(g);
String value = new String(get1.getValue(columnFamily.getBytes(), column.getBytes()));
return value;
}
/**
* 显示所有数据
* @param tableName
* @throws IOException
*/
public static void scan(String tableName) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner) {
System.out.println("scan:"+result);
}
}
/**
* 显示所有数据
* @param tableName
* @param columnFamily
* @param column
* @throws IOException
*/
public static void scan(String tableName, String columnFamily) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
//扫描列族
scan.addFamily(Bytes.toBytes(columnFamily));
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner) {
System.out.println("scan:"+result);
List<KeyValue> keyValueList = result.list();
for(KeyValue keyValue:keyValueList) {
System.out.println("key:" + Bytes.toString(keyValue.getKey()) + " value:" + Bytes.toString(keyValue.getValue()));
}
}
scanner.close();
}
/**
* 显示所有数据
* @param tableName
* @param columnFamily
* @param column
* @throws IOException
*/
public static void scan(String tableName, String columnFamily, String column) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
if(null != column && !column.trim().equals("")) {
//扫描列
scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
} else {
//扫描列族
scan.addFamily(Bytes.toBytes(columnFamily));
}
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner) {
System.out.println("scan:"+result);
List<KeyValue> keyValueList = result.list();
for(KeyValue keyValue:keyValueList) {
System.out.println("key:" + Bytes.toString(keyValue.getKey()) + " value:" + Bytes.toString(keyValue.getValue()));
}
}
scanner.close();
}
/**
* select 。。。 from table where id between ... and
* 显示所有数据
* @param tableName
* @param columnFamily
* @param column
* @param rowKeyBegin
* @param rowKeyEnd
* @throws IOException
*/
public static void scan(String tableName, String columnFamily, List<String> columns, String rowKeyBegin, String rowKeyEnd) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
scan.setStartRow(rowKeyBegin.getBytes());//开始位置
scan.setStopRow(rowKeyEnd.getBytes());//结束位置
for(String column : columns) {
//扫描列
scan.addColumn(columnFamily.getBytes(), column.getBytes());
}
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
String rowKey = new String(result.getRow());
for(KeyValue keyValue : result.raw()) {
System.out.println(rowKey + ":" + new String(keyValue.getFamily()) + ":" + new String(keyValue.getQualifier()) + "=" + new String(keyValue.getValue()));
}
}
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void drop(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
/**
* 删除多条记录
* @param tableName
* @param rowKey
* @throws IOException
*/
public static void delete(String tableName, String... rowKeys) throws IOException {
HTable table = new HTable(conf, tableName);
List<Delete> list = new ArrayList<Delete>();
for(String rowKey : rowKeys) {
Delete delete = new Delete(rowKey.getBytes());
list.add(delete);
}
table.delete(list);
}
/**
* select 。。。 from table where id c_column = ?
* 条件过滤
* @param tableName
* @param columnFamily
* @param column
* @param rowKeyBegin
* @param rowKeyEnd
* @throws IOException
*/
public static void query(String tableName, String columnFamily, String column, String value) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
Filter filter = new SingleColumnValueFilter(columnFamily.getBytes(), column.getBytes(), CompareFilter.CompareOp.EQUAL, value.getBytes());
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
String rowKey = new String(result.getRow());
for(KeyValue keyValue : result.raw()) {
System.out.println(rowKey + ":" + new String(keyValue.getFamily()) + ":" + new String(keyValue.getQualifier()) + "=" + new String(keyValue.getValue()));
}
}
}
/**
* 条件过滤 rowkey模糊查询
* @param tableName
* @param columnFamily
* @param column
* @param rowKeyBegin
* @param rowKeyEnd
* @throws IOException
*/
public static void queryByRowKey(String tableName, String value) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(value)) ;
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
String rowKey = new String(result.getRow());
for(KeyValue keyValue : result.raw()) {
System.out.println(rowKey + ":" + new String(keyValue.getFamily()) + ":" + new String(keyValue.getQualifier()) + "=" + new String(keyValue.getValue()));
}
}
}
/**
* select * from table where c1 = 111 and c2 in (1, 2)
* @param tableName
* @param columnFamily
* @throws IOException
*/
public static void query(String tableName, String columnFamily) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(columnFamily.getBytes(), "c1".getBytes(), CompareFilter.CompareOp.EQUAL, "aaa".getBytes());
FilterList filterAll = new FilterList();
filterAll.addFilter(filter1);
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(columnFamily.getBytes(), "c4".getBytes(), CompareFilter.CompareOp.EQUAL, "101".getBytes());
SingleColumnValueFilter filter3 = new SingleColumnValueFilter(columnFamily.getBytes(), "c4".getBytes(), CompareFilter.CompareOp.EQUAL, "102".getBytes());
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
filterList.addFilter(filter2);
filterList.addFilter(filter3);
filterAll.addFilter(filterList);
scan.setFilter(filterAll);
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner) {
String rowKey = new String(result.getRow());
for(KeyValue keyValue : result.raw()) {
System.out.println(rowKey + ":" + new String(keyValue.getFamily()) + ":" + new String(keyValue.getQualifier()) + "=" + new String(keyValue.getValue()));
}
}
}
/**
* select * from table where rowkey like 'aaaa%'
* 查询rowkey以xxx开头的
* @param tableName
* @param columnFamily
* @throws IOException
*/
public static void query(String tableName, String columnFamily, String value) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
PrefixFilter filter = new PrefixFilter(value.getBytes());
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner) {
String rowKey = new String(result.getRow());
for(KeyValue keyValue : result.raw()) {
System.out.println(rowKey + ":" + new String(keyValue.getFamily()) + ":" + new String(keyValue.getQualifier()) + "=" + new String(keyValue.getValue()));
}
}
}
/**
* 清空所有数据
* @param tableName
* @throws IOException
*/
public static void truncate(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.truncateTable(TableName.valueOf(tableName), false);
}
public static void main(String[] args) throws IOException {
//Configuration conf = HBaseUtil.getConfiguration();
//System.out.println(conf);
//HBaseUtil.create("t5", "f1");
//创建表(多个列族)
//HBaseUtil.create("t5", new String[]{"f1", "f2"});
/*
for(int i=0; i<5; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("c1", "aaa"+i);
map.put("c2", "bbb"+i);
HBaseUtil.put("t5", "r"+i, "f1", map);
}
System.err.println(HBaseUtil.get("t5", "r1", "f1", "c1"));
*/
/*
HBaseUtil.put("t5", "r1", "f1", "c1", "aaaaaa");
HBaseUtil.put("t5", "r1", "f1", "c2", "bbbbbb");
HBaseUtil.put("t5", "r1", "f2", "c1", "cccccc");
HBaseUtil.put("t5", "r2", "f2", "c1", "cccccc");
HBaseUtil.get("t5", "r1", "f1", "c1");
*/
// HBaseUtil.scan("t5");
/*
HBaseUtil.scan("t5", "f1", "");
HBaseUtil.scan("t5", "f1", "c1");
*/
//HBaseUtil.scan("t5", "f1");
//System.out.println(conf);
String[] rowKeys = new String[]{"r1", "r3"};
HBaseUtil.delete("t5", rowKeys);
HBaseUtil.scan("t5", "f1");
HBaseUtil.delete("t5", "r2");
List<String> columns = new ArrayList<String>();
columns.add("c1");
HBaseUtil.scan("t5", "f1", columns, "r2", "r4");
HBaseUtil.query("t5", "f1", "c1", "aaa1");
HBaseUtil.queryByRowKey("t5", "[a-z][0-1]");
//创建订单表
//HBaseUtil.create("t_order", "info");
//创建订单明细项表
//HBaseUtil.create("t_item", "info");
/*for(int i=100; i<110; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("c_userid", "userid"+i);
map.put("c2", "bbb"+i);
map.put("c3", "ccc"+i);
map.put("c4", ""+i);
map.put("c5", "eee"+i);
HBaseUtil.put("t_order", "rowkey"+i, "info", map);
}
for(int i=100; i<110; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("c1", "aaa");
map.put("c2", "bbb"+i);
map.put("c3", "ccc"+i);
map.put("c4", i+"");
map.put("c5", "eee"+i);
HBaseUtil.put("t_item", i+"_"+"item", "info", map);
}*/
//query("t_item", "info");
//query("t_item", "info", "105_");
HBaseUtil.truncate("t_order");
HBaseUtil.drop("t5");
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.sniper.hbase</groupId>
<artifactId>hbase_util_01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hbase_util_01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.15-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.2.0</version>
</dependency>
<!-- sevlet 编译阶段需要,打包时不打包,由容器提供 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>hbase</finalName>
</build>
</project>