2024.9.20

成功连接hbase并进行表的增删改查

package com.example.hbasetest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HbaseCRUD {
    private static Configuration configuration;
    private static Connection connection;
    private static Admin admin;

    public HbaseCRUD() {
        init();
    }

    /**
     * 建立连接
     */
    public void init(){
        configuration=HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","192.168.43.183"); // 换成你自己的IP
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        try{
            connection=ConnectionFactory.createConnection(configuration);
            admin=connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 关闭连接
     */
    public void close(){
        try{
            if(admin!=null)
                admin.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 创建表
     * @param myTableName 表名
     * @param colFamily 列族数组
     * @throws IOException
     */
    public void createTable(String myTableName,String[]colFamily)throws IOException{
        TableName tablename = TableName.valueOf(myTableName);
        if(admin.tableExists(tablename)){
            System.out.println("表已存在,删除旧表");
            admin.disableTable(tablename);//使表无效
            admin.deleteTable(tablename);//删表
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
        for(String str:colFamily){  //增加一列
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor); //建表
        System.out.println("建表成功");
    }

    /**
     * 插入数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param fields 列族(或列族:列限定符)
     * @param values  值
     * @throws IOException
     */
    public void addRecord(String tableName,String rowKey,String []fields,String [] values) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        for (int i = 0; i < fields.length; i++) {
            Put put = new Put(rowKey.getBytes());
            String [] cols = fields[i].split(":");
            if(cols.length == 1)
                put.addColumn(cols[0].getBytes(), "".getBytes(), values[i].getBytes());
            else
                put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
            table.put(put);
        }
        table.close();
    }

    /**
     * 查询数据
     * @param tableName 表名
     * @param column 列族(或列族:列限定符)
     * @throws IOException
     */
    public void scanColumn (String tableName,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        String [] cols = column.split(":");
        if(cols.length==1)
            scan.addFamily(column.getBytes());
        else
            scan.addColumn(Bytes.toBytes(cols[0]), cols[1].getBytes());
        ResultScanner scanner = table.getScanner(scan);
        for (Result result = scanner.next(); result !=null;result = scanner.next()) {
            for(Cell cell:result.rawCells()){
                System.out.print("列族:"+new String(CellUtil.cloneFamily(cell)));
                System.out.print(",行键:"+new String(CellUtil.cloneRow(cell)));
                System.out.print(",列名:"+new String(CellUtil.cloneQualifier(cell)));
                System.out.print(",值:"+new String(CellUtil.cloneValue(cell)));
                System.out.println();
            }
        }
        table.close();
    }

    /**
     * 修改数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param column 列族(或列族:列限定符)
     * @param value 值
     * @throws IOException
     */
    public void modifyData(String tableName,String rowKey,String column,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        String [] cols = column.split(":");
        if(cols.length==1)
            put.addColumn(column.getBytes(),"".getBytes() , value.getBytes());
        else
            put.addColumn(cols[0].getBytes(),cols[1].getBytes() , value.getBytes());
        table.put(put);
        table.close();
    }

    /**
     * 删除数据
     * @param tableName 表名
     * @param rowKey 行键
     * @throws IOException
     */
    public void deleteRow(String tableName,String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowKey.getBytes());
        table.delete(delete);
        table.close();
    }
}
package com.example.hbasetest;

import java.io.IOException;

public class Client {
    public static void main(String[] args) throws IOException {
        HbaseCRUD hbaseCRUD = new HbaseCRUD();
        String[] cols=new String[]{"S_No","S_Name","S_Sex","S_Age"};
        //建表
        hbaseCRUD.createTable("student1",cols);
        //插入
        hbaseCRUD.addRecord("student","2015001",cols,new String[]{"2015001","Zhangsan","male","23"});
        hbaseCRUD.addRecord("student","2015002",cols,new String[]{"2015002","Mary","female","22"});
        hbaseCRUD.addRecord("student","2015003",cols,new String[]{"2015003","Lisa","male","24"});
        //查询
        hbaseCRUD.scanColumn("student","S_Name");
        //修改
        hbaseCRUD.modifyData("student","2015001","S_Age","22");
        hbaseCRUD.scanColumn("student","S_Age");
        //删除
        hbaseCRUD.deleteRow("student","2015001");
        hbaseCRUD.scanColumn("student","S_No");
        hbaseCRUD.close();
        System.out.println("记得一键三连~");
    }
}

 

posted @ 2024-12-25 01:01  cvjj  阅读(3)  评论(0)    收藏  举报