【Hbase学习之三】Hbase Java API

环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  hadoop-2.6.5
  hbase-0.98.12.1-hadoop2

建立一个java工程

导入hadoop 相关jar
导入hbase相关jar

使用客户端(java API)操作hbase

示例一

package hbase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import hbase.Phone.PhoneDetail;
public class HBaseDemo {

    HBaseAdmin admin;
    HTable htable;
    String TN = "phone";

    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
        //ZK配置 用于连接hbase集群
        conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
        //HBaseAdmin属于数据库对象
        admin = new HBaseAdmin(conf);
        //HTable 表对象  非线程安全的
        htable = new HTable(conf, TN.getBytes());
    }

    @Test
    public void creatTable() throws Exception {

        if (admin.tableExists(TN)) {
            admin.disableTable(TN);
            admin.deleteTable(TN);
        }

        //表描述
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
        //列族
        HColumnDescriptor cf = new HColumnDescriptor("cf".getBytes());
        desc.addFamily(cf);
        //创建表
        admin.createTable(desc);
    }

    /**
     * 写入数据
     */
    @Test
    public void insertDB() throws Exception {
        String rowKey = "1231231312";
        Put put = new Put(rowKey.getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "xiaohong".getBytes());
        put.add("cf".getBytes(), "age".getBytes(), "23".getBytes());
        put.add("cf".getBytes(), "sex".getBytes(), "women".getBytes());
        htable.put(put);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 有10个用户,每个用户随机产生100条记录
     *
     * @throws Exception
     */
    @Test
    public void insertDB2() throws Exception {
        List<Put> puts = new ArrayList<Put>();
        for (int i = 0; i < 10; i++) {
            String phoneNum = getPhoneNum("186");
            for (int j = 0; j < 100; j++) {
                //对方号码
                String dnum = getPhoneNum("158");
                //通话时长
                String length = r.nextInt(99) + "";
                //主叫 被叫类型
                String type = r.nextInt(2) + "";
                //时间
                String dateStr = getDate("2018");
                String rowkey = phoneNum + "_" + (Long.MAX_VALUE - sdf.parse(dateStr).getTime());
                Put put = new Put(rowkey.getBytes());
                put.add("cf".getBytes(), "dnum".getBytes(), dnum.getBytes());
                put.add("cf".getBytes(), "length".getBytes(), length.getBytes());
                put.add("cf".getBytes(), "type".getBytes(), type.getBytes());
                put.add("cf".getBytes(), "date".getBytes(), dateStr.getBytes());
                puts.add(put);
            }
        }
        htable.put(puts);
    }

    @Test
    public void insertDB3() throws Exception {
        List<Put> puts = new ArrayList<Put>();
        for (int i = 0; i < 10; i++) {
            String phoneNum = getPhoneNum("186");
            for (int j = 0; j < 100; j++) {
                String dnum = getPhoneNum("158");
                String length = r.nextInt(99) + "";
                String type = r.nextInt(2) + "";
                String dateStr = getDate("2018");
                String rowkey = phoneNum + "_" + (Long.MAX_VALUE - sdf.parse(dateStr).getTime());
                Phone2.PhoneDetail.Builder phoneDetail = Phone2.PhoneDetail.newBuilder();
                phoneDetail.setDate(dateStr);
                phoneDetail.setDnum(dnum);
                phoneDetail.setLength(length);
                phoneDetail.setType(type);
                Put put = new Put(rowkey.getBytes());
                put.add("cf".getBytes(), "phoneDetail".getBytes(), phoneDetail.build().toByteArray());
                puts.add(put);
            }
        }
        htable.put(puts);
    }

    /**
     * 有十个用户,每个用户每天产生100条记录,将100条记录放到一个集合进行存储
     *
     * @throws Exception
     */
    @Test
    public void insertDB4() throws Exception {
        List<Put> puts = new ArrayList<Put>();
        for (int i = 0; i < 10000; i++) {
            String phoneNum = getPhoneNum("186");
            String rowkey = phoneNum + "_" + (Long.MAX_VALUE - sdf.parse(getDate2("20180529")).getTime());
            Phone.dayPhoneDetail.Builder dayPhone = Phone.dayPhoneDetail.newBuilder();
            for (int j = 0; j < 100; j++) {
                String dnum = getPhoneNum("158");
                String length = r.nextInt(99) + "";
                String type = r.nextInt(2) + "";
                String dateStr = getDate("2018");
                Phone.PhoneDetail.Builder phoneDetail = Phone.PhoneDetail.newBuilder();
                phoneDetail.setDate(dateStr);
                phoneDetail.setDnum(dnum);
                phoneDetail.setLength(length);
                phoneDetail.setType(type);
                dayPhone.addDayPhoneDetail(phoneDetail);
            }
            Put put = new Put(rowkey.getBytes());
            put.add("cf".getBytes(), "day".getBytes(), dayPhone.build().toByteArray());
            puts.add(put);
        }
        htable.put(puts);
    }

    @Test
    public void getDB2() throws Exception{
        Get get = new Get("18686966381_9223370509257224807".getBytes());
        Result result = htable.get(get);
        Cell cell = result.getColumnLatestCell("cf".getBytes(), "day".getBytes());
        Phone.dayPhoneDetail dayPhone = Phone.dayPhoneDetail.parseFrom(CellUtil.cloneValue(cell));
        for (PhoneDetail pd : dayPhone.getDayPhoneDetailList()) {
            System.out.println(pd);
        }
    }

    private String getDate(String year) {
        return year + String.format("%02d%02d%02d%02d%02d",
                new Object[] { r.nextInt(12) + 1, r.nextInt(31) + 1, r.nextInt(24), r.nextInt(60), r.nextInt(60) });
    }

    private String getDate2(String yearMonthDay) {
        return yearMonthDay
                + String.format("%02d%02d%02d", new Object[] { r.nextInt(24), r.nextInt(60), r.nextInt(60) });
    }

    Random r = new Random();

    /**
     * 生成随机的手机号码
     *
     * @param string
     * @return
     */
    private String getPhoneNum(String string) {
        return string + String.format("%08d", r.nextInt(99999999));
    }

    @Test
    public void getDB() throws Exception {
        String rowKey = "1231231312";
        Get get = new Get(rowKey.getBytes());
        //设置过滤条件
        get.addColumn("cf".getBytes(), "name".getBytes());
        get.addColumn("cf".getBytes(), "age".getBytes());
        get.addColumn("cf".getBytes(), "sex".getBytes());
        Result rs = htable.get(get);
        //rs.advance();//true if the next cell is found and CellScanner.current() will return a valid Cell
        Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
        Cell cell2 = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
        Cell cell3 = rs.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
        // System.out.println(new String(cell.getValue()));
        System.out.println(new String(CellUtil.cloneValue(cell)));
        System.out.println(new String(CellUtil.cloneValue(cell2)));
        System.out.println(new String(CellUtil.cloneValue(cell3)));

    }

    /**
     * 统计二月份到三月份的通话记录
     *
     * @throws Exception
     */
    @Test
    public void scan() throws Exception {
        String phoneNum = "18676604687";
        String startRow = phoneNum + "_" + (Long.MAX_VALUE - sdf.parse("20180301000000").getTime());
        String stopRow = phoneNum + "_" + (Long.MAX_VALUE - sdf.parse("20180201000000").getTime());
        //遍历取值
        Scan scan = new Scan();
        scan.setStartRow(startRow.getBytes());
        scan.setStopRow(stopRow.getBytes());
        ResultScanner rss = htable.getScanner(scan);
        for (Result rs : rss) {
            System.out
                    .print(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes()))));
            System.out.print("-"
                    + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "length".getBytes()))));
            System.out.print(
                    "-" + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "type".getBytes()))));
            System.out.println(
                    "-" + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "date".getBytes()))));
        }
    }

    /**
     * 查询某个手机号主叫为1 的所有记录
     *
     * @throws Exception
     */
    @Test
    public void scan2() throws Exception {
        FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        //添加过滤条件
        //前缀条件
        PrefixFilter filter1 = new PrefixFilter("18676604687".getBytes());
        //单列值匹配过滤
        SingleColumnValueFilter filter2 =
                new SingleColumnValueFilter("cf".getBytes(),
                        "type".getBytes(),
                        CompareOp.EQUAL,
                        "1".getBytes());
        list.addFilter(filter1);
        list.addFilter(filter2);

        Scan scan = new Scan();
        scan.setFilter(list);
        ResultScanner rss = htable.getScanner(scan);
        for (Result rs : rss) {
            System.out
                    .print(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes()))));
            System.out.print("-"
                    + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "length".getBytes()))));
            System.out.print(
                    "-" + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "type".getBytes()))));
            System.out.println(
                    "-" + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "date".getBytes()))));
        }
    }

    /**
     * 关闭数据库
     */
    @After
    public void destory() throws Exception {
        if (admin != null) {
            admin.close();
        }
    }
}

示例二

package hbase;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class HBaseDAOImp {

    HConnection hTablePool = null;
    static Configuration conf =null;
    public HBaseDAOImp()
    {
         conf = new Configuration();
        String zk_list = "node1,node2,node3";
        conf.set("hbase.zookeeper.quorum", zk_list);
        try {
            hTablePool = HConnectionManager.createConnection(conf) ;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void save(Put put, String tableName) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        try {
            table = hTablePool.getTable(tableName) ;
            table.put(put) ;

        } catch (Exception e) {
            e.printStackTrace() ;
        }finally{
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 插入一个cell
     * @param tableName
     * @param rowKey
     * @param family
     * @param quailifer
     * @param value
     */
    public void insert(String tableName, String rowKey, String family,
            String quailifer, String value) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        try {
            table = hTablePool.getTable(tableName) ;
            Put put = new Put(rowKey.getBytes());
            put.add(family.getBytes(), quailifer.getBytes(), value.getBytes()) ;
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        }finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 在一个列族下插入多个单元格
     * @param tableName
     * @param rowKey
     * @param family
     * @param quailifer
     * @param value
     */
    public void insert(String tableName,String rowKey,String family,String quailifer[],String value[])
    {
        HTableInterface table = null;
        try {
            table = hTablePool.getTable(tableName) ;
            Put put = new Put(rowKey.getBytes());
            // 批量添加
            for (int i = 0; i < quailifer.length; i++) {
                String col = quailifer[i];
                String val = value[i];
                put.add(family.getBytes(), col.getBytes(), val.getBytes());
            }
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        }finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void save(List<Put> Put, String tableName) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        try {
            table = hTablePool.getTable(tableName) ;
            table.put(Put) ;
        }
        catch (Exception e) {
            // TODO: handle exception
        }finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public Result getOneRow(String tableName, String rowKey) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        Result rsResult = null;
        try {
            table = hTablePool.getTable(tableName) ;
            Get get = new Get(rowKey.getBytes()) ;
            rsResult = table.get(get) ;
        } catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return rsResult;
    }

    /**
     * 最常用的方法,优化查询
     * 查询一行数据,
     * @param tableName
     * @param rowKey
     * @param cols
     * @return
     */
    public Result getOneRowAndMultiColumn(String tableName, String rowKey,String[] cols) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        Result rsResult = null;
        try {
            table = hTablePool.getTable(tableName) ;
            Get get = new Get(rowKey.getBytes()) ;
            for (int i = 0; i < cols.length; i++) {
                get.addColumn("cf".getBytes(), cols[i].getBytes()) ;
            }
            rsResult = table.get(get) ;
        } catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return rsResult;
    }

    public List<Result> getRows(String tableName, String rowKeyLike) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        List<Result> list = null;
        try {
            FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            table = hTablePool.getTable(tableName) ;
            PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());
            SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
                      "order".getBytes(),
                      "order_type".getBytes(),
                      CompareOp.EQUAL,
                      Bytes.toBytes("1")
                      );
            fl.addFilter(filter);
            fl.addFilter(filter1);
            Scan scan = new Scan();
            scan.setFilter(fl);
            ResultScanner scanner = table.getScanner(scan) ;
            list = new ArrayList<Result>() ;
            for (Result rs : scanner) {
                list.add(rs) ;
            }
        } catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public List<Result> getRows(String tableName, String rowKeyLike ,String cols[]) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        List<Result> list = null;
        try {
            table = hTablePool.getTable(tableName) ;
            PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());

            Scan scan = new Scan();
            for (int i = 0; i < cols.length; i++) {
                scan.addColumn("cf".getBytes(), cols[i].getBytes()) ;
            }
            scan.setFilter(filter);
            ResultScanner scanner = table.getScanner(scan) ;
            list = new ArrayList<Result>() ;
            for (Result rs : scanner) {
                list.add(rs) ;
            }
        } catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public List<Result> getRowsByOneKey(String tableName, String rowKeyLike ,String cols[]) {
        // TODO Auto-generated method stub
        HTableInterface table = null;
        List<Result> list = null;
        try {
            table = hTablePool.getTable(tableName) ;
            PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());

            Scan scan = new Scan();
            for (int i = 0; i < cols.length; i++) {
                scan.addColumn("cf".getBytes(), cols[i].getBytes()) ;
            }
            scan.setFilter(filter);
            ResultScanner scanner = table.getScanner(scan) ;
            list = new ArrayList<Result>() ;
            for (Result rs : scanner) {
                list.add(rs) ;
            }
        } catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 范围查询
     * @param tableName
     * @param startRow
     * @param stopRow
     * @return
     */
    public List<Result> getRows(String tableName,String startRow,String stopRow)
    {
        HTableInterface table = null;
        List<Result> list = null;
        try {
            table = hTablePool.getTable(tableName) ;
            Scan scan = new Scan() ;
            scan.setStartRow(startRow.getBytes()) ;
            scan.setStopRow(stopRow.getBytes()) ;
            ResultScanner scanner = table.getScanner(scan) ;
            list = new ArrayList<Result>() ;
            for (Result rsResult : scanner) {
                list.add(rsResult) ;
            }

        }catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public void deleteRecords(String tableName, String rowKeyLike){
        HTableInterface table = null;
        try {
            table = hTablePool.getTable(tableName) ;
            PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());
            Scan scan = new Scan();
            scan.setFilter(filter);
            ResultScanner scanner = table.getScanner(scan) ;
            List<Delete> list = new ArrayList<Delete>() ;
            for (Result rs : scanner) {
                Delete del = new Delete(rs.getRow());
                list.add(del) ;
            }
            table.delete(list);
        }
        catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void deleteCell(String tableName, String rowkey,String cf,String column){
        HTableInterface table = null;
        try {
            table = hTablePool.getTable(tableName) ;
            Delete del = new Delete(rowkey.getBytes());
            del.deleteColumn(cf.getBytes(), column.getBytes());
            table.delete(del);
        }
        catch (Exception e) {
            e.printStackTrace() ;
        }
        finally
        {
            try {
                table.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public   void createTable(String tableName, String[] columnFamilys){
        try {
            // admin 对象
            HBaseAdmin admin = new HBaseAdmin(conf);
            if (admin.tableExists(tableName)) {
                System.err.println("此表,已存在!");
            } else {
                HTableDescriptor tableDesc = new HTableDescriptor(
                        TableName.valueOf(tableName));

                for (String columnFamily : columnFamilys) {
                    tableDesc.addFamily(new HColumnDescriptor(columnFamily));
                }

                admin.createTable(tableDesc);
                System.err.println("建表成功!");

            }
            admin.close();// 关闭释放资源
        } catch (MasterNotRunningException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 删除一个表
     *
     * @param tableName
     *            删除的表名
     * */
    public   void deleteTable(String tableName)   {
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            if (admin.tableExists(tableName)) {
                admin.disableTable(tableName);// 禁用表
                admin.deleteTable(tableName);// 删除表
                System.err.println("删除表成功!");
            } else {
                System.err.println("删除的表不存在!");
            }
            admin.close();
        } catch (MasterNotRunningException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 查询表中所有行
     * @param tablename
    */
    public void scaner(String tablename) {
    try {
            HTable table =new HTable(conf, tablename);
            Scan s =new Scan();
//            s.addColumn(family, qualifier)
//            s.addColumn(family, qualifier)
            ResultScanner rs = table.getScanner(s);
    for (Result r : rs) {

           for(Cell cell:r.rawCells()){
                System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
                System.out.println("Timetamp:"+cell.getTimestamp()+" ");
                System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
                System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
                System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
               }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void scanerByColumn(String tablename) {

        try {
            HTable table =new HTable(conf, tablename);
            Scan s =new Scan();
            s.addColumn("cf".getBytes(), "201504052237".getBytes());
            s.addColumn("cf".getBytes(), "201504052237".getBytes());
            ResultScanner rs = table.getScanner(s);
            for (Result r : rs) {

                for(Cell cell:r.rawCells()){
                    System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
                    System.out.println("Timetamp:"+cell.getTimestamp()+" ");
                    System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
                    System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
                    System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {

//        创建表
//        String tableName="test";
//        String cfs[] = {"cf"};
//        dao.createTable(tableName,cfs);

//        存入一条数据
//        Put put = new Put("bjsxt".getBytes());
//        put.add("cf".getBytes(), "name".getBytes(), "cai10".getBytes()) ;
//        dao.save(put, "test") ;

//        插入多列数据
//        Put put = new Put("bjsxt".getBytes());
//        List<Put> list = new ArrayList<Put>();
//        put.add("cf".getBytes(), "addr".getBytes(), "shanghai1".getBytes()) ;
//        put.add("cf".getBytes(), "age".getBytes(), "30".getBytes()) ;
//        put.add("cf".getBytes(), "tel".getBytes(), "13889891818".getBytes()) ;
//        list.add(put) ;
//        dao.save(list, "test");

//        插入单行数据
//        dao.insert("test", "testrow", "cf", "age", "35") ;
//        dao.insert("test", "testrow", "cf", "cardid", "12312312335") ;
//        dao.insert("test", "testrow", "cf", "tel", "13512312345") ;

//        List<Result> list = dao.getRows("test", "testrow",new String[]{"age"}) ;
//        for(Result rs : list)
//        {
//            for(Cell cell:rs.rawCells()){
//                System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
//                System.out.println("Timetamp:"+cell.getTimestamp()+" ");
//                System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
//                System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
//                System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
//               }
//        }

//        Result rs = dao.getOneRow("test", "testrow");
//         System.out.println(new String(rs.getValue("cf".getBytes(), "age".getBytes())));

//        Result rs = dao.getOneRowAndMultiColumn("cell_monitor_table", "29448-513332015-04-05", new String[]{"201504052236","201504052237"});
//        for(Cell cell:rs.rawCells()){
//            System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
//            System.out.println("Timetamp:"+cell.getTimestamp()+" ");
//            System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
//            System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
//            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
//           }

//        dao.deleteTable("cell_monitor_table");
//        创建表
        String tableName="cell_monitor_table";
        String cfs[] = {"cf"};
//        dao.createTable(tableName,cfs);
    }

      public static void testRowFilter(String tableName){
             try {
                  HTable table =new HTable(conf, tableName);
                Scan scan = new Scan();
                scan.addColumn(Bytes.toBytes("column1"),Bytes.toBytes("qqqq"));
                Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("laoxia157")));
                scan.setFilter(filter1);
                ResultScanner scanner1 = table.getScanner(scan);
                for (Result res : scanner1) {
                    System.out.println(res);
                }
                scanner1.close();

//
//                Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("laoxia4\\d{2}"));
//                scan.setFilter(filter2);
//                ResultScanner scanner2 = table.getScanner(scan);
//                for (Result res : scanner2) {
//                     System.out.println(res);
//                }
//                scanner2.close();

                Filter filter3 = new RowFilter( CompareOp.EQUAL,new SubstringComparator("laoxia407"));
                scan.setFilter(filter3);
                ResultScanner scanner3 = table.getScanner(scan);
                for (Result res : scanner3) {
                      System.out.println(res);
                }
                scanner3.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

      @Test
      public void testTrasaction(){
          try{
              HTableInterface table = null;
              table = hTablePool.getTable("t_test".getBytes());
//              Put put1 =new Put("002".getBytes());
//              put1.add("cf1".getBytes(), "name".getBytes(), "王五".getBytes());
//              table.put(put1);
              Put newput =new Put("001".getBytes());
              newput.add("cf1".getBytes(), "like".getBytes(), "看书".getBytes());

              boolean f= table.checkAndPut("001".getBytes(), "cf1".getBytes(), "age".getBytes(), "24".getBytes(), newput);
              System.out.println(f);

          }catch (Exception e){
              e.printStackTrace();
          }

      }
}

官网API:http://hbase.apache.org/2.1/apidocs/index.html
参考:https://www.cnblogs.com/liuwei6/p/6842536.html

原文地址:https://www.cnblogs.com/cac2020/p/10449674.html

时间: 2024-11-05 05:41:57

【Hbase学习之三】Hbase Java API的相关文章

HBase学习笔记-HBase shell常用命令

扫表的时候限定行数 scan 'TABLE_NAME', { LIMIT => 5 } 即为扫表的时候,限定只输出五条数据 ============================================== 删表 删表需要执行两个步骤,即disable和drop,具体如下: disable 'TABLE_NAME' drop 'TABLE_NAME' 当然disable后的表还可以重新enable起来,命令为: enable 'TABLE_NAME' =================

Android学习八---OpenCV JAVA API

OpenCV java API的文档说明在OpenCV-2.4.10-android-sdk/sdk/java/javadoc/index.html的文件夹下. 想用java API的方式进行OpenCV4android 应用开发还是挺简单,首先就这些API先熟悉一下,然后对自己要开发的应用设计好流程,需要用到什么的数据结构进行存储,用到什么算法.然后对算法进行了解,输入参数是什么,输出参数是什么.有哪些fields和methods. 1.Packages:org.opencv.core Cor

HBase 二次开发 java api和demo

1. 试用thrift python/java以及hbase client api,结论如下: 1.1 thrift的安装和发布繁琐,可能会遇到未知的错误,且hbase.thrift的版本在变化中.优点代码简单,需要打包的内容少. 1.2 hbase client api,需要的jar很多,发布版的容量也很大,打包后近百兆.优点是,明确,无歧义. 2. 推荐用hbase client api的方式搞定. 3. 以下均为技术细节. 4. 有一台机器/一个集群,在运行hadoop,也运行了基于这个h

Hbase java API 调用详解

Hbase java API 调用 一. hbase的安装 参考:http://blog.csdn.net/mapengbo521521/article/details/41777721 二.hbase访问方式 Native java api:最常规最高效的访问方式. Hbase shell:hbase的命令行工具,最简单的接口,适合管理员使用 Thrift gateway:利用thrift序列化结束支持各种语言,适合异构系统在线访问 Rest gateway:支持rest风格的http api

Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbase调用MapReduce函数使用理解 第三部分:Hbase调用Java API使用理解 第四部分:Hbase Shell操作 第五部分:Hbase建表.读写操作方式性能优化总结 第一部分:Hbase框架原理理解 概述 HBase是一个构建在HDFS上的分布式列存储系统:HBase是基于Google

HBase学习小节v1.2

1. HBase 一个构建在HDFS上的高可靠.高性能.面向列.可伸缩.分布式列存储开源数据库,主要用于存储海量数据,同时使用mapreduce处理HBase中的数据,利用zookeeper作为协同服务.读写相对简单,不支持条件查询 2. HBase与HDFS对比 都具有良好的容错性和扩展性 HDFS适合批处理场景,但是不支持数据随即查找,不适合增量数据处理,不支持数据更新 3. HBase的特点: 海量数据:可支持上百万列,分成多个region 无模式:每行都有一个可排序的主键和任意多的列,列

Hbase学习(五)-hbase常识及habse适合什么场景

当我们对于数据结构字段不够确定或杂乱无章很难按一个概念去进行抽取的数据适合用使用什么数据库?答案是什么,如果我们使用的传统数据库,肯定留有多余的字段,10个不行,20个,但是这个严重影响了质量.并且如果面对大数据库,pt级别的数据,这种浪费更是严重的,那么我们该使用是什么数据库?hbase数个不错的选择,那么我们对于hbase还存在下列问题: 1.Column Family代表什么? 2.HBase通过row和column确定一份数据,这份数据的值可能有多个版本,为什么会存在多个版本? 3.查询

学习笔记--HBase

HBase 和 HDFS HDFS HBase HDFS是适于存储大容量文件的分布式文件系统. HBase是建立在HDFS之上的数据库. HDFS不支持快速单独记录查找. HBase提供在较大的表快速查找 它提供了高延迟批量处理;没有批处理概念. 它提供了数十亿条记录低延迟访问单个行记录(随机存取). 它提供的数据只能顺序访问. HBase内部使用哈希表和提供随机接入,并且其存储索引,可将在HDFS文件中的数据进行快速查找. HBase和RDBMS HBase RDBMS HBase无模式,它不

HBase学习解析之起源.md

概述 HBase是谷歌公司BigTable的开源实现.BigTable是一个分布式存储系统,利用谷歌提出的MapReduce分布式并行计算模型来处理海量数据,使用谷歌分布式文件系统GFS作为底层数据存储,并采用Chubby提供协同服务管理,可以扩展到PB级别的数据和上千台机器,具备广泛应用型.可扩展性.搞性能和高可用性等特点. BigTable具备以下特点: 支持大规模海量数据: 分布式并发数据处理效率极高: 易于扩展且支持动态伸缩: 适用于廉价设备: 适合于读操作不适合写操作. HBase简介