hbase java api 两种方式

NoSQL Hbase JAVA API

实例一

导入架包:

<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>1.3.5</version>
</dependency>

首先在java中新建一个HbaseService类

步骤一:添加配置

public static Connection getConnection() throws IOException {
    Configuration configuration = HBaseConfiguration.create();
    Connection connection= ConnectionFactory.createConnection(configuration);
    return connection;
}

步骤二:快速执行方法

public static void main(String[] args) throws IOException{
    //创建一张表
    createTable();
    //向表中插入数据
    insertTable();
    //查询表中对应的数据
    searchTable();
    //查询所有
    searchAll();
}

步骤三:查询所有

private static void searchAll() throws IOException{
    //scan 'emps',FILTER=>ValueFilter(=,'substring:c')
    Table emps = getConnection().getTable(TableName.valueOf("emps"));
    Scan scan=new Scan();
    FilterList lst = new FilterList();
    ValueFilter vf = new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("c"));
    lst.addFilter(vf);
    scan.setFilter(lst);
    ResultScanner scanner = emps.getScanner(new Scan());//ResultSet=>Result
    Iterator<Result> rs = scanner.iterator();
    while (rs.hasNext()){
        Result row = rs.next();
        //可以取出多个数值
        System.out.println(row.getValue("base".getBytes(),"name".getBytes()));

    }
}

步骤四:查询表中对应的数据

private static void searchTable() throws IOException{
    //get 'emps','001','base:name'
    Table emps = getConnection().getTable(TableName.valueOf("emps"));
    Get get = new Get("001".getBytes());
    Result result = emps.get(get);
    result.getValue("base".getBytes(),"name".getBytes());
    String str =Bytes.toString(result.getValue("base".getBytes(),"name".getBytes()));
    System.out.println(str);
}

步骤五:向表中插入数据

private static void insertTable() throws IOException{
    //put 'emps','001','base:name','value'
    Table emps=getConnection().getTable(TableName.valueOf("emps"));
    //添加001
    Put put = new Put("001".getBytes());
    put.addColumn("base".getBytes(),"name".getBytes(),"cm".getBytes());
    emps.put(put);
}

步骤六:创建一张表格

private static void createTable() throws IOException{
    Admin admin=getConnection().getAdmin();
    //给个表名
    HTableDescriptor table=new HTableDescriptor(TableName.valueOf("emps"));
   //创建一个列族
    HColumnDescriptor base=new HColumnDescriptor("base");
    //表上加列族
    table.addFamily(base);
    admin.createTable(table);
}

步骤七:我们打包前可以修改包名:

进入pom.xml:在<build>下一行添加<filalName>包名<filalName>

步骤八:进入Linux执行下面命令:

[root@head42 ~]# java -cp hb.jar:/opt/hbase-1.3.5/lib/* com.njbdqn.services.HbaseService

实例二

步骤一:导入架包:

<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>1.3.5</version>
</dependency>

步骤二:编写java代码

public class hbase {
    public static void main(String[] args) throws IOException {
        createTable();
        listTables();
        //找配置文件
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","localhost");
        //设置端口号
        conf.set("hbase.zookeeper.property.clientPort","2181");
        Connection connection= ConnectionFactory.createConnection(conf);
        Admin admin=connection.getAdmin();
        //拿到表的名字类型数组,进行for循环
        for (TableName name:admin.listTableNames()) {
            System.out.println(name.toString());
        }
        connection.close();
    }

步骤三:以下是开始打包命令:

 

步骤四:运行并形成一个包:

 

步骤五:进入Linux执行:[root@head42 ~]# java -cp hbase-java.jar cn.kgc.hbase

(hbase-java.jar:为导入的架包 cn.kgc.hbase:类名地址)

执行成功如下:

这样就可以了哈!

 

posted on 2019-07-23 19:51  来勒  阅读(266)  评论(0编辑  收藏  举报

导航