我的环境:完全分布式集群,不是hbase自带的zookeeper,是独立的zookeeper

该代码操作过程:在hbase中创建表、插入数据、查询数据

注意:如果缺少相应的包,请上网下载后,导入项目即可

代码如下:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;


public class ExampleForHbase {
    public static Configuration configuration; // 管理Hbase的配置信息
    public static Connection connection; // 管理Hbase连接
    public static Admin admin; // 管理Hbase数据库的信息
    public static void main(String[] args) throws IOException {
        init(); //连接
        String colF[] ={"score"};
        createTable("student",colF); // 建表
        insertData("student","zhangsan","score","English","69");
        insertData("student","zhangsan","score","Math","86");
        insertData("student","zhangsan","score","Computer","77");
        getData("student","zhangsan","score","Computer");
    }

    // 操作数据库之前,建立连接
    public static void init(){
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://master:9000/hbase");
        configuration.set("hbase.zookeeper.quorum","master,slave1,slave2"); // 设置zookeeper节点
        configuration.set("hbase.zookeeper.property.clientPort","2181"); // 设置zookeeper节点
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();

        }catch (IOException e){
            e.printStackTrace();
        }
    }

    // 创建表
    /*
    * @param myTableName 表名
    * @param colFamily 列族数组
    * @throws Exception
    * */
    public static void createTable(String myTableName,String[] colFamily) throws IOException{
        TableName tableName = TableName.valueOf(myTableName);
        if(admin.tableExists(tableName)){
            System.out.println("Table exists");
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for(String str:colFamily){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
        }
    }


    // 添加单元格数据
    /*
    * @param tableName 表名
    * @param rowKey 行键
    * @param colFamily 列族
    * @param col 列限定符
    * @param val 数据
    * @thorws Exception
    * */
    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());
        table.put(put);
        table.close();
    }

    //浏览数据
    /*
    * @param tableName 表名
    * @param rowKey 行
    * @param colFamily 列族
    * @param col 列限定符
    * @throw IOException
    * */
    public static void getData(String tableName,String rowKey,String colFamily,String col) throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result =table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        table.close();
    }



    // 操作数据库之后,关闭连接
    public static void close(){
        try{
            if(admin!=null){
                admin.close(); // 退出用户
            }
            if(null != connection){
                connection.close(); // 关闭连接
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}

补充;使用maven时,pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hbaseAction</groupId>
    <artifactId>hbaseAction</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-http</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>LATEST</version>
        </dependency>
    </dependencies>

</project>

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐