大数据(Hbase简单示例)

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;



public class HBaseCRUDExample {
    private static final String TABLE_NAME = "bl_test";
    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("info");

    public static void main(String[] args) {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.property.clientPort", "2181");
        config.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");

        try (Connection connection = ConnectionFactory.createConnection(config)) {
            TableName tableName = TableName.valueOf(TABLE_NAME);
            Table table = connection.getTable(tableName);

            Admin admin = connection.getAdmin();
            createTable(admin, TABLE_NAME, COLUMN_FAMILY);

            // 插入数据
            putData(table, "row1", "name", "John");
            putData(table, "row1", "age", "25");

            // 查询数据
            getData(table, "row1", "name");
            getData(table, "row1", "age");

            // 更新数据
            putData(table, "row1", "age", "26");

            // 再次查询数据
            getData(table, "row1", "name");
            getData(table, "row1", "age");

            // 删除数据
            deleteData(table, "row1", "age");

            // 再次查询数据
            getData(table, "row1", "name");
            getData(table, "row1", "age");

            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void createTable(Admin admin, String tableName, byte[] columnFamily) throws IOException {
        TableName table = TableName.valueOf(tableName);

        if (admin.tableExists(table)) {
            System.out.println("Table already exists: " + tableName);
        } else {
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(table);
            ColumnFamilyDescriptorBuilder columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(columnFamily);
            tableDescriptor.setColumnFamily(columnFamilyDescriptor.build());

            admin.createTable(tableDescriptor.build());
            System.out.println("Table created: " + tableName);
        }
    }

    private static void putData(Table table, String rowKey, String column, String value) throws IOException {
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(COLUMN_FAMILY, Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);
        System.out.println("Data inserted. Row: " + rowKey + ", Column: " + column + ", Value: " + value);
    }

    private static void getData(Table table, String rowKey, String column) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        byte[] value = result.getValue(COLUMN_FAMILY, Bytes.toBytes(column));
        if (value != null) {
            String columnValue = Bytes.toString(value);
            System.out.println("Data retrieved. Row: " + rowKey + ", Column: " + column + ", Value: " + columnValue);
        } else {
            System.out.println("Data not found. Row: " + rowKey + ", Column: " + column);
        }
    }

    private static void deleteData(Table table, String rowKey, String column) throws IOException {
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumn(COLUMN_FAMILY, Bytes.toBytes(column));
        table.delete(delete);
        System.out.println("Data deleted. Row: " + rowKey + ", Column: " + column);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值