美文网首页
28hbase java api(用eclipse进行编程)

28hbase java api(用eclipse进行编程)

作者: 文茶君 | 来源:发表于2019-12-30 12:43 被阅读0次

现在我们使用在Windows上的(我的是win10)的java的ide来连接数据库。在这里使用eclipse来进行编译。首先考虑的是怎么连接。在这里并不需要像前面的拷贝配置文件。只需要配置config类。
这里的conf是hadoop的conf包,所以需要导入hadoop的包

 //表的管理类
    HBaseAdmin admin = null;
    //数据的管理类
    HTable table = null;

public void init() throws Exception{
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "node1,node2,node3");//客户端需要连接zk,所以配置
        admin = new HBaseAdmin(conf);//新版本就用admin,查看api要注意版本
        table = new HTable(conf,tm.getBytes());
    }

创建表

public void createTable() throws Exception{
        //表的描述类
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tm));
        //列族的描述类
        HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
        desc.addFamily(family);
        if(admin.tableExists(tm)){
            admin.disableTable(tm);//要先disable
            admin.deleteTable(tm);
        }
        admin.createTable(desc);
    }

插入数据

//HTable table = null;
//table = new HTable(conf,tm.getBytes());
 @Test
    public void insert() throws Exception{
        Put put = new Put("1111".getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
        put.add("cf".getBytes(), "age".getBytes(), "12".getBytes());
        put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes());
        table.put(put);
    }

取数据

public void get() throws Exception{
        Get get = new Get("1111".getBytes());
        //添加要获取的列和列族,减少网络的io,相当于在服务器端做了过滤
        get.addColumn("cf".getBytes(), "name".getBytes());
        get.addColumn("cf".getBytes(), "age".getBytes());
        get.addColumn("cf".getBytes(), "sex".getBytes());
        Result result = table.get(get);
        Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
        Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
        Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
        System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
        System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
        System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
    }

另一种取数据的方式

  @Test
    public void scan() throws Exception{
        Scan scan = new Scan();//进行全盘扫描
//      scan.setStartRow(startRow);
//      scan.setStopRow(stopRow);一般采用这两行注释所写的方法来限制范围进行查找
        ResultScanner rss = table.getScanner(scan);
        for (Result result : rss) {
            Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
            Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
            Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
        }
    }

全部代码

public class HBaseDemo {

    //表的管理类
    HBaseAdmin admin = null;
    //数据的管理类
    HTable table = null;
    //表名
    String tm = "phone";
    
    /**
     * 完成初始化功能
     * @throws Exception
     */
    @Before
    public void init() throws Exception{
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
        admin = new HBaseAdmin(conf);
        table = new HTable(conf,tm.getBytes());
    }
    
    /**
     * 创建表
     * @throws Exception
     */
    @Test
    public void createTable() throws Exception{
        //表的描述类
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tm));
        //列族的描述类
        HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
        desc.addFamily(family);
        if(admin.tableExists(tm)){
            admin.disableTable(tm);
            admin.deleteTable(tm);
        }
        admin.createTable(desc);
    }
    
    @Test
    public void insert() throws Exception{
        Put put = new Put("1111".getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
        put.add("cf".getBytes(), "age".getBytes(), "12".getBytes());
        put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes());
        table.put(put);
    }
    @Test
    public void get() throws Exception{
        Get get = new Get("1111".getBytes());
        //添加要获取的列和列族,减少网络的io,相当于在服务器端做了过滤
        get.addColumn("cf".getBytes(), "name".getBytes());
        get.addColumn("cf".getBytes(), "age".getBytes());
        get.addColumn("cf".getBytes(), "sex".getBytes());
        Result result = table.get(get);
        Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
        Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
        Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
        System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
        System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
        System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
    }
    
    @Test
    public void scan() throws Exception{
        Scan scan = new Scan();
//      scan.setStartRow(startRow);
//      scan.setStopRow(stopRow);
        ResultScanner rss = table.getScanner(scan);
        for (Result result : rss) {
            Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
            Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
            Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
        }
    }
    
    @After
    public void destory() throws Exception{
        if(admin!=null){
            admin.close();
        }
    }
}

导入的包

import java.io.IOException;

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.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

相关文章

网友评论

      本文标题:28hbase java api(用eclipse进行编程)

      本文链接:https://www.haomeiwen.com/subject/ufdloctx.html