Hbase客户端操作api

来源:互联网 发布:中兴b600刷成网络盒子 编辑:程序博客网 时间:2024/04/29 22:09
1,创建一张表
import java.io.FileInputStream;

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;

public class HbaseJavaClientDemo {
   
    public static void main(String[] args) throws Exception {
        // Configuration conf = new Configuration();  // 只会加载hadoop的配置文件 : core-site.xml
        Configuration conf = HBaseConfiguration.create(); // 会自动加载classpath中的hbase-site.xml文件中的参数
        
        // conf.set("hbase.zookeeper.quorum", "hdp20-01:2181,hdp20-02:2181,hdp-03:2181");
        
        Connection conn = ConnectionFactory.createConnection(conf);
        
        Admin ddlTool = conn.getAdmin(); // 做DDL操作的工具对象
        
        //  先描述一个表
        
        // 先创建一个表名
        /*FileInputStream in = new FileInputStream("f:/a.jpg");
        byte[] b = new byte[10];
        in.read(b);*/
        TableName name = TableName.valueOf("t_user_info".getBytes());
        // 生成一个表描述器对象
        HTableDescriptor hTableDescriptor = new HTableDescriptor(name);
        
        // 创建一个列族描述器,定义好列族名
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("f1");
        hColumnDescriptor.setMaxVersions(3);
        
        // 往表描述器中添加列族描述
        hTableDescriptor.addFamily(hColumnDescriptor);
        
        
        ddlTool.createTable(hTableDescriptor);
 
        ddlTool.close();
        conn.close();
        
        
    }

}

二,表数据的增删改查

public class HbaseDemo {
    Connection conn = null;
    
    @Before
    public void init() throws Exception{
        Configuration conf = HBaseConfiguration.create(); // 会自动加载classpath中的hbase-site.xml文件中的参数
        conn = ConnectionFactory.createConnection(conf);
    }
    
    
    /**
     * 插入数据
     * @throws IOException
     */
    @Test
    public void testPut() throws IOException{
        
        Table table = conn.getTable(TableName.valueOf("t_article"));
        
        // 准备好要插入的数据,封装为Put对象
        Put put1 = new Put("zhangsan-20170809-001".getBytes());
        put1.addColumn("primary_content".getBytes(), "title".getBytes(), "震惊世界,印度阿三入侵伟大中华帝国达50天之久,中国居然毫无反应".getBytes());
        put1.addColumn("primary_content".getBytes(), "author".getBytes(), "zhangsan".getBytes());
        put1.addColumn("primary_content".getBytes(), "content".getBytes(), "阿三作死,迟早会把中国人民逼急的,直接一路打到新德里,活捉墓地,扒皮抽筋".getBytes());
        
        
        Put put2 = new Put("zhongsheng-20170809-002".getBytes());
        put2.addColumn("primary_content".getBytes(), "title".getBytes(), "忍无可忍,无需再忍".getBytes());
        put2.addColumn("primary_content".getBytes(), "author".getBytes(), "zhongsheng".getBytes());
        put2.addColumn("primary_content".getBytes(), "content".getBytes(), "话已说尽,仁至义尽,忍无可忍,无需再忍,勿谓言之不预也".getBytes());
    
        ArrayList<Put> puts = new ArrayList<>();
        puts.add(put1);
        puts.add(put2);
        
        table.put(puts);
        
        table.close();
        conn.close();
    }
    
    @Test
    public void testPut2() throws IOException{
        
        Table table = conn.getTable(TableName.valueOf("t_user_info"));
        
        // 准备好要插入的数据,封装为Put对象
        Put put1 = new Put("zhang-bj-male-001".getBytes());
        put1.addColumn("f1".getBytes(), "name".getBytes(), "张三".getBytes());
        put1.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(30));
        
        
        Put put2 = new Put("zhang-bj-female-002".getBytes());
        put2.addColumn("f1".getBytes(), "name".getBytes(), "张丽".getBytes());
        put2.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(20));
        
        
        Put put3 = new Put("liu-bj-female-003".getBytes());
        put3.addColumn("f1".getBytes(), "name".getBytes(), "刘芳".getBytes());
        put3.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(23));
        
        Put put4 = new Put("liu-bj-female-004".getBytes());
        put4.addColumn("f1".getBytes(), "name".getBytes(), "刘红".getBytes());
        put4.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(23));
        
        
        Put put5 = new Put("liu-sh-male-005".getBytes());
        put5.addColumn("f1".getBytes(), "name".getBytes(), "刘德华".getBytes());
        put5.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(43));
        
        
    
        ArrayList<Put> puts = new ArrayList<>();
        puts.add(put1);
        puts.add(put2);
        puts.add(put3);
        puts.add(put4);
        puts.add(put5);
        
        table.put(puts);
        
        table.close();
        conn.close();
    }
    
    
    
    
    /**
     * 删除数据
     * @throws Exception
     */
    @Test
    public void testDelete() throws Exception{
        Table table = conn.getTable(TableName.valueOf("t_article"));
        
        Delete delete = new Delete("zhangsan-20170809-001".getBytes());
        // 如果delete对象中不指定具体的key,则会删除这一整行
        // table.delete(delete);
        
        // delete对象中指定要删的key
        delete.addColumn("primary_content".getBytes(), "author".getBytes());
        table.delete(delete);
        
        table.close();
        conn.close();
    }
    
    
    /**
     * 修改数据 : 其实就是put,put会覆盖相同rowkey相同key的数据
     * @throws Exception
     */
    @Test
    public void testUpdate() throws Exception{
        
        Table table = conn.getTable(TableName.valueOf("t_article"));
        
        Put put = new Put("zhongsheng-20170809-002".getBytes());
        put.addColumn("primary_content".getBytes(), "author".getBytes(), "人民日报".getBytes());
        
        // 覆盖同行同key的数据value
        table.put(put);
        
        table.close();
        conn.close();
    }
    
    
    /**
     * 单行查询数据:
     * @throws Exception
     */
    @Test
    public void testGet() throws Exception{
        
        Table table = conn.getTable(TableName.valueOf("t_article"));
        // 构造查询参数
        Get get = new Get("zhongsheng-20170809-002".getBytes());
        // 指定要获取的key
        get.addColumn("primary_content".getBytes(), "author".getBytes());
        
        Result result = table.get(get);
        byte[] value = result.getValue("primary_content".getBytes(), "author".getBytes());
        
        String author = new String(value,"utf-8");
        
        System.out.println(author);
        
        table.close();
        conn.close();
    }
    
    
    /**
     * 范围查询
     * @throws IOException
     */
    @Test
    public void testScan() throws IOException{
        
        Table table = conn.getTable(TableName.valueOf("t_article"));
        
        // 含首不含尾
        Scan scan = new Scan("zhangsan-20170809-001".getBytes(), "zhongsheng-20170809-002\000".getBytes());
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> it = scanner.iterator();
        while(it.hasNext()){
            // 取到一行
            Result result = it.next();
            //result.getValue(family, qualifier)
            
            // 取该行的cell迭代器
            CellScanner cellScanner = result.cellScanner();
            // 迭代这一行的cell
            while(cellScanner.advance()){
                Cell cell = cellScanner.current();
                byte[] rowArray = cell.getRowArray();
                byte[] familyArray = cell.getFamilyArray();
                byte[] qualifierArray = cell.getQualifierArray();
                byte[] valueArray = cell.getValueArray();
                
                System.out.println("行键:" +new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
                System.out.println("列族名:" +new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
                System.out.println("key:" +new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
                System.out.println("value:" +new String(valueArray,cell.getValueOffset(),cell.getValueLength()));
            }
            
            
            
            /*cellScanner.advance();
            Cell cell = cellScanner.current();
            byte[] rowArray = cell.getRowArray();
            byte[] familyArray = cell.getFamilyArray();
            byte[] qualifierArray = cell.getQualifierArray();
            byte[] valueArray = cell.getValueArray();
            
            System.out.println("行键:" +new String(rowArray));
            System.out.println("列族名:" +new String(familyArray));
            System.out.println("key:" +new String(qualifierArray));
            System.out.println("value:" +new String(valueArray));*/
            
            System.out.println("**************行分割线***********************");
            
        }
        
        scanner.close();
        table.close();
        conn.close();
        
    }
    
    
    /**
     * 过滤查询示范
     * @throws Exception
     */
    @Test
    public void testFilterScan() throws Exception{
        
        Table table = conn.getTable(TableName.valueOf("t_user_info"));
        
        PrefixFilter filter =new PrefixFilter("liu".getBytes()); // 构造了一个条件过滤器
        
        
        Scan scan = new Scan(Bytes.toBytes("liu-bj-female-003"), filter);
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> it = scanner.iterator();
        while(it.hasNext()){
            // 取到一行
            Result result = it.next();
            //result.getValue(family, qualifier)
            
            // 取该行的cell迭代器
            CellScanner cellScanner = result.cellScanner();
            // 迭代这一行的cell
            while(cellScanner.advance()){
                Cell cell = cellScanner.current();
                byte[] rowArray = cell.getRowArray();
                byte[] familyArray = cell.getFamilyArray();
                byte[] qualifierArray = cell.getQualifierArray();
                byte[] valueArray = cell.getValueArray();
                
                System.out.println("行键:" +new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
                System.out.println("列族名:" +new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
                System.out.println("key:" +new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
                System.out.println("value:" +new String(valueArray,cell.getValueOffset(),cell.getValueLength()));
            }
            System.out.println("**************行分割线***********************");
            
        }
        
        scanner.close();
        table.close();
        conn.close();
        
    }
    
    
}




原创粉丝点击