ideaAPI对hbase进行操作

package com.bawei;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.
;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class Count {

//创建连接
private static Connection connection=null;
static {
    //创建配置文件
    Configuration conf = HBaseConfiguration.create();
    //设置zookeeper参数和主机名称
    conf.set("hbase.zookeeper.quorum", "hdp444,hdp555,hdp666");
    //设置端口号
    conf.set("hbase.zookeeper.property.clientPort", "2181");
    try {
        connection = ConnectionFactory.createConnection(conf);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
//添加表
public static void start1(String tablename,String... familes) throws IOException {
    Admin admin = connection.getAdmin();
    HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename));
    try {
        for (String famile:familes){
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(famile);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);
    }finally {
        admin.close();
    }
}
//添加数据
public static void start2(String tablename,String rowKey,String family,String column,String value,String family1,String column1,String value1) throws IOException {
    Admin admin = connection.getAdmin();

    try {
        if (!admin.tableExists(TableName.valueOf(tablename))){
            System.out.println(tablename+"表不存在");
            return;
        }
        Table table = connection.getTable(TableName.valueOf(tablename));
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        table.put(put);
        Put put1 = new Put(Bytes.toBytes(rowKey));
        put1.addColumn(Bytes.toBytes(family1),Bytes.toBytes(column1),Bytes.toBytes(value1));
        table.put(put1);
    }finally {
        admin.close();
    }
}
//获取数据
public static void start3(String tablename,String startrow,String stoprow) throws IOException {
    //获取表
    Table table = connection.getTable(TableName.valueOf(tablename));
    Scan scan = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(stoprow));
    //获取列
    ResultScanner scanner = table.getScanner(scan);
    for (Result result:scanner){
        for (Cell cell:result.rawCells()){
            //获取rowkey
            byte[] rowbys = CellUtil.cloneRow(cell);
            String rowpom = Bytes.toString(rowbys);
            //获取val
            String commstr = Bytes.toString(CellUtil.cloneQualifier(cell));
            String values = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(rowpom+"----"+commstr+"---"+values);
        }
    }
    scanner.close();
    table.close();
}
//删除一行数据
public static void start4(String tablename,String rowkey) throws IOException {
    Admin admin = connection.getAdmin();
    if (!admin.tableExists(TableName.valueOf(tablename))){
        System.out.println(tablename+"表不存在");
        return;
    }
    Table table = connection.getTable(TableName.valueOf(tablename));
    try {
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        table.delete(delete);
    }finally {
        admin.close();
    }
}

//删除表
public static void start5(String tablename) throws IOException {
    //获取对象
    Admin admin = connection.getAdmin();
   try {
       if (!admin.tableExists(TableName.valueOf(tablename))){
           System.out.println(tablename+"表不存在");
           return;
       }
       admin.disableTable(TableName.valueOf(tablename));
       admin.deleteTable(TableName.valueOf(tablename));
   }finally {
       admin.close();
   }
}

public static void main(String[] args) throws IOException {
    //创建表

// start1(“company”,“info”,“data”);
//添加数据
// start2(“company”,“001”,“info”,“name”,“Baidu”,“data”,“city”,“xm”);
// start2(“company”,“002”,“info”,“name”,“huawei”,“data”,“city”,“xh”);
// start2(“company”,“003”,“info”,“name”,“xiaomi”,“data”,“city”,“xb”);
//查询数据
// start3(“company”,“001”,“004”);
//删除一行数据
// start4(“company”,“002”);
//删除表
start5(“company”);

}

}

在Hadoop生态系统中,Apache HBase是一个分布式列式存储系统,主要用于处理大规模数据集。Java APIHBase的主要客户端接口,它允许开发者使用Java编写程序来访问、管理和操作HBase。 使用Java API进行HBase操作主要包括以下几个步骤: 1. **添加依赖**:首先,在你的项目中引入HBase的jar包。如果你的项目是Maven项目,可以在pom.xml文件中添加HBase的dependency。 ```xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>版本号</version> </dependency> ``` 2. **连接HBase集群**:通过`HBaseConfiguration`配置工具获取连接HBase所需的配置信息,然后创建`HBaseAdmin`或`Table`对象。 ```java HBaseConfiguration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "zookeeper-hosts"); config.set("hbase.rootdir", "hdfs://namenode:port/path"); HBaseAdmin admin = new HBaseAdmin(config); // 或者创建表 HTable table = new HTable(config, "your-table-name"); ``` 3. **执行操作**:你可以使用`HBaseAdmin`进行表的管理(比如创建、删除),使用`HTable`进行读写操作,如行(Row)、列族(ColumnFamily)、列(Column)的操作。 ```java // 插入一条记录 Put put = new Put(Bytes.toBytes("row-key")); put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), Bytes.toBytes("value")); table.put(put); // 查询数据 Get get = new Get(Bytes.toBytes("row-key")); Result result = table.get(get); ``` 4. **关闭连接**:完成操作后记得关闭`HBaseAdmin`和`HTable`实例。 ```java admin.close(); table.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值