HBase实验1:Java API读写HBase

概要

1)如何获取hbase的连接信息

&:hbase的连接信息类似jdbc地址,可通过配置文件,也可写在程序里。

  1. 写在程序里:
    Configuration conf=HBaseConfiguration.create();
    conf.set(“hbase.zookeeper.quorum”,“127.0.0.1”);
  2. 通过固定位置的配置文件
    conf.addResource(new Path(“F:\hbase-1.3.3\conf\hbase-site.xml”));
  3. 默认值
    hbase-common-x.x.x.jar中有默认的配置文件,hbase-default.xml。
    值是localhost。
    4.property文件指定

2)java api的使用

通过api的使用,更深切的体会了nosql数据库的叫法的意义。
操作全部通过api完成,比jdbc写sql麻烦一些。

3)常用对象

  1. hadoop.conf.Configuration:配置,读取xml,获取hbase地址。
  2. hadoop.hbase.client.HBaseAdmin:管理,建表、使能、删表等管理操作。
  3. hadoop.hbase.client.HTablePool:
  4. hadoop.hbase.client.Put:插入
  5. hadoop.hbase.client.Delete:删除
  6. hadoop.hbase.client.Scan:查询,全表、按行键过滤、按列值过滤。

示例代码

package com.demo.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CollectionUtils;
import org.apache.hadoop.hbase.util.Strings;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.log4j.PropertyConfigurator;

import java.io.IOException;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;

public class HBaseDemo {
    public static Configuration config;
    static {
        config=HBaseConfiguration.create();
        config.addResource(new Path("F:\\hbase-1.3.3\\conf\\hbase-site.xml"));
    }

    /**
     * 创建表
     * @param tableName
     *        表名
     * @param colFmlys
     *        列族
     * @throws Exception
     */
    public static void createTable(String tableName,String[] colFmlys)
        throws IOException
    {
        HBaseAdmin hbaseAdmin=new HBaseAdmin(config);
        if(hbaseAdmin.tableExists(tableName)){
            System.out.print("表已经存在");
            return;
        }
        HTableDescriptor desc=new HTableDescriptor(tableName);
        for(String cf :colFmlys){
            desc.addFamily(new HColumnDescriptor(cf));
        }
        hbaseAdmin.createTable(desc);
        System.out.println("建表成功");
    }

    /**
     * 插入数据
     * @param tabname 表名
     * @throws Exception
     */
    public static void insert(String tabname,String rowkey,String colfmly,String col,String val){
        System.out.println("******* start insert *******");
        HTablePool pool=new HTablePool(config,1000);
        Put put=new Put(rowkey.getBytes());
        put.add(colfmly.getBytes(),col.getBytes(),val.getBytes());
        try{
            pool.getTable(tabname).put(put);
        }catch(IOException e){
            e.printStackTrace();
        }
        System.out.println("******* end insert *******");
    }

    /**
     * 根据rowkey删除记录
     * @param tabname
     * @param rowkey
     * @throws Exception
     */
    public static void delrow(String tabname,String rowkey){
        try{
            HTable table=new HTable(config,tabname);
            List list =new ArrayList();
            Delete del=new Delete(rowkey.getBytes());
            list.add(del);
            table.delete(list);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 查询全表
     * @param tabname 表名
     * @throws Exception
     */
    public static void queryAll(String tabname){
        HTablePool pool =new HTablePool(config,1000);
        try {
            ResultScanner rs =pool.getTable(tabname).getScanner(new Scan());
            for(Result r:rs){
                System.out.println("rowkey:"+new String(r.getRow()));
                for(KeyValue kv:r.raw()){
                    System.out.println("列族:"+new String(kv.getFamily())+"====列:"+new String(kv.getQualifier())+"====值:"+new String(kv.getValue()));
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }

    }

    /**
     * 用rowkey过滤
     * @param tabname
     * @throws Exception
     */
    public static void queryRange(String tabname,String startRk,String endRk){
        HTablePool pool =new HTablePool(config,1000);
        Scan scan=new Scan(startRk.getBytes(),endRk.getBytes());
        try {
            ResultScanner rs =pool.getTable(tabname).getScanner(scan);
            for(Result r:rs){
                System.out.println("rowkey:"+new String(r.getRow()));
                for(KeyValue kv:r.raw()){
                    System.out.println("列族:"+new String(kv.getFamily())+"====列:"+new String(kv.getQualifier())+"====值:"+new String(kv.getValue()));
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 用filter过滤
     * @param tabname
     * @throws Exception
     */
    public static void queryFilter(String tabname,String col,String regexp){
        HTablePool pool =new HTablePool(config,1000);
        Scan scan=new Scan();
        scan.addColumn(new String("info").getBytes(),col.getBytes());
        Filter f=new ValueFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*"+regexp+"*."));
        scan.setFilter(f);
        try {
            ResultScanner rs =pool.getTable(tabname).getScanner(scan);
            for(Result r:rs){
                System.out.println("rowkey:"+new String(r.getRow()));
                for(KeyValue kv:r.raw()){
                    System.out.println("列族:"+new String(kv.getFamily())+"====列:"+new String(kv.getQualifier())+"====值:"+new String(kv.getValue()));
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
        try {
//            Configuration conf = HBaseConfiguration.create();
            //hbase连接信息
            //1 直接写到代码里
//            conf.set("hbase.zookeeper.quorum", "127.0.0.1");
            //2 引用hbase-site.xml
//            conf.addResource(new Path("F:\\hbase-1.3.3\\conf\\hbase-site.xml"));

//            conf.addResource(new Path("hbase-site.xml"));

//            Properties prop=new Properties();
//            PropertyConfigurator.configure("hbase.properties");


            //验证配置值
//            String quorum=conf.get("hbase.zookeeper.quorum");
//            System.out.println(quorum);
//            HTable table = new HTable(conf, "table");
//            Put put = new Put(Bytes.toBytes("row1607"));
//            put.add(Bytes.toBytes("column_famaly1"), Bytes.toBytes("col1"), Bytes.toBytes("val1"));
//            table.put(put);
            String[] colfmly={"info","twit"};
            createTable("tabapi",colfmly);
            insert("tabapi","rowkey2","info","name","tim");
            insert("tabapi","rowkey2","info","age","25");
            queryAll("tabapi");
            System.out.println("******* queryRange start*******");
            queryRange("tabapi","rowkey1","rowkey7");
            System.out.println("******* queryRange end*******");

            System.out.println("******* queryFilter start*******");
            queryFilter("tabapi","name","tim");
            System.out.println("******* queryFilter end*******");

            System.out.println("compelete");
        } catch (Exception e){
            e.printStackTrace();
        }

    }

}

参考:
1)https://www.cnblogs.com/kxdblog/p/4034248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值