使用Java操作Hbase

19 篇文章 1 订阅
4 篇文章 0 订阅

目录

修改hosts文件

导入jar包

配置hbase信息,连接hbase数据库

创建表

删除表

获取namespace

获取tables

添加数据

查询表中的数据

查询表中所有数据

关闭流


修改hosts文件

位置:C:\Windows\System32\drivers\etc\hosts

win+R:ping一下IP地址和虚拟机名看一下能不能ping通 

 

导入jar包

创建maven项目,在pom.xml 里面添加jar包

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

配置hbase信息,连接hbase数据库

    //定义一个config,用于获取配置对象
    static Configuration config = null;
    //获取连接
    private Connection conn = null;
    Admin admin = null;

    public void init() throws IOException {
        //配置hbase信息,连接hbase数据库
        config = HBaseConfiguration.create();
        config.set(HConstants.HBASE_DIR, "hdfs://192.168.152.192:9000/hbase");
        config.set(HConstants.ZOOKEEPER_QUORUM, "192.168.152.192");
        config.set(HConstants.CLIENT_PORT_STR, "2181");
        //hbase连接工厂
        conn = ConnectionFactory.createConnection(config);
        //拿到admin
        admin = conn.getAdmin();
    }

创建命名空间

    public void createNameSpace() {
        NamespaceDescriptor test = NamespaceDescriptor.create("test").build();

        try {
            //执行创建对象
            admin.createNamespace(test);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

创建表

    public void createTable() throws IOException {
        //创建表的描述类
        TableName tableName = TableName.valueOf("test:student");
        //获取表格描述器
        HTableDescriptor desc = new HTableDescriptor(tableName);

        //创建列族的描述类,添加列族
        HColumnDescriptor family1 = new HColumnDescriptor("info1");
        HColumnDescriptor family2 = new HColumnDescriptor("info2");
        desc.addFamily(family1);
        desc.addFamily(family2);

        admin.createTable(desc);
    }

删除表

    public void deleteTable() throws IOException { 
        //禁用
        admin.disableTable(TableName.valueOf("test:student"));
        //删除
        admin.deleteTable(TableName.valueOf("test:student"));
}

获取namespace

    public void getAllNamespace() throws IOException {
        String[] namespaces = admin.listNamespaces();
         String s = Arrays.toString(namespaces);
         System.out.println(s);
    }

 

获取tables

    public void getAllNamespace() throws IOException {
        List<TableDescriptor> tableDescriptors =
 admin.listTableDescriptorsByNamespace("test".getBytes());
        System.out.println(tableDescriptors);
    }

 

添加数据

 public void insertData() throws IOException {
        //获取表的信息
        Table table = conn.getTable(TableName.valueOf("test:student"));
        //设置行键
        Put put = new Put(Bytes.toBytes("student1"));
        //设置列的标识以及列值
        put.addColumn("info1".getBytes(), "name".getBytes(), "zs".getBytes());
        put.addColumn("info2".getBytes(), "school".getBytes(), "xwxx".getBytes());
        //执行添加
        table.put(put);

        //使用集合添加数据
        Put put2 = new Put(Bytes.toBytes("student2"));
        put2.addColumn("info1".getBytes(), "name".getBytes(), "zss".getBytes());
        put2.addColumn("info2".getBytes(), "school".getBytes(), "xwxx".getBytes());
        Put put3 = new Put(Bytes.toBytes("student3"));
        put3.addColumn("info1".getBytes(), "name".getBytes(), "zsr".getBytes());
        put3.addColumn("info2".getBytes(), "school".getBytes(), "xwxx".getBytes());
        List<Put> list = new ArrayList<>();
        list.add(put2);
        list.add(put3);
        table.put(list);
    }

查询表中的数据

public void queryData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("test:student"));
        Get get = new Get(Bytes.toBytes("student1"));
        Result result = table.get(get);
        byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
        System.out.println("姓名:" + Bytes.toString(value));
        value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
        System.out.println("学校:" + Bytes.toString(value));

    }

 

查询表中所有数据

关闭流

    public void close() throws IOException {
        if (admin != null) {
            admin.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: HBase是一个分布式的、面向列的NoSQL数据库,它是建立在Hadoop之上的。使用Java操作HBase可以通过HBase提供的Java API来实现。Java API提供了一系列的类和接口,可以用来连接HBase集群、创建表、插入数据、查询数据等操作。具体的操作步骤如下: 1. 引入HBaseJava API依赖包,可以通过Maven或手动下载方式获取。 2. 创建HBase的配置对象,设置HBase集群的Zookeeper地址和端口号。 3. 创建HBase的连接对象,通过配置对象和连接对象来连接HBase集群。 4. 创建HBase表,通过HBaseAdmin类的createTable方法来创建表。 5. 插入数据,通过Put类来封装数据,然后通过Table类的put方法来插入数据。 6. 查询数据,通过Get类来封装查询条件,然后通过Table类的get方法来查询数据。 7. 删除数据,通过Delete类来封装删除条件,然后通过Table类的delete方法来删除数据。 8. 关闭连接,通过Connection类的close方法来关闭连接。 以上就是使用Java操作HBase的基本步骤,需要注意的是,在使用HBase时需要考虑数据的一致性和可靠性,同时需要合理设计表结构和数据存储方式。 ### 回答2: HBase是一种分布式的非关系型数据库,它被广泛应用于大规模数据存储和数据分析领域。HBase的特点在于高可靠性、高可扩展性、分布式计算能力强等优点,可以很好地处理海量数据。 HBase的开发语言支持Java、Python等多种语言,使用Java操作HBase时,需要使用HBase提供的Java API,通过Java编写代码来实现对HBase操作操作HBase可以分为连接HBase、创建表、插入数据、查询数据、删除数据、关闭连接等步骤。 1. 连接HBase 通过使用HBase提供的Configuration类,可以设置连接HBase所需的配置信息,包括zk连接地址、端口号等。创建完Configuration对象后,通过ConnectionFactory来获取连接HBase的Connection对象。 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.100"); conf.set("hbase.zookeeper.property.clientPort", "2181"); Connection conn = ConnectionFactory.createConnection(conf); 2. 创建表 通过Connection对象,可以使用Admin来操作HBase,创建表需要先创建TableDescriptor和ColumnFamilyDescriptor对象,然后通过Admin.createTable()方法来创建表。 Admin admin = conn.getAdmin(); TableName tableName = TableName.valueOf("test_table"); TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("test_family")).build(); TableDescriptor tableDescriptor = tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor).build(); admin.createTable(tableDescriptor); admin.close(); 3. 插入数据 插入数据需要先创建Put对象,并将需要插入的数据通过AddColumn()方法添加到Put对象中,然后通过Table.put()方法将数据插入到表中。 Table table = conn.getTable(tableName); Put put = new Put(Bytes.toBytes("row_key")); put.addColumn(Bytes.toBytes("test_family"), Bytes.toBytes("test_qualifier"), Bytes.toBytes("test_value")); table.put(put); table.close(); 4. 查询数据 查询数据需要先创建Get对象,然后通过Table.get()方法来获取数据。可以通过addColumn()方法指定需要获取的列。 Get get = new Get(Bytes.toBytes("row_key")); get.addColumn(Bytes.toBytes("test_family"), Bytes.toBytes("test_qualifier")); Result result = table.get(get); byte[] resultValue = result.getValue(Bytes.toBytes("test_family"), Bytes.toBytes("test_qualifier")); 5. 删除数据 删除数据需要先创建Delete对象,指定需要删除的行和列,然后通过Table.delete()方法来执行删除操作。 Delete delete = new Delete(Bytes.toBytes("row_key")); delete.addColumn(Bytes.toBytes("test_family"), Bytes.toBytes("test_qualifier")); table.delete(delete); 6. 关闭连接 操作HBase后需要关闭连接以释放资源。 table.close(); admin.close(); conn.close(); 综上所述,使用Java操作HBase需要掌握HBaseJava API以及相关的操作步骤。通过以上代码示例,可以更好地理解JavaHBase中的应用。 ### 回答3: HBase是Apache Hadoop生态系统中的一种面向列的数据库系统,它能够提供低延迟的实时读写能力以及可扩展性和容错性。Java作为一种流行的编程语言,可以被用来操作HBase数据库系统。下面是使用Java操作HBase的一些常见操作和案例。 1. 连接HBase:在Java中,我们可以使用HBaseConfiguration类来创建连接HBase的配置信息。使用HBaseAdmin类可以验证HBase数据库是否可用,如下所示: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); HBaseAdmin admin = new HBaseAdmin(conf); boolean availability = admin.isMasterRunning(); ``` 2. 创建表格:可以使用HTableDescriptor和HColumnDescriptor类来创建HBase表格: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor tableDesc = new HTableDescriptor("mytable"); HColumnDescriptor familyDesc = new HColumnDescriptor("myfamily"); tableDesc.addFamily(familyDesc); admin.createTable(tableDesc); ``` 3. 插入数据:HBase的数据是基于行和列族的,可以使用Put类来将数据插入HBase的表中。可以使用HBase shell中的put命令来插入数据,而Java代码如下: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); HTable table = new HTable(conf, "mytable"); Put put = new Put(Bytes.toBytes("row1")); put.add(Bytes.toBytes("myfamily"), Bytes.toBytes("mycolumn"), Bytes.toBytes("myvalue")); table.put(put); ``` 4. 查询数据:HBase提供了多种查询方式,如Get、Scan和Filter等。使用Get查询可以根据行键来查询指定行的数据,如下所示: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); HTable table = new HTable(conf, "mytable"); Get get = new Get(Bytes.toBytes("row1")); Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes("myfamily"), Bytes.toBytes("mycolumn")); ``` 5. 删除数据:可以使用Delete类来删除指定的行或列族,如下所示: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); HTable table = new HTable(conf, "mytable"); Delete delete = new Delete(Bytes.toBytes("row1")); delete.deleteColumn(Bytes.toBytes("myfamily"), Bytes.toBytes("mycolumn")); table.delete(delete); ``` 综上所述,以上是使用Java操作HBase的一些基本操作和案例,HBaseJava API提供了许多高级功能,包括复杂的过滤查询、多版本控制和扫描器等,使其成为处理大量数据的优秀工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值