static Connection conn = null; static Admin admin = null; @Before public void init() throws IOException { Configuration conf = HbaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "node1,node2,node3"); conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); } @After public void close() throws IOException { if (admin != null) { admin.close(); } if (conn != null) { conn.close(); } }1.1 创建命名空间
@Test public void createNameSpace() { NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("myNM").build(); try { // 该方法无返回值 admin.createNamespace(namespaceDescriptor); } catch (NamespaceExistException e) { System.out.println("该命名空间已经存在,无法创建!"); } catch (IOException e) { e.printStackTrace(); } }1.2 删除命名空间
@Test public void deleteNameSpace() { try { // 该方法无返回值 admin.deleteNamespace("myNM"); } catch (NamespaceNotFoundException e) { System.out.println("该命名空间不存在,无法删除!"); } catch (IOException e) { e.printStackTrace(); } }1.3 判断表是否存在
@Test public void isExists() throws IOException { boolean exists = admin.tableExists(TableName.valueOf("tea")); if (exists) { System.out.println("该表存在!"); } else { System.out.println("该表不存在!"); } }1.4 创建表
@Test public void createTable() throws IOException { // 判断该表是否存在 boolean exists = admin.tableExists(TableName.valueOf("myNM:stu")); // 若该表不存在,则创建 if (!exists) { System.out.println("正在创建表..."); // 表名 HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myNM:stu")); // 列族 hTableDescriptor.addFamily(new HColumnDescriptor("info")); // 创建表 admin.createTable(hTableDescriptor); } // 再一次判断该表是否存在 boolean flag = admin.tableExists(TableName.valueOf("myNM:stu")); // 若该表存在,则证明已创建 if (flag) { System.out.println("该表已经创建"); } }1.5 删除表
@Test public void deleteTable() throws IOException { // 判断该表是否存在 boolean exists = admin.tableExists(TableName.valueOf("tea")); // 判断该表是否存在 if (exists) { System.out.println("正在删除表..."); // 先禁用该表 admin.disableTable(TableName.valueOf("tea")); // 再将该表删除 admin.deleteTable(TableName.valueOf("tea")); } // 再一次判断该表是否存在 boolean flag = admin.tableExists(TableName.valueOf("tea")); // 若该表不存在,则证明已删除 if (!flag) { System.out.println("该表已经删除"); } }2. DML
private Connection conn = null; private Table tab = null; @Before public void init() throws IOException { Configuration conf = HbaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "node1,node2,node3"); conn = ConnectionFactory.createConnection(conf); } @After public void close() throws IOException { if (tab != null) { tab.close(); } if (conn != null) { conn.close(); } }2.1 插入数据
@Test public void putData() throws IOException { // 获取表对象 tab = conn.getTable(TableName.valueOf("myNM:stu")); // 获取 put 对象(参数:rowKey) Put put = new Put(Bytes.toBytes("1001")); // 向 put 对象添加数据(参数:columnFamily、columnName、value) put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("JOEL")); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("male")); // 向表中添加数据 tab.put(put); }2.2 查询数据 2.2.1 get
@Test public void getData() throws IOException { // 获取表对象 tab = conn.getTable(TableName.valueOf("stu")); // 获取 get 对象(参数:rowKey) Get get = new Get(Bytes.toBytes("1001")); // 1.指定列组 // get.addFamily(Bytes.toBytes("info")); // 2.指定列组和列 // get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex")); // 3.指定版本数 get.getMaxVersions(); Result result = tab.get(get); // 打印数据 for (Cell cell : result.rawCells()) { System.out.println("ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) + "t" + "ColumnName: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "t" + "Value: " + Bytes.toString(CellUtil.clonevalue(cell))); } }2.2.2 scan
@Test public void scanData() throws IOException { // 获取表对象 tab = conn.getTable(TableName.valueOf("stu")); // 获取 scan 对象 Scan scan = null; // 1.无参,全查 // scan = new Scan(); // 2.指定 StartRow // scan = new Scan(Bytes.toBytes("1001")); // 3.指定 StartRow、StopRow scan = new Scan(Bytes.toBytes("1001"), Bytes.toBytes("1004")); // 获取数据 ResultScanner results = tab.getScanner(scan); // 遍历结果集 方法1: Iterator2.3 删除数据iterator = results.iterator(); while (iterator.hasNext()){ for (Cell cell : iterator.next().rawCells()) { System.out.println("RowKey: " + Bytes.toString(CellUtil.cloneRow(cell)) + "tm" + "ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) + "t" + "ColumnName: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "t" + "Value: " + Bytes.toString(CellUtil.clonevalue(cell))); } } // 遍历结果集 方法2: for (Result result : results) { for (Cell cell : result.rawCells()) { System.out.println("RowKey: " + Bytes.toString(CellUtil.cloneRow(cell)) + "tm" + "ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) + "t" + "ColumnName: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "t" + "Value: " + Bytes.toString(CellUtil.clonevalue(cell))); } } }
@Test public void deleteData() throws IOException { // 获取表对象 tab = conn.getTable(TableName.valueOf("stu")); // 获取 delete 对象 // 1.根据rowKey删除 Delete delete = new Delete(Bytes.toBytes("1001")); // 2.根据列族删除 delete.addFamily(Bytes.toBytes("info")); // 3.根据列删除 delete.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name")); // 4.根据列删除 delete.addColumns(Bytes.toBytes("info"), Bytes.toBytes("name")); // 删除数据 tab.delete(delete); }
❤️ END ❤️
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)