Java API 连接Hbase数据库并进行操作

Java API 对Hbase数据库进行操作
每个方法后面有其功能所对应的Hbase语句

package z_hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class a{

    private static Configuration config = null;
    private static Connection connection = null;
    private static Table table = null;

    public static void main(String[] args) throws Exception {
        config = HBaseConfiguration.create();

        config.set("hbase.zookeeper.quorum", "192.168.153.131");//单机

//		config.set("hbase.zookeeper.quorum", "master,work1,work2");// zookeeper地址
//		config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        connection = ConnectionFactory.createConnection(config);
        table = connection.getTable(TableName.valueOf("emp2014"));

//        testCrateTable();
//      testDropTable();
//         insertData();
//         scanDataByFilter1();
//        scanDataByFilter2();
//        testQualifierFilter();
//            scanData();
//        scanDatamany();
//    one();
    }
    
    public static void one() throws Exception {
        Scan scan = new Scan();
        SingleColumnValueFilter filter = new SingleColumnValueFilter(
                Bytes.toBytes("address"),
                Bytes.toBytes("country"),
                CompareOp.EQUAL, Bytes.toBytes("China"));

        filter.setFilterIfMissing(true);
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {

            System.out.println("rowkey:  " + new String(result.getRow()));   //获取结果列值
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("country"))));

        }
    }

    public static void testCrateTable() throws IOException {

        // 表管理类
        Admin admin = connection.getAdmin();// hbase表管理类
        TableName tableName = TableName.valueOf("emp2014");
        HTableDescriptor descriptor = new HTableDescriptor(tableName);

        // 创建列簇的描述类
        HColumnDescriptor family_info = new HColumnDescriptor("info");
        HColumnDescriptor family_address = new HColumnDescriptor("address");
        // 将列簇添加到表中
        descriptor.addFamily(family_info);
        descriptor.addFamily(family_address);

        admin.createTable(descriptor);

        admin.close();
        System.out.println("创建成功");
    }

    public static void testDropTable() throws IOException {     //disable 'emp2014'
        //drop 'emp2014'

        Admin admin = connection.getAdmin();// hbase表管理类
        TableName tableName = TableName.valueOf("emp2014");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
        admin.close();
        System.out.println("删除表成功");
    }


    public static void insertData() throws IOException {   // put 'table', '2014','info:name', 'lgy'
        // 表管理类
        Put put = new Put(Bytes.toBytes("31"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("1990-05-01"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("industry"), Bytes.toBytes("architect"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sal"), Bytes.toBytes("9000"));
        put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("city"), Bytes.toBytes("paris"));
        put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("country"), Bytes.toBytes("France"));

        Put put2014 = new Put(Bytes.toBytes("2014"));
        put2014.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lgy"));
        put2014.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("19"));
        put2014.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("2001-01-01"));
        put2014.addColumn(Bytes.toBytes("info"), Bytes.toBytes("industry"), Bytes.toBytes("student"));
        put2014.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sal"), Bytes.toBytes("9000"));
        put2014.addColumn(Bytes.toBytes("address"), Bytes.toBytes("city"), Bytes.toBytes("Zhengzhou"));
        put2014.addColumn(Bytes.toBytes("address"), Bytes.toBytes("country"), Bytes.toBytes("China"));
//        table.put(put);
        table.put(put2014);
        System.out.println("添加成功");
    }

    public static void scanDataByFilter2() throws Exception {   //get 'table', '2014'
        // 创建全表扫描的scan
        Scan scan = new Scan();
        // 匹配rowkey以wangsenfeng开头的
        RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("2014"));
        // 设置过滤器
        scan.setFilter(filter);
        // 打印结果集
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("birthday"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("industry"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sal"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("city"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("country"))));
        }
    }


    public static void scanDataByFilter1() throws Exception {
        //对表'emp2014'中,等值过滤条件:'address:country'为China的某几列:'info:name','info:age','info:sal','address:city'进行查询
        // 创建全表扫描的scan
        Scan scan = new Scan();
        // 过滤器:列值过滤器
        SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("address"), Bytes.toBytes("country"),
                CompareOp.EQUAL, Bytes.toBytes("China"));
        // 设置过滤器
        scan.setFilter(filter);
        // 打印结果集
        ResultScanner scanner = table.getScanner(scan);
        System.out.println(scanner);
        for (Result result : scanner) {
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sal"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("city"))));

        }
    }


    public static void testQualifierFilter() throws Exception {
        Scan scan = new Scan();
        QualifierFilter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("China")));
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            System.out.println("查询结果:");

            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("country"))));

        }
    }


    public static void scanData() throws Exception {    // scan 'table', {COLUMNS=> 'info:name'}
        Scan scan = new Scan();
        scan.addFamily(Bytes.toBytes("info")); //扫描指定family
        scan.addColumn(Bytes.toBytes("info"),
                Bytes.toBytes("name"));//扫描column指定

        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {

            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));

        }
    }


    public static void scanDatamany() throws Exception {    // scan 'emp2014', {COLUMNS => ['info:name','info:birthday']}
        Scan scan = new Scan();
        scan.addFamily(Bytes.toBytes("info")); //扫描指定family
        scan.addColumn(Bytes.toBytes("info"),
                Bytes.toBytes("name"));//扫描column指定
        scan.addColumn(Bytes.toBytes("info"),
                Bytes.toBytes("birthday"));//扫描column指定

        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {

            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
            System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("birthday"))));
        }
    }


}

参考网址:
https://www.cnblogs.com/wishyouhappy/p/3753760.html
https://cloud.tencent.com/developer/article/1336651
https://blog.csdn.net/aA518189/article/details/89190693

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值