Flink 连接Hbase进行建表正删改查操作

//创建连接
private static Connection connection=null;
static {
try {
//创建配置文件
Configuration conf = HBaseConfiguration.create();
//设置zookeeper参数和主机名称
conf.set(“hbase.zookeeper.quorum”,“hdp111,hdp222,hdp333”);
//设置端口号
conf.set(“hbase.zookeeper.property.clientPort”,“2181”);
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//添加表
public static void start1(String tablename,String… familes) throws IOException {
Admin admin = connection.getAdmin();

    HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename));
    try {
        for (String famile : familes) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(famile);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);
    }finally {
        admin.close();
    }
}
//添加数据
public static void start2(String tablename,String rowKey,String family,String column,String value,String family1,String column1,String value1) throws IOException {
    Admin admin = connection.getAdmin();

    try {
        if (!admin.tableExists(TableName.valueOf(tablename))){
            System.out.println(tablename+"表不存在");
            return;
        }
        Table table = connection.getTable(TableName.valueOf(tablename));
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        table.put(put);
        Put put1 = new Put(Bytes.toBytes(rowKey));
        put1.addColumn(Bytes.toBytes(family1),Bytes.toBytes(column1),Bytes.toBytes(value1));
        table.put(put1);
    }finally {
        admin.close();
    }
}

//获取数据
public static void start3(String tableName,String startrow,String stoprow) throws IOException {
    //获取表
    Table table = connection.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(stoprow));
    //获取列
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
        for (Cell cell : result.rawCells()) {
            //获取rowkey
            byte[] rowbys = CellUtil.cloneRow(cell);
            String rowpom = Bytes.toString(rowbys);
            //获取val
            String commstr = Bytes.toString(CellUtil.cloneQualifier(cell));
            String values = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(rowpom+"----"+commstr+"----"+values);
        }
    }
    scanner.close();
    table.close();
}
//删除一行数据
public static void start4(String tablename,String rowKey) throws IOException {
    Admin admin = connection.getAdmin();
    if(!admin.tableExists(TableName.valueOf(tablename))){
        return;
    }
    Table table = connection.getTable(TableName.valueOf(tablename));
    try {
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
    }finally {
        admin.close();
    }

}
//删除表
public static void start5(String tableName) throws IOException {
    //获取对象
    Admin admin = connection.getAdmin();
    try {
        if (!admin.tableExists(TableName.valueOf(tableName))){
            System.out.println("表不存在!");
            return;
        }
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
    }finally {
        admin.close();
    }
}

public static void main(String[] args) throws IOException {
    //创建表

// start1(“company”,“info”,“data”);
//添加数据
// start2(“company”,“001”,“info”,“name”,“Baidu”,“data”,“city”,“Beijing”);
// start2(“company”,“002”,“info”,“name”,“BTencent”,“data”,“city”,“Shenzhen”);
// start2(“company”,“003”,“info”,“name”,“HUAWEI”,“data”,“city”,“Dongguan”);
//查询数据
// start3(“company”,“001”,“004”);
// start3(“company”,“001”,“001”);
//删除一行数据
// start4(“company”,“002”);
//删除表
start5(“company”);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值