IDEA远程连接HBase及其Java API实战

2021-12-31 09:08:44 浏览数 (1)

文章目录

  • 开放端口
  • 域名配置
  • Hbase安装
  • IDEA源码

开放端口

安全组没开放端口是原罪!!!导致好多BUG费时费力。Hbase悄悄咪咪的用了好多端口,比如被我抓到的42239,直接搜索报错药不对症。

域名配置

  1. 服务器 vi /etc/hosts 私网ip master
  1. 本地 C:WindowsSystem32driversetchosts 公网ip master

Hbase安装

  1. 下载压缩包 可以去官网下载http://hbase.apache.org/downloads.html 也可以去镜像下载历史版本http://archive.apache.org/dist/hbase/ 以1.3.2版本为例:

直接下载或者下载到本地再上传都行,看你哪个快。

代码语言:javascript复制
wget http://archive.apache.org/dist/hbase/1.3.2/hbase-1.3.2-bin.tar.gz
tar -zxvf hbase-1.3.2-bin.tar.gz #解压
mv hbase-1.3.2 /usr/local/hbase
  1. 配置hbase-site.xml
代码语言:javascript复制
cd /usr/local/hbase/conf
vi hbase-site.xml
代码语言:javascript复制
<property>
        <name>hbase.cluster.distributed</name>
        <value>true</value>
    </property>
    <property>
        <name>hbase.rootdir</name>
        <value>/hbase-data</value>
    </property>
    <property>
        <name>hbase.zookeeper.quorum</name>
        <value>master:2181</value>
    </property>
  1. 配置hbase-env.sh
代码语言:javascript复制
cd /usr/local/hbase/conf
echo $JAVA_HOME #若没有安装jdk可百度(偷懒)
vi hbase-env.sh #添加和你输出的JAVA_HOME一致
export JAVA_HOME=/usr/local/java
  1. 运行测试
代码语言:javascript复制
cd /usr/local/hbase/bin
./start-hbase.sh

ip:16010访问

IDEA源码

目录结构:

  1. 创建一个maven项目并在pom.xml添加依赖:
代码语言:javascript复制
		<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.2</version>
        </dependency>

版本是1.3.2,注意和你自己的一致,可以登录hbase shell时查看。

插播反爬信息 )博主CSDN地址:https://wzlodq.blog.csdn.net/

  1. log4j.properties配置
代码语言:javascript复制
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log

log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.com.codefutures=DEBUG
  1. HBaseCRUD.java
代码语言:javascript复制
package ex3;

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 HbaseCRUD {
    private static Configuration configuration;
    private static Connection connection;
    private static Admin admin;

    public HbaseCRUD() {
        init();
    }

    /**
     * 建立连接
     */
    public void init(){
        configuration=HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","121.36.240.205"); // 换成你自己的IP
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        try{
            connection=ConnectionFactory.createConnection(configuration);
            admin=connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 关闭连接
     */
    public void close(){
        try{
            if(admin!=null)
                admin.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 创建表
     * @param myTableName 表名
     * @param colFamily 列族数组
     * @throws IOException
     */
    public void createTable(String myTableName,String[]colFamily)throws IOException{
        TableName tablename = TableName.valueOf(myTableName);
        if(admin.tableExists(tablename)){
            System.out.println("表已存在,删除旧表");
            admin.disableTable(tablename);//使表无效
            admin.deleteTable(tablename);//删表
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
        for(String str:colFamily){  //增加一列
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor); //建表
        System.out.println("建表成功");
    }

    /**
     * 插入数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param fields 列族(或列族:列限定符)
     * @param values  值
     * @throws IOException
     */
    public void addRecord(String tableName,String rowKey,String []fields,String [] values) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        for (int i = 0; i < fields.length; i  ) {
            Put put = new Put(rowKey.getBytes());
            String [] cols = fields[i].split(":");
            if(cols.length == 1)
                put.addColumn(cols[0].getBytes(), "".getBytes(), values[i].getBytes());
            else
                put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
            table.put(put);
        }
        table.close();
    }

    /**
     * 查询数据
     * @param tableName 表名
     * @param column 列族(或列族:列限定符)
     * @throws IOException
     */
    public void scanColumn (String tableName,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        String [] cols = column.split(":");
        if(cols.length==1)
            scan.addFamily(column.getBytes());
        else
            scan.addColumn(Bytes.toBytes(cols[0]), cols[1].getBytes());
        ResultScanner scanner = table.getScanner(scan);
        for (Result result = scanner.next(); result !=null;result = scanner.next()) {
            for(Cell cell:result.rawCells()){
                System.out.print("列族:" new String(CellUtil.cloneFamily(cell)));
                System.out.print(",行键:" new String(CellUtil.cloneRow(cell)));
                System.out.print(",列名:" new String(CellUtil.cloneQualifier(cell)));
                System.out.print(",值:" new String(CellUtil.cloneValue(cell)));
                System.out.println();
            }
        }
        table.close();
    }

    /**
     * 修改数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param column 列族(或列族:列限定符)
     * @param value 值
     * @throws IOException
     */
    public void modifyData(String tableName,String rowKey,String column,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        String [] cols = column.split(":");
        if(cols.length==1)
            put.addColumn(column.getBytes(),"".getBytes() , value.getBytes());
        else
            put.addColumn(cols[0].getBytes(),cols[1].getBytes() , value.getBytes());
        table.put(put);
        table.close();
    }

    /**
     * 删除数据
     * @param tableName 表名
     * @param rowKey 行键
     * @throws IOException
     */
    public void deleteRow(String tableName,String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowKey.getBytes());
        table.delete(delete);
        table.close();
    }
}
  1. Client.java
代码语言:javascript复制
package ex3;

import java.io.IOException;

public class Client {
    public static void main(String[] args) throws IOException {
        HbaseCRUD hbaseCRUD = new HbaseCRUD();
        String[] cols=new String[]{"S_No","S_Name","S_Sex","S_Age"};
        //建表
        hbaseCRUD.createTable("student",cols);
        //插入
        hbaseCRUD.addRecord("student","2015001",cols,new String[]{"2015001","Zhangsan","male","23"});
        hbaseCRUD.addRecord("student","2015002",cols,new String[]{"2015002","Mary","female","22"});
        hbaseCRUD.addRecord("student","2015003",cols,new String[]{"2015003","Lisa","male","24"});
        //查询
        hbaseCRUD.scanColumn("student","S_Name");
        //修改
        hbaseCRUD.modifyData("student","2015001","S_Age","22");
        hbaseCRUD.scanColumn("student","S_Age");
        //删除
        hbaseCRUD.deleteRow("student","2015001");
        hbaseCRUD.scanColumn("student","S_No");
        hbaseCRUD.close();
        System.out.println("记得一键三连~");
    }
}

点击运行后需要点耐心等待,灰色INFO、DEBUG信息都可不用管,报错的话会是红色字体(评论区交流),有tomcat内味了。

最后感谢大佬YCB的帮助。

0 人点赞