【HBase】用Java操作HBase

先序

  • JAVA1.8
  • Hadoop 2.7.7
  • HBase 2.0.5
  • Gradle 5.3.1
  • 依赖:
    compile group: 'org.apache.hbase', name: 'hbase-client', version: '2.0.5'
  • 源码:https://github.com/Hayaking/HBaseOperator

创建连接

@Logger
public class HBaseUtils {
    static java.util.logging.Logger logger = getLogger( "haya" );
    private static Configuration config = HBaseConfiguration.create();
    private static Connection con;

    private static void setConfig(String address, String port) {
        config.set( "hbase.zookeeper.quorum", address );
        config.set( "hbase.zookeeper.property.clientPort", port );
    }
    public static Connection getConnection(String address, String port) throws IOException {
        setConfig( address, port );
        return con = ConnectionFactory.createConnection( config );
    }
}

@Logger和static java.util.logging.Logger logger = getLogger( “haya” )是和日志有关的。
接下来创建两个成员变量Configuration config、Connection con。其中config是配置对象,把必要的信息设置到这个对象里,比如IP地址,端口号;然后调用ConnectionFactory工厂类的createConnection方法,把配置好的config对象传递进去,就会得到一个Connection对象。
至于为什么要把Configuration和Connection的实例对象设置为成员变量,是因为后面好多操作都要用到他俩。。。

创建表

public static void createTable(String tableName, String familyName) throws IOException {
        Admin admin = con.getAdmin();
        TableName name = TableName.valueOf( tableName );
        if (admin.tableExists( name )) {
            logger.warning( "表已存在" );
        } else {
            TableDescriptorBuilder desc = TableDescriptorBuilder.newBuilder( name );
            //构建列族对象
            ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder( Bytes.toBytes( familyName ) ).build();
            //设置列族
            desc.setColumnFamily( family );
            //创建表
            admin.createTable( desc.build() );
            logger.info( tableName + "创建成功" );
        }
}

首先看Admin admin = con.getAdmin()。Admin类是一个管理类,可用于创建、删除、列出、启用和禁用、修改表,以及执行其他管理操作。以下是官方源码里的注释:

Admin can be used to create, drop, list, enable and disable and otherwise modify tables,
as well as perform other administrative operations

TableName name = TableName.valueOf( tableName )是将String类型的表名转换为TableName类型。
admin.tableExists( name )是用于查看指定表名的表是否存在于数据库中。
TableDescriptorBuilder 是一个表的构造对象,通过表名传进newBuilder中获得一个对象。
ColumnFamilyDescriptor是一个列族的构造对象,通过把列族名传进newBuild中获得一个对象。
desc.setColumnFamily( family )是把得到的列族对象设置到表对象。
admin.createTable( desc.build() )根据表对象创建表

删除表

    public static void deleteTable(String tableName) throws IOException {
        Admin admin = con.getAdmin();
        admin.disableTable( TableName.valueOf( tableName ) );
        admin.deleteTable( TableName.valueOf( tableName ) );
    }

删表前需要先禁用表。

添加数据

    public static void add(String tableName, String rowName, String familyName, String qualifierName, String val) throws IOException {
        Table table = getTable( tableName );
        Put put = buildPut( rowName, familyName, qualifierName, val );
        table.put( put );
        table.close();
    }
    public static Table getTable(String tableName) throws IOException {
        return con.getTable( TableName.valueOf( tableName ) );
    }

    public static Put buildPut(String rowName, String familyName, String qualifierName, String val) {
        byte[] row = Bytes.toBytes( rowName );
        byte[] family = Bytes.toBytes( familyName );
        byte[] qualifier = Bytes.toBytes( qualifierName );
        byte[] value = Bytes.toBytes( val );
        Put put = new Put( row );
        put.addColumn( family, qualifier, value );
        return put;
    }

	public static Result getResultByRow(String tableName, String rowName) throws IOException {
        Table table = getTable( tableName );
        Get get = new Get( Bytes.toBytes( rowName ) );
        Result result = table.get( get );
        table.close();
        return result;
    }

    public static String getValue(Result result, String familyName, String qualifierName) {
        byte[] value = result.getValue( Bytes.toBytes( familyName ), Bytes.toBytes( qualifierName ) );
        return new String( value, StandardCharsets.UTF_8 );
    }
    
    public static List<Result> getAllResult(String tableName) throws IOException {
        Table table = getTable( tableName );
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner( scan );
        ArrayList<Result> list = new ArrayList<>();
        for (Result res : scanner) {
            list.add( res );
        }
        return list;
    }

你可能感兴趣的:(——Hadoop,——HBase)