import java.util.ArrayList;

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.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; public class HBaseUtil {
private static HBaseConfiguration conf = null; static {
conf = new HBaseConfiguration();
//conf.set("hbase.master", "10.3.61.141:60000");
//conf.set("hbase.zookeeper.quorum", "hadoop141,hadoop142,hadoop143,hadoop144");
//conf.set("hbase.master.port", "60000");
conf.addResource("hbase-site.xml");
} // add
public void createTable(String tableName, String[] cfs) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
System.out.println("表已经存在了");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);//表描述
for (int i = 0; i < cfs.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(cfs[i]));//列族
}
admin.createTable(tableDesc);
System.out.println("表创建成功!");
}
} // delete table
public void deleteTable(String tableName) throws IOException {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("删除表成功");
} catch (Exception e) {
e.printStackTrace();
}
} public void writeRow(String tableName, String[] cfs) {
try {
HTable table = new HTable(conf, tableName);
Put put = new Put(org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[0]));
put.add(org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[1]),
org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[2]),
org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[3]));
table.put(put);
} catch (Exception e) {
e.printStackTrace();
}
}
// delete value
public void deleteRow(String tableName, String rowKey) throws IOException {
HTable table = new HTable(conf, tableName);
java.util.List<Delete> list = new ArrayList<Delete>();
Delete dl = new Delete(rowKey.getBytes());
list.add(dl);
table.delete(list);
System.out.println("删除成功!");
} public void selectRow(String tableName, String rowKey) throws IOException {
HTable table = new HTable(conf, tableName);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println(new String(kv.getRow()) + " ");
System.out.println(new String(kv.getFamily()) + ":");
System.out.println(new String(kv.getQualifier()) + " ");
System.out.println(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()) + " ");
}
} public void scaner(String tableName) {
try {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan); for (Result r : rs) {
KeyValue[] kv = r.raw();
for (int i = 0; i < kv.length; i++) {
System.out.print(new String(kv[i].getRow()) + " ");
System.out.print(new String(kv[i].getFamily()) + ": ");
System.out.print(new String(kv[i].getQualifier()) + " ");
System.out.print(kv[i].getTimestamp() + " ");
System.out.println(new String(kv[i].getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

2.HBaseTest.java

package cn.netcenter.test;

import java.io.IOException;

import cn.netcenter.hbase.HBaseUtil;

public class HBaseTest {

	public static void main(String[] args) throws IOException {
//createTable();
getRow();
//put(); } public static void createTable() throws IOException{
HBaseUtil util=new HBaseUtil();
String[] cfs={"grade","course"};
util.createTable("score", cfs);
} public static void put() throws IOException{
HBaseUtil util=new HBaseUtil();
//String[] cfs={"zkb","grade","","5"};
String[] cfs={"baoniu","course","art","80"}; util.writeRow("scores", cfs); } public static void scaner()throws IOException{
HBaseUtil util=new HBaseUtil();
util.scaner("bio");
} public static void getRow()throws IOException{
HBaseUtil util=new HBaseUtil();
util.selectRow("scores", "baoniu");
}
public static void deleteTable()throws IOException{
HBaseUtil util=new HBaseUtil();
util.deleteTable("score");
}
}

最新文章

  1. Gearman使用示例
  2. CodeForces - 699B One Bomb
  3. Spark-1.5.2安装
  4. js 未结束的字符串常量错误解决方法
  5. html10个特效(转载)
  6. 函数重载二义性:error C2668: &#39;pow&#39; : ambiguous call to overloaded function
  7. HDU 1754 I Hate It 线段树 单点更新 区间最大值
  8. chisel中pviews命令无法使用
  9. webpack入门与解析(一)
  10. Netty方法误解ChannelHandlerContext.writeAndFlush(Object msg)
  11. 虚拟主机、VPS以及云主机的区别和对比
  12. AOV网络和Kahn算法拓扑排序
  13. 提升 PLSQL 开发性能漫谈
  14. Java集合类根接口:Collection 和 Map
  15. 配置国内 Docker Registry Mirror
  16. linux目录与文件权限的意义
  17. 搭建ssm框架
  18. 洛谷P1337 【[JSOI2004]平衡点 / 吊打XXX】(模拟退火)
  19. [SQL]用SQL语句断开某个数据库的所有活动连接
  20. Json解析数据

热门文章

  1. 【京东详情页】——原生js爬坑之标签页
  2. JAVA多线程---volatile关键字
  3. JQuery获取图片大小并控制图片文件上传大小以及上图片文件时如何预览图片
  4. java集合系列——List集合之ArrayList介绍(二)
  5. “一切都是消息”--MSF(消息服务框架)之【请求-响应】模式
  6. DataGridView的使用记录
  7. 将 Intent 序列化,像 Uri 一样传递 Intent!!!
  8. java 学习笔记 读取配置文件的三种方式
  9. html加载时事件触发顺序
  10. JavaScript面向对象(OOP)