Hbase APi
1、引入依赖
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.4.11</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.11</version> </dependency>
2、编写方法测试
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * Connection:通过ConnectionFactory 获取,是重量级实现,获取一次即可 * Table:DML 操作 * Admin:DDL 操作 */ public class HbaseDemo { private static Connection connection; static { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop201,hadoop202,hadoop203"); try { connection = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { // createTable("", "stu1", "info", "info1"); //createNameSpace(""); //deleteTable("", "stu1"); // putDate("", "stu", "1002", "info", "name", "yilin"); deleteDate("", "stu", "1002", "", ""); } /** * get 数据 * @param nameSpace * @param tableName * @param rorkey * @param cf * @param c1 * @throws IOException */ public static void getDate(String nameSpace, String tableName, String rorkey, String cf, String c1) throws IOException { //表若存在,直接退出 if (!existsTable(nameSpace, tableName)) { System.err.println("表不存在"); return; } Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); Get get = new Get(Bytes.toBytes(rorkey)); get.addFamily(Bytes.toBytes(cf)); Result result = table.get(get); Cell[] cells = result.rawCells(); for (Cell cell : cells) { String s = Bytes.toString(CellUtil.cloneRow(cell)) + " : " + Bytes.toString(CellUtil.cloneFamily(cell)) + " : " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " : " + Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(s); } table.close(); } /** * 新增/修改 * * @param nameSpace * @param tableName * @param rorkey * @param cf * @param c1 * @param value * @throws IOException */ public static void putDate(String nameSpace, String tableName, String rorkey, String cf, String c1, String value) throws IOException { //表若存在,直接退出 if (!existsTable(nameSpace, tableName)) { System.err.println("表不存在"); return; } Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); Put put = new Put(Bytes.toBytes(rorkey)); put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(c1), Bytes.toBytes(value)); table.put(put); table.close(); } /** * 删除数据 * * @param nameSpace * @param tableName * @param rorkey * @param cf * @param cl * @throws IOException */ public static void deleteDate(String nameSpace, String tableName, String rorkey, String cf, String cl) throws IOException { //表若存在,直接退出 if (!existsTable(nameSpace, tableName)) { System.err.println("表不存在"); return; } Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); Delete delete = new Delete(Bytes.toBytes(rorkey)); table.delete(delete); table.close(); } /** * 判断表是否存在 * * @param nameSpace * @param tableName * @return * @throws IOException */ public static Boolean existsTable(String nameSpace, String tableName) throws IOException { //获取 admin 对象 Admin admin = connection.getAdmin(); //判断表是否存在 return admin.tableExists(TableName.valueOf(nameSpace, tableName)); } /** * 创建表 * * @param nameSpace * @param tableName * @param cfx * @throws IOException */ public static void createTable(String nameSpace, String tableName, String... cfx) throws IOException { //表若存在,直接退出 if (existsTable(nameSpace, tableName)) { System.err.println("表已经存在"); return; } //获取 admin 对象 Admin admin = connection.getAdmin(); TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(nameSpace, tableName)); if (cfx.length < 1 || cfx == null) { System.err.println("列族不能为空"); return; } for (String cf : cfx) { //处理列族数据 ColumnFamilyDescriptor build = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build(); tableDescriptorBuilder.setColumnFamily(build); } TableDescriptor build = tableDescriptorBuilder.build(); //执行创建表操作 admin.createTable(build); //关闭资源 admin.close(); } /** * 创建 nameSpace * * @param namepace * @throws IOException */ public static void createNameSpace(String namepace) throws IOException { //如果 namepace 为空直接瑞出 if (namepace == null || namepace.length() == 0) { System.err.println("namepace 不能为空"); return; } //获取 admin 对象 Admin admin = connection.getAdmin(); NamespaceDescriptor build = NamespaceDescriptor.create(namepace).build(); //调用方法创建库 try { admin.createNamespace(build); } catch (NamespaceExistException e) { System.err.println(namepace + "已经存在了"); } //关闭资源 admin.close(); } /** * 表删除 * * @param nameSpace * @param tableName * @throws IOException */ public static void deleteTable(String nameSpace, String tableName) throws IOException { if (!existsTable(nameSpace, tableName)) { System.err.println("表不存在..."); return; } Admin admin = connection.getAdmin(); //拼接 库&表 TableName tableName1 = TableName.valueOf(nameSpace, tableName); admin.disableTable(tableName1); admin.deleteTable(tableName1); admin.deleteTable(TableName.valueOf(nameSpace, tableName)); admin.close(); } }