Hbase客户端操作api

阅读: 评论:0

Hbase客户端操作api

Hbase客户端操作api

1,创建一张表
import java.io.FileInputStream;

import org.f.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的配置文件 : l
        Configuration conf = ate(); // 会自动加载classpath中的l文件中的参数
        
        // conf.set(&#keeper.quorum", "hdp20-01:2181,hdp20-02:2181,hdp-03:2181");
        
        Connection conn = ateConnection(conf);
        
        Admin ddlTool = Admin(); // 做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);
        
        
        ateTable(hTableDescriptor);
 
        ddlTool.close();
        conn.close();
        
        
    }

}

二,表数据的增删改查

public class HbaseDemo {
    Connection conn = null;
    
    @Before
    public void init() throws Exception{
        Configuration conf = ate(); // 会自动加载classpath中的l文件中的参数
        conn = ateConnection(conf);
    }
    
    
    /**
     * 插入数据
     * @throws IOException
     */
    @Test
    public void testPut() throws IOException{
        
        Table table = Table(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 = Table(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(30));
        
        
        Put put2 = new Put("zhang-bj-female-002".getBytes());
        put2.addColumn("f1".getBytes(), "name".getBytes(), "张丽".getBytes());
        put2.addColumn("f1".getBytes(), "age".getBytes(), Bytes(20));
        
        
        Put put3 = new Put("liu-bj-female-003".getBytes());
        put3.addColumn("f1".getBytes(), "name".getBytes(), "刘芳".getBytes());
        put3.addColumn("f1".getBytes(), "age".getBytes(), Bytes(23));
        
        Put put4 = new Put("liu-bj-female-004".getBytes());
        put4.addColumn("f1".getBytes(), "name".getBytes(), "刘红".getBytes());
        put4.addColumn("f1".getBytes(), "age".getBytes(), Bytes(23));
        
        
        Put put5 = new Put("liu-sh-male-005".getBytes());
        put5.addColumn("f1".getBytes(), "name".getBytes(), "刘德华".getBytes());
        put5.addColumn("f1".getBytes(), "age".getBytes(), Bytes(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 = Table(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 = Table(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 = Table(TableName.valueOf("t_article"));
        // 构造查询参数
        Get get = new Get("zhongsheng-20170809-002".getBytes());
        // 指定要获取的key
        get.addColumn("primary_content".getBytes(), "author".getBytes());
        
        Result result = (get);
        byte[] value = Value("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 = Table(TableName.valueOf("t_article"));
        
        // 含首不含尾
        Scan scan = new Scan("zhangsan-20170809-001".getBytes(), "zhongsheng-20170809-00200".getBytes());
        ResultScanner scanner = Scanner(scan);
        Iterator<Result> it = scanner.iterator();
        while(it.hasNext()){
            // 取到一行
            Result result = it.next();
            //Value(family, qualifier)
            
            // 取该行的cell迭代器
            CellScanner cellScanner = llScanner();
            // 迭代这一行的cell
            while(cellScanner.advance()){
                Cell cell = cellScanner.current();
                byte[] rowArray = RowArray();
                byte[] familyArray = FamilyArray();
                byte[] qualifierArray = QualifierArray();
                byte[] valueArray = ValueArray();
                
                System.out.println("行键:" +new String(RowOffset(),RowLength()));
                System.out.println("列族名:" +new String(FamilyOffset(),FamilyLength()));
                System.out.println("key:" +new String(QualifierOffset(),QualifierLength()));
                System.out.println("value:" +new String(ValueOffset(),ValueLength()));
            }
            
            
            
            /*cellScanner.advance();
            Cell cell = cellScanner.current();
            byte[] rowArray = RowArray();
            byte[] familyArray = FamilyArray();
            byte[] qualifierArray = QualifierArray();
            byte[] valueArray = ValueArray();
            
            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 = Table(TableName.valueOf("t_user_info"));
        
        PrefixFilter filter =new PrefixFilter("liu".getBytes()); // 构造了一个条件过滤器
        
        
        Scan scan = new Bytes("liu-bj-female-003"), filter);
        ResultScanner scanner = Scanner(scan);
        Iterator<Result> it = scanner.iterator();
        while(it.hasNext()){
            // 取到一行
            Result result = it.next();
            //Value(family, qualifier)
            
            // 取该行的cell迭代器
            CellScanner cellScanner = llScanner();
            // 迭代这一行的cell
            while(cellScanner.advance()){
                Cell cell = cellScanner.current();
                byte[] rowArray = RowArray();
                byte[] familyArray = FamilyArray();
                byte[] qualifierArray = QualifierArray();
                byte[] valueArray = ValueArray();
                
                System.out.println("行键:" +new String(RowOffset(),RowLength()));
                System.out.println("列族名:" +new String(FamilyOffset(),FamilyLength()));
                System.out.println("key:" +new String(QualifierOffset(),QualifierLength()));
                System.out.println("value:" +new String(ValueOffset(),ValueLength()));
            }
            System.out.println("**************行分割线***********************");
            
        }
        
        scanner.close();
        table.close();
        conn.close();
        
    }
    
    
}




本文发布于:2024-02-05 06:38:58,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170726463163946.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:客户端   操作   Hbase   api
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23