ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

hbase的Java基本操作

2020-10-19 20:31:26  阅读:196  来源: 互联网

标签:Java String column Bytes H1 tableName rowkey 基本操作 hbase


hbase的Java基本操作

建表,建列簇操作

private static Connection connection;
    private static Admin admin;

    public static void createTable(String tableName, String[] fields) throws IOException {
        if(admin.tableExists(TableName.valueOf(tableName)))
        {
            deleteTable(tableName);
        }
        //2.TableDescriptorBuilder.newBuilder构建表描述构建器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
        for (int i=0;i<fields.length;i++)
        {
            //3.创建列簇构造描述器
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(fields[i]));
            //4.构建列簇描述
            ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build();
            //建立表与列簇的关联关系
            tableDescriptorBuilder.setColumnFamily(cfDes);
        }
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
        admin.createTable(tableDescriptor);
    }
public static void main(String[] args) throws IOException {
        //用HBaseConfiguration.create();创建HBase的配置
        Configuration configuration = HBaseConfiguration.create();
        //用ConnectionFactory.createConnection创建HBase()连接
        connection = ConnectionFactory.createConnection(configuration);
        // 创建表,要给予HBase获取Admin对象
        admin = connection.getAdmin();
        String tablename = "WATER_BILL";
        //1.创建表和列
        String[] filed = {"H1", "H2", "H3"};
        createTable(tablename, filed);
    }

添加数据,指定表名,列簇列名,以及rowkey和对应的value

//添加数据
    public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        String []column=new String[110];
        String columnFamily="";
        for(int i=0;i<fields.length;i++)
        {
            String []split=fields[i].split(":");
            column[i]=split[1];
            columnFamily=split[0];
        }
        Put put = new Put(Bytes.toBytes(row));
        for(int i=0;i<values.length;i++)
        {
            put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column[i]),Bytes.toBytes(values[i]));
        }
        //5.使用htable表执行put操作
        table.put(put);
        //关闭htable表对象
        table.close();
    }

public static void main(String[] args) throws IOException {
        //用HBaseConfiguration.create();创建HBase的配置
        Configuration configuration = HBaseConfiguration.create();
        //用ConnectionFactory.createConnection创建HBase()连接
        connection = ConnectionFactory.createConnection(configuration);
        // 创建表,要给予HBase获取Admin对象
        admin = connection.getAdmin();
        String tablename = "WATER_BILL";
        //1.创建表和列
//        String[] filed = {"H1", "H2", "H3"};
//        createTable(tablename, filed);

        //2.向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。
        // 其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。
        // 例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,
        // 字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。
        String[] fields = {"H1:S_NO", "H1:S_Name", "H1:S_Sex", "H1:S_Age"};
        String[] values = {"2015001", "Zhangsan", "male", "23"};
        String row = "Zhangsan";
        addRecord(tablename, row, fields, values);
    }

查看表中某rowkey的列簇下的所有值,或者单个列簇:列名的值

public static void scanColumn(String tableName,String rowKey, String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        if(column.contains(":"))
        {
            //查询指定rowkey和列簇下的指定列名
            String[] split = column.split(":");
            get.addColumn(Bytes.toBytes(split[0]),Bytes.toBytes(split[1]));
            Result result = table.get(get);
            byte[] value = result.getValue(Bytes.toBytes(split[0]), Bytes.toBytes(split[1]));
            if(Bytes.toString(value)!=null)
                System.out.println(Bytes.toString(value));
            else
                System.out.println("null");
        }
        else
        {
            //查询指定rowkey和列簇下的所有数据
            get.addFamily(column.getBytes());
            Result result = table.get(get);
            Cell[] cells = result.rawCells();
            for (Cell cell:cells)
            {
                //获取列簇名称
                String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                //获取列的名称
                String colunmName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                //获取值
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                if(value!=null)
                    System.out.println(cf+":"+colunmName+"=>"+value);
                else
                    System.out.println(cf+":"+colunmName+"=>"+"null");
            }
        }
        table.close();
    }

public static void main(String[] args) throws IOException {
        //用HBaseConfiguration.create();创建HBase的配置
        Configuration configuration = HBaseConfiguration.create();
        //用ConnectionFactory.createConnection创建HBase()连接
        connection = ConnectionFactory.createConnection(configuration);
        // 创建表,要给予HBase获取Admin对象
        admin = connection.getAdmin();
        String tablename = "WATER_BILL";
        //1.创建表和列
//        String[] filed = {"H1", "H2", "H3"};
//        createTable(tablename, filed);

        //2.向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。
        // 其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。
        // 例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,
        // 字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。
//        String[] fields = {"H1:S_NO", "H1:S_Name", "H1:S_Sex", "H1:S_Age"};
//        String[] values = {"2015001", "Zhangsan", "male", "23"};
//        String row = "Zhangsan";
//        addRecord(tablename, row, fields, values);


        //3.浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。
        // 要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;
        // 当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
        String column = "H1:S_Name";
        String rowkey = "Zhangsan";
        scanColumn(tablename, rowkey, column);
    }

修改rowkey下的对应的单独的列簇:列名的值

public static void modifyData(String tableName,String rowkey,String column,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        //修改操作
        Put put = new Put(Bytes.toBytes(rowkey));
        String[] split = column.split(":");
        String columnFamily=split[0];
        String columnName=split[1];
        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));
        table.put(put);
        //查看修改后的数据
        Get get = new Get(rowkey.getBytes());
        get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
        Result result = table.get(get);
        byte[] value2 = result.getValue(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
        if(Bytes.toString(value2)!=null)
            System.out.println(columnFamily+":"+columnName+"=>"+Bytes.toString(value2));
        else
            System.out.println("null");
        System.out.println("修改成功!!");
        table.close();
    }

public static void main(String[] args) throws IOException {
        //用HBaseConfiguration.create();创建HBase的配置
        Configuration configuration = HBaseConfiguration.create();
        //用ConnectionFactory.createConnection创建HBase()连接
        connection = ConnectionFactory.createConnection(configuration);
        // 创建表,要给予HBase获取Admin对象
        admin = connection.getAdmin();
        String tablename = "WATER_BILL";
        //1.创建表和列
//        String[] filed = {"H1", "H2", "H3"};
//        createTable(tablename, filed);

        //2.向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。
        // 其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。
        // 例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,
        // 字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。
//        String[] fields = {"H1:S_NO", "H1:S_Name", "H1:S_Sex", "H1:S_Age"};
//        String[] values = {"2015001", "Zhangsan", "male", "23"};
//        String row = "Zhangsan";
//        addRecord(tablename, row, fields, values);


        //3.浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。
        // 要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;
        // 当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
//        String column = "H1:S_Name";
//        String rowkey = "Zhangsan";
//        scanColumn(tablename, rowkey, column);

        //4.modifyData(S修改表tableName,行row(可以用学生姓名S_Name表示),
        // 列column指定的单元格的数据tring tableName, String row, String column)
        String colum = "H1:S_Name";
        String rowkey = "Zhangsan";
        String value = "XiaoFengZai";
        modifyData(tablename, rowkey, colum, value);
    }

删除指定的rowkey内容

public static void deleteRow(String tableName, String row) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        //删除一条rowkey记录
        Delete delete = new Delete(Bytes.toBytes(row));
        table.delete(delete);
        table.close();
    }

    public static void main(String[] args) throws IOException {
        //用HBaseConfiguration.create();创建HBase的配置
        Configuration configuration = HBaseConfiguration.create();
        //用ConnectionFactory.createConnection创建HBase()连接
        connection = ConnectionFactory.createConnection(configuration);
        // 创建表,要给予HBase获取Admin对象
        admin = connection.getAdmin();
        String tablename = "WATER_BILL";
        //1.创建表和列
//        String[] filed = {"H1", "H2", "H3"};
//        createTable(tablename, filed);

        //2.向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。
        // 其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。
        // 例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,
        // 字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。
//        String[] fields = {"H1:S_NO", "H1:S_Name", "H1:S_Sex", "H1:S_Age"};
//        String[] values = {"2015001", "Zhangsan", "male", "23"};
//        String row = "Zhangsan";
//        addRecord(tablename, row, fields, values);


        //3.浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。
        // 要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;
        // 当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
//        String column = "H1:S_Name";
//        String rowkey = "Zhangsan";
//        scanColumn(tablename, rowkey, column);

        //4.modifyData(S修改表tableName,行row(可以用学生姓名S_Name表示),
        // 列column指定的单元格的数据tring tableName, String row, String column)
//        String colum = "H1:S_Name";
//        String rowkey = "Zhangsan";
//        String value = "XiaoFengZai";
//        modifyData(tablename, rowkey, colum, value);


        //5.删除指定的rowkey内容
        String rowkey="Zhangsan";
        deleteRow(tablename,rowkey);

        admin.close();
        connection.close();
    }

删除指定的表

public void deleteTable(String tablename)throws IOException
    {
        TableName tableName=TableName.valueOf("WATER_BILL");
        if(admin.tableExists(tableName))
        {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
    }

 

 

 

标签:Java,String,column,Bytes,H1,tableName,rowkey,基本操作,hbase
来源: https://www.cnblogs.com/xiaofengzai/p/13842821.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有