注:

window环境连接linux可能不能识别,为方便程序可以直接在ideal中运行,打开window/system32/drivers/hosts并修改host属性取消可读模式,
在这里插入图片描述
在hosts文档末尾处添加需要连接虚拟机ip地址以及用户名,注意空格也是英文状态下的空格
在这里插入图片描述

pom依赖

😇习惯使用1.2.0版本

    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-common</artifactId>
      <version>1.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>1.2.0</version>
    </dependency>

连接核心代码代码及建表操作

public class Myhbase {
    public static void main(String[] args) throws Exception{
    //连接hbase核心代码如下
        //取得一个数据库连接的配置参数对象
        Configuration conf = HBaseConfiguration.create();
        //设置连接参数
        conf.set("hbase.zookeeper.quorum","192.168.56.21");
        //zookepper地址
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //hbaseMaster 默认端口为60000
        conf.set("hbase.master","192.168.56.21:60000");
        Connection conn = ConnectionFactory.createConnection(conf);



//建表操作
        //获得一个hbaseAdmin 提供的方法包括:创建表、删除表、列出表....
        Admin admin = conn.getAdmin();
        //制造一个表名
        TableName mytestTab = TableName.valueOf("mytestTab");
        //查询表是否存在,若不存在进行建表操作
        if (!admin.tableExists(mytestTab)){
            //建立一个表结构
            HTableDescriptor table = new HTableDescriptor(mytestTab);
            //建立表下列族,并存放到表结构中
            HColumnDescriptor base = new HColumnDescriptor("base");
            table.addFamily(base);
            HColumnDescriptor myinfo = new HColumnDescriptor("myinfo");
            table.addFamily(myinfo);
            //完成建表
            admin.createTable(table);
        }
    }
}

运行成功后,hbase界面查看mytestTab表已经创建好了✌🏻
在这里插入图片描述

新增数据put

public class Myhbase {
    public static void main(String[] args) throws Exception{
        //取得一个数据库连接的配置参数对象
        Configuration conf = HBaseConfiguration.create();
        //设置连接参数
        conf.set("hbase.zookeeper.quorum","192.168.56.21");
        //zookepper地址
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //hbaseMaster 默认端口为60000
        conf.set("hbase.master","192.168.56.21:60000");
        Connection conn = ConnectionFactory.createConnection(conf);

        //制造一个表名
        TableName mytestTab = TableName.valueOf("mytestTab");
        
        //连接取得该表
        Table table = conn.getTable(mytestTab);
        //添加行键
        Put put = new Put("1".getBytes());
        //添加顺序分别为列族名,列名和列值 并注意getbytes格式转换
        put.addColumn("base".getBytes(),"username".getBytes(),"jzy".getBytes());
        put.addColumn("base".getBytes(),"age".getBytes(),"23".getBytes());
        put.addColumn("myinfo".getBytes(),"hobby".getBytes(),"basketball,sing".getBytes());
        //将数据存入表中
        table.put(put);
    }
}

返回hbase下,查看新增数据再次添加成功😸
在这里插入图片描述

全表扫描

public class Myhbase {
    public static void main(String[] args) throws Exception{
        //取得一个数据库连接的配置参数对象
        Configuration conf = HBaseConfiguration.create();
        //设置连接参数
        conf.set("hbase.zookeeper.quorum","192.168.56.21");
        //zookepper地址
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //hbaseMaster 默认端口为60000
        conf.set("hbase.master","192.168.56.21:60000");
        Connection conn = ConnectionFactory.createConnection(conf);


        //制造一个表名
        TableName mytestTab = TableName.valueOf("mytestTab");


   		//连接取得该表
        Table table = conn.getTable(mytestTab);

        //scan扫描数据
        //创建扫描scan对象
        Scan scan = new Scan();
        ResultScanner resultScanner = table.getScanner(scan);
        //打印查找的结果
        for (Result result : resultScanner) {
            //1.获取所有信息
            //System.out.println("scan :"+result);
            //2.获取指定信息
            System.out.println("scan: "+new String(result.getValue(Bytes.toBytes("base"),Bytes.toBytes("username"))));
        }
    }
}

打印所取得的值如下:
在这里插入图片描述

过滤扫描filter

public class Myhbase {
    public static void main(String[] args) throws Exception{
        //取得一个数据库连接的配置参数对象
        Configuration conf = HBaseConfiguration.create();
        //设置连接参数
        conf.set("hbase.zookeeper.quorum","192.168.56.21");
        //zookepper地址
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //hbaseMaster 默认端口为60000
        conf.set("hbase.master","192.168.56.21:60000");
        Connection conn = ConnectionFactory.createConnection(conf);


        //制造一个表名
        TableName mytestTab = TableName.valueOf("mytestTab");
   		//连接取得该表
        Table table = conn.getTable(mytestTab);
        //创建扫描scan对象
   		Scan scan = new Scan();
   		//添加过滤器 取出列值中包含sing的内容 相当于语句如下:
   		// scan 'mytestTab',FILTER=>"ValueFilter(=,'substring:sing')"
        ValueFilter vf = new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("sing"));
        scan.setFilter(vf);
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            System.out.println("scanFilter: "+result.toString());
        }
    }
}

总结版如下

public class HbaseClient {
    @Test
    //创建表
    public void createTable() throws IOException {
        //创建配置
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","jzy1");
        conf.set("hbase.zookeeper.property.clientPort","2181");

        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);
        //创建admin
        Admin admin = conn.getAdmin();
        //创建表的描述信息
        HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student"));
        //添加列簇
        student.addFamily(new HColumnDescriptor("Info"));
        student.addFamily(new HColumnDescriptor("score"));
        //调用API进行建表操作
        admin.createTable(student);
    }
    @Test
    //判断表是否存在
    public void isTableExists() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","jzy1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);
        //创建admin
        Admin admin = conn.getAdmin();
        //调用API进行判断表是否存在
        System.out.println(admin.tableExists(TableName.valueOf("student")));
    }

    @Test
    //向表中插入数据
    public void putData2Table() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","jzy1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);
        //创建table类
        Table student = conn.getTable(TableName.valueOf("student"));
        //创建put类
        Put put = new Put(Bytes.toBytes("1001"));
        //想put中添加列簇,列名,值  注意:需要转化成字节数组
        put.addColumn(Bytes.toBytes("Info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
        //调用API操作进行插入操作
        student.put(put);
    }

    //查看一条数据
    @Test
    public void getDataFromTable() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","jzy1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);
        //创建table类
        Table student = conn.getTable(TableName.valueOf("student"));
        //创建get类
        Get get = new Get(Bytes.toBytes("1001"));
        //调用API进行获取数据
        Result result = student.get(get);
        //将返回的结果进行遍历输出
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("列簇:"+Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列名:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

    //删除表操作
    @Test
    public void dropTable() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","jzy1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);
        //创建admin
        Admin admin = conn.getAdmin();
        //调用API禁用表
        admin.disableTable(TableName.valueOf("student"));
        //调用API删除表
        admin.deleteTable(TableName.valueOf("student"));
    }
}

革命尚未成功,同志仍需努力😼,加油fighting!!🐱‍🏍

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐