Hbase的常用API(Java版)

1.判断表是否存在

admin.tableExists(表名)

private static boolean isExsist (String name) throws IOException{
       Connection connection = ConnectionFactory.createConnection(conf);
       Admin admin = connection.getAdmin();
       return admin.tableExists(TableName.valueOf(name));
}

2.在hbase中创建表
admin.createTable(表名)

public static void createTablet(String name,String... columnFamily) throws IOException{
    Connection connection = ConnectionFactory.createConnection(conf);
    Admin admin = connection.getAdmin();
    HTableDescriptor hd = new HTableDescriptor(TableName.valueOf(name));
    for(String cf : columnFamily){
        hd.addFamily(new HColumnDescriptor(cf));
    }
    admin.createTable(hd);
    System.out.println(name+"表已经创建成功");
}

3.向表中添加数据
Put put = new Put(Bytes.toBytes(rowkey))
put.addColumn(Bytes.toBytes(列族),Bytes.toBytes(列),Bytes.toBytes(值))

public static void  putData(String tableName,String rowkey,String columnFamily,String column,String value)throws  IOException{
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowkey));
		put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        table.put(put);
        System.out.println("向"+tableName+"表中put了一条数据");
}

4.删除一个rowkey数据

Delete delete = new Delete(Bytes.toBytes(rowkey)) ;
table.delete(delete);

public static void deleteRow(String tablename,String rowkey) throws IOException{
    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf(tablename));
    Delete delete = new Delete(Bytes.toBytes(rowkey)) ;
    table.delete(delete);
    System.out.println("删除了habse表:"+tablename+"rowkey");
}

5.删除多个rowkey的数据
table.delete(deleteList)

public  static void deleteRowList(String tablename,String... rowkey)throws IOException{
    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf(tablename));
    List<Delete> deleteList = new ArrayList<Delete>();
    for (String key : rowkey){
        Delete delete = new Delete(Bytes.toBytes(key));
        deleteList.add(delete);
    }
    table.delete(deleteList);
    System.out.println("删除了hbase表:"+tablename+"多条记录");
}

6.全表扫描

 public static void scanTable(String tableName)throws IOException{
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        for(Result r : rs){
            Cell[] cells = r.rawCells();
            for(Cell c : cells){
                System.out.println("rowkey为:" + Bytes.toString(CellUtil.cloneRow(c)));
                System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));
                System.out.println("列为:" + Bytes.toString(CellUtil.cloneQualifier(c)));
                System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));

        }
    }
}

7.删除表
admin.disableTable(表名)
admin.deleteTable(表名)

public  static void deleteTable(String tableName)throws  IOException{
       Connection connection = ConnectionFactory.createConnection(conf);
       Admin admin = connection.getAdmin();
       admin.disableTable(TableName.valueOf(tableName));
       admin.deleteTable(TableName.valueOf(tableName));
       System.out.println("删除hbase表"+tableName+"成功");
}
发布了15 篇原创文章 · 获赞 28 · 访问量 1140

猜你喜欢

转载自blog.csdn.net/qq_37865420/article/details/103534083
今日推荐