package com.my.hbase;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.Cell;
public class HbaseUtils {
private static Configuration conf = null;
private static HConnection conn = null;
private final static int BATCH_SIZE = 1000;
static {
try {
conf = HBaseConfiguration.create();
conn = HConnectionManager.createConnection(conf);
conf.set("hbase.zookeeper.quorum","master");
conf.set("hbase.zookeeper.property.clientPort", "2181");
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建数据库表
public void createTable(String tableName, String[] columnFamilys)
throws Exception {
// 新建一个数据库管理员
HBaseAdmin hAdmin = new HBaseAdmin(conf);
if (hAdmin.tableExists(tableName)) {
System.out.println("表已经存在");
System.exit(0);
} else {
// 新建一个 scores 表的描述
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
// 在描述里添加列族
for (String columnFamily : columnFamilys) {
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
// 根据配置好的描述建表
hAdmin.createTable(tableDesc);
System.out.println("创建表成功");
}
hAdmin.close();
}
// 删除数据库表
public void deleteTable(String tableName) {
// 新建一个数据库管理员
try {
HBaseAdmin hAdmin = new HBaseAdmin(conf);
if (hAdmin.tableExists(tableName)) {
// 关闭一个表
hAdmin.disableTable(tableName);
// 删除一个表
hAdmin.deleteTable(tableName);
System.out.println("删除表成功");
} else {
System.out.println("删除的表不存在");
System.exit(0);
}
hAdmin.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
// 添加一条数据
public void addRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTableInterface table =conn.getTable(tableName);
Put put = new Put(Bytes.toBytes(row));
// 参数出分别:列族、列、值
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);
table.close();
}
// 批量添加数据
private void write(String hbaseTableName,String rowPrefix, String columnFamily,
String qualifier, Collection<String> contents) {
HTableInterface table = null;
try {
table = conn.getTable(hbaseTableName);
List<Put> putList = new ArrayList<Put>();
int idx = 0;
//row自行定义
for (String line : contents) {
String rowKey = rowPrefix + idx;
if (contents.size() == 1)
rowKey = rowPrefix;
idx++;
Put put = new Put(rowKey.getBytes());
put.add(columnFamily.getBytes(), qualifier.getBytes(),
line.getBytes());
putList.add(put);
if (putList.size() >= BATCH_SIZE) {
table.put(putList);
table.flushCommits();
putList.clear();
}
}
table.put(putList);
table.flushCommits();
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 删除一条数据
public void delRow(String tableName, String row) throws Exception {
HTableInterface table =conn.getTable(tableName);
Delete del = new Delete(Bytes.toBytes(row));
table.delete(del);
table.close();
}
// 删除多条数据
public void delMultiRows(String tableName, String[] rows)
throws Exception {
HTableInterface table =conn.getTable(tableName);
List<Delete> list = new ArrayList<Delete>();
for (String row : rows) {
Delete del = new Delete(Bytes.toBytes(row));
list.add(del);
}
table.delete(list);
table.close();
}
// get row
public Map<String, String> getRow(String tableName, String row) throws Exception {
HTableInterface table =conn.getTable(tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
Map<String, String> returnResult = new HashMap<String, String>();
// 输出结果
for (Cell rowKV : result.rawCells()) {
returnResult.put(new String(CellUtil.cloneRow(rowKV)), new String(CellUtil.cloneValue(rowKV)));
}
table.close();
return returnResult;
}
// get all records
public Map<String, String> getAllRows(String tableName) throws Exception {
HTableInterface table =conn.getTable(tableName);
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
Map<String, String> returnResult = new HashMap<String, String>();
// 输出结果
for (Result result : results) {
for (Cell rowKV : result.rawCells()) {
returnResult.put(new String(CellUtil.cloneRow(rowKV)), new String(CellUtil.cloneValue(rowKV)));
}
}
table.close();
return returnResult;
}
private String getFirst(Map<String, String> dataMap) {
if (dataMap == null || dataMap.size() == 0)
return null;
else {
for (String s : dataMap.values()) {
return s;
}
}
return null;
}
// main
public static void main(String[] args) {
try {
HbaseUtils hbaseUtils=new HbaseUtils();
String tableName = "users2";
// 第一步:创建数据库表:“users2”
String[] columnFamilys = { "info", "course" };
hbaseUtils.createTable(tableName, columnFamilys);
// 第二步:向数据表的添加数据
// 添加第一行数据
hbaseUtils.addRow(tableName, "tht", "info", "sex", "boy");
hbaseUtils.addRow(tableName, "tht", "course", "china", "97");
hbaseUtils.addRow(tableName, "tht", "course", "math", "128");
hbaseUtils.addRow(tableName, "tht", "course", "english", "85");
// 添加第二行数据
hbaseUtils.addRow(tableName, "xiaoxue", "info", "age", "19");
hbaseUtils.addRow(tableName, "xiaoxue", "info", "sex", "boy");
hbaseUtils.addRow(tableName, "xiaoxue", "course", "china", "90");
hbaseUtils.addRow(tableName, "xiaoxue", "course", "math", "120");
hbaseUtils .addRow(tableName, "xiaoxue", "course", "english", "90");
// 添加第三行数据
hbaseUtils.addRow(tableName, "qingqing", "info", "age", "18");
hbaseUtils.addRow(tableName, "qingqing", "info", "sex", "girl");
hbaseUtils.addRow(tableName, "qingqing", "course", "china", "100");
hbaseUtils.addRow(tableName, "qingqing", "course", "math", "100");
hbaseUtils.addRow(tableName, "qingqing", "course", "english","99");
// 第三步:获取一条全部版本数据
System.out.println("获取一条数据");
hbaseUtils.getRow(tableName, "tht");
// 第三步:获取一条有效数据
hbaseUtils.getFirst(hbaseUtils.getRow(tableName, "tht"));
// 第四步:获取所有数据
System.out.println("获取所有数据");
hbaseUtils.getAllRows(tableName);
// 第五步:删除一条数据
System.out.println("删除一条数据");
hbaseUtils.delRow(tableName, "tht");
hbaseUtils.getAllRows(tableName);
// 第六步:删除多条数据
System.out.println("删除多条数据");
String[] rows = { "xiaoxue", "qingqing" };
hbaseUtils.delMultiRows(tableName, rows);
hbaseUtils.getAllRows(tableName);
// 第八步:删除数据库
System.out.println("删除数据库");
hbaseUtils.deleteTable(tableName);
} catch (Exception err) {
err.printStackTrace();
}
}
}