package firsthbase;
import java.io.IOException;
import java.util.ArrayList; import java.util.Iterator; import java.util.List;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell; 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.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; 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.client.Table;public class HbaseUtil {
private static Connection connection=null; //创建一个hbase的configuartion对象 private static Configuration conf = HBaseConfiguration.create(); public static void main(String[] args) throws Exception { //createTable("account1"); //insert("account1"); //get("account1"); //scan("account1"); //del("user");getAllTables();
} /** * 创建表 * tableName * Exception */ public static void createTable(String tableName) throws Exception{ setConfing(); //拿到hbase管理员的对象 这个对象可以操作hbase的表 Admin admin = connection.getAdmin(); //判断表是否存在 boolean tableExists = admin.tableExists(TableName.valueOf(tableName)); if(tableExists){ //先把该表设置成不可用 admin.disableTable(TableName.valueOf(tableName)); //删除表 admin.deleteTable(TableName.valueOf(tableName)); } //创建表描述信息对象 HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(tableName)); //创建列族对象 HColumnDescriptor info=new HColumnDescriptor("cf1".getBytes()); //设置最大版本号 info.setMaxVersions(5); //是否开启缓存 info.setBlockCacheEnabled(true); info.setBlocksize(1800000); //添加列族 desc.addFamily(info); //创建表 admin.createTable(desc); //关闭流 admin.close(); System.out.println("create success"); } private static void setConfing() throws IOException { //设置连接哪个hbase集群 的zookeeper地址 conf.set("hbase.zookeeper.quorum", "node22:2181,node33:2181,node44:2181"); //获取链接 connection = ConnectionFactory.createConnection(conf); } /** * 向表中插入数据 * tableName * Exception */ public static void insert(String tableName) throws Exception{ setConfing(); //创建表对象 来往里插入数据 Table table = connection.getTable(TableName.valueOf(tableName)); //创建插入数据的对象集合 List<Put> list = new ArrayList<Put>(); Put put=null; for (int i=0;i<20;i++) { put=new Put(String.valueOf(i).getBytes()); //插入数据的列 put.addColumn("cf1".getBytes(), "count".getBytes(), String.valueOf(i).getBytes()); list.add(put); } //向表中放入数据 table.put(list); table.close(); System.out.println("insert success"); } public static void get(String tableName) throws Exception{ setConfing(); //创建表对象 来往里插入数据 Table table = connection.getTable(TableName.valueOf(tableName)); //要查询那个rowkey的信息 Get get=new Get("2".getBytes()); //查询的结果 Result result = table.get(get); System.out.println(new String(result.getValue("cf1".getBytes(), "count".getBytes()))); } /** * 全表扫描 * tableName * @throws Exception */ public static void scan(String tableName) throws Exception{ setConfing(); //创建表对象 来往里插入数据 Table table = connection.getTable(TableName.valueOf(tableName)); //扫描那个rowkey的范围 Scan scan = new Scan("1".getBytes(),"20".getBytes()); ResultScanner scanner = table.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result next = it.next(); byte[] value = next.getValue("cf1".getBytes(), "count".getBytes()); System.out.println(new String(value,"GBK")); } } /** * 删除表里的数据 * @param tableName * @throws Exception */ public static void del(String tableName) throws Exception{ setConfing(); //创建表对象 来往里插入数据 Table table = connection.getTable(TableName.valueOf(tableName)); //创建删除哪个rowkey对象 Delete del=new Delete("rk001".getBytes()); //要删除该rowkey下的哪个列族 // del.addColumn("data1".getBytes(),"pic".getBytes()); table.delete(del); System.out.println("delete success"); } }/**
* 获得所有表名 * @throws Exception */ public static void getAllTables() throws Exception{ setConfing(); //拿到hbase管理员的对象 这个对象可以操作hbase的表 Admin admin = connection.getAdmin(); TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { System.out.println(tableName.getNameAsString()); } }