HBase学习笔记(2)—— HBase Java API

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86568694

1 添加依赖

<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.7.0</version>
        </dependency>

在这里插入图片描述

2 API 测试

package hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IOUtils;


import java.io.IOException;

public class HBaseOperation {
    public static void main(String[] args) {

        String tableName = "user";
        HTable table = null;
        ResultScanner resultScanner = null;

        try {
            table = getHTableByTableName(tableName);

            Scan scan = new Scan();

            scan.setStartRow(Bytes.toBytes("10001"));
            scan.setStopRow(Bytes.toBytes("10003"));
            

            resultScanner = table.getScanner(scan);

            for (Result result : resultScanner) {
                System.out.println(Bytes.toString(result.getRow()));
                System.out.println(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(table);
            IOUtils.closeStream(resultScanner);
        }
    }


    public static HTable getHTableByTableName(String tableName) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        HTable table = new HTable(conf, tableName);

        return table;
    }

    /*
     * 实际开发时,建议 tablename & column family 写为 常量
     * */
    public static void putData() throws IOException {
        String tableName = "user";

        HTable table = getHTableByTableName(tableName);

        Put put = new Put(Bytes.toBytes("10004"));

        //add a column with a value
        put.add(
                Bytes.toBytes("info"),
                Bytes.toBytes("name"),
                Bytes.toBytes("Jenny")
        );

        put.add(
                Bytes.toBytes("info"),
                Bytes.toBytes("age"),
                Bytes.toBytes(24)
        );
        put.add(
                Bytes.toBytes("info"),
                Bytes.toBytes("address"),
                Bytes.toBytes("Beijing")
        );

        table.put(put);

        table.close();

    }

    public static void getData(HTable table) throws IOException {
        Get get = new Get(Bytes.toBytes("10002"));

        get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));

        Result result = table.get(get);

        /*
         * Key: rowkey + cf + c + version
         * Value : value
         * */
        for (Cell cell : result.rawCells()) {
            System.out.println(
                    Bytes.toString(CellUtil.cloneFamily(cell)) + ":"
                            + Bytes.toString(CellUtil.cloneQualifier(cell)) + " -> "
                            + Bytes.toString(CellUtil.cloneValue(cell))
            );
        }

        table.close();
    }


    public static void deleteData() throws IOException {

        String tableName = "user";

        HTable table = getHTableByTableName(tableName);
        Delete delete = new Delete(Bytes.toBytes("10004"));

        delete.addColumn(Bytes.toBytes("info"),
                Bytes.toBytes("address"));

        table.delete(delete);

        table.close();
    }

}

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/86568694
今日推荐