sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

hbase的java编程实例 hbase项目实战


首先 根据 hadoop 搭建 + hbase 搭建把 环境弄好

由于 hbase 依赖于 hdfs ,所以 需要 进入 hadoop --》sbin  下 启动 start-dfs.sh , start-yarn.sh
然后 进 hbase --> 下 启动 start-hbase.sh  如果 后面 运行失败 报 找不到 zookeeper 八成 你需要进 hbase-bin-> stop-hbase 然后 重run  start-hbase.sh
  • 1.
  • 2.

hbase的java编程实例 hbase项目实战_hadoop

这里列举下 hbase shell 的常用操作

#最有用命令 help
help 'status'
#建表
create 'FileTable','fileInfo','saveInfo'
#列出有哪些表
list
#描述表信息
desc 'FileTable'
#统计 表
count 'FileTable'
#添加列簇
alter 'FileTable','cf'
# 删除列簇 ,一定要注意大小写
alter 'FileTable',{NAME=>'cf',METHOD=>'delete'}

插入数据



put 'FileTable','rowkey1','fileInfo:name','file1.txt'

0 row(s) in 3.9840 seconds

put 'FileTable','rowkey1','fileInfo:type','txt'

0 row(s) in 0.0410 seconds

put 'FileTable','rowkey1','fileInfo:size','1024'

0 row(s) in 0.1100 seconds

put 'FileTable','rowkey1','saveInfo:path','/home/pics'

0 row(s) in 0.1320 seconds

put 'FileTable' , 'rowkey1','saveInfo:creator','tom'

0 row(s) in 0.1430 seconds



查询表总行数



hbase(main):016:0> count 'FileTable'

1 row(s) in 0.8500 seconds



get 查询



get 'FileTable','rowkey1'

get 'FileTable','rowkey1','fileInfo'



delete


deleteall


scan


删除表之前必须先禁用表Drop the named table. Table must first be disabled:



disable 'FileTable'




is_enabled

is_disabled


drop 'FileTable'

查询所有列簇

hbase的java编程实例 hbase项目实战_java_02

查询指定列簇

hbase的java编程实例 hbase项目实战_java_03

HBase 连接类

package com.ghc.hbase.api;




import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.Table;




import java.io.IOException;




public class HBaseConn {

private static final HBaseConn INSTANCE = new HBaseConn();

private static Configuration configuration;

private static Connection connection;



private HBaseConn(){
    try{
        if(configuration == null){
            configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181");
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}
private Connection getConnection(){
    if(connection == null || connection.isClosed()){
        try{
            connection = ConnectionFactory.createConnection(configuration);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    return connection;
}

public static Connection getHBaseConnection(){
    return INSTANCE.getConnection();
}

public static Table getTable(String tableName) throws IOException{
    return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}

public static void closeConn(){
    if(connection != null){
        try{
            connection.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}


private HBaseConn(){
    try{
        if(configuration == null){
            configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181");
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}
private Connection getConnection(){
    if(connection == null || connection.isClosed()){
        try{
            connection = ConnectionFactory.createConnection(configuration);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    return connection;
}

public static Connection getHBaseConnection(){
    return INSTANCE.getConnection();
}

public static Table getTable(String tableName) throws IOException{
    return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}

public static void closeConn(){
    if(connection != null){
        try{
            connection.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
}

junit 测试一波连接类

import com.ghc.hbase.api.HBaseConn;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.Table;

import org.junit.Test;




import java.io.IOException;


public class HBaseConnTest {

@Test

public void getConnTest(){

Connection conn = HBaseConn.getHBaseConnection();

System.out.println(conn.isClosed());

HBaseConn.closeConn();

System.out.println(conn.isClosed());

}

@Test

public void getTableTest(){

Table table = null;

try{table = HBaseConn.getTable("FileTable");

System.out.println(table.getName());

}catch(IOException ioe){

ioe.printStackTrace();

}

}

}

hbase 增删操作类

package com.ghc.hbase.api;




import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.filter.FilterList;

import org.apache.hadoop.hbase.util.Bytes;




import java.io.IOException;

import java.util.Arrays;

import java.util.List;

import java.util.Scanner;




public class HBaseUtil {



public static Boolean createTable(String tableName,String[] cfs){
    try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
            if(admin.tableExists(tableName)) {
                return false;
            }
            // 不存在 则创建
            HTableDescriptor  hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            Arrays.stream(cfs).forEach(cf-&gt;{
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
                hColumnDescriptor.setMaxVersions(1);
                hTableDescriptor.addFamily(hColumnDescriptor);
            });
            admin.createTable(hTableDescriptor);
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){
    try(Table table = HBaseConn.getTable(tableName)){
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
        table.put(put);
    }catch (IOException ioe){
        ioe.printStackTrace();
    }
    return true;
}

public static Boolean putRows(String tableName,List&lt;Put&gt; puts){
    try(Table table = HBaseConn.getTable(tableName)){
        table.put(puts);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return true;
}

public static Result getRow(String tableName, String rowKey){
    try(Table table = HBaseConn.getTable(tableName)){
        Get get = new Get(Bytes.toBytes(rowKey));
        return table.get(get);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

public static Result getRow(String tableName, String rowKey, FilterList filterList){
    try(Table table = HBaseConn.getTable(tableName)){
        Get get = new Get(Bytes.toBytes(rowKey));
        get.setFilter(filterList);
        return table.get(get);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

public static ResultScanner getScanner(String tableName){
    try(Table table = HBaseConn.getTable(tableName)){
        Scan scan = new Scan();
        scan.setCaching(1000);
        return table.getScanner(scan);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){
    try(Table table = HBaseConn.getTable(tableName)){
        Scan  scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRowKey));
        scan.setStopRow(Bytes.toBytes(endRowKey));
        scan.setCaching(1000);
        return table.getScanner(scan);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

// 使用过滤器
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){
    try(Table table = HBaseConn.getTable(tableName)){
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRowKey));
        scan.setStopRow(Bytes.toBytes(endRowKey));
        scan.setCaching(1000);
        scan.setFilter(filterList);
        return table.getScanner(scan);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

// 删除
public static Boolean deleteRow(String tableName,String rowKey){
    try(Table table = HBaseConn.getTable(tableName)){
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        return true;
    }catch(IOException ioe){ioe.printStackTrace();}
    return null;
}

// 删除 列簇 用 admin
public static Boolean deleteColumnFamily(String tableName,String cfName){
    try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
        admin.deleteColumn(tableName,cfName);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return true;
}
//删除 某列 用 table
public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){
    try(Table table = HBaseConn.getTable(tableName)){
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier));
        table.delete(delete);
        return true;
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}


public static Boolean createTable(String tableName,String[] cfs){
    try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
            if(admin.tableExists(tableName)) {
                return false;
            }
            // 不存在 则创建
            HTableDescriptor  hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            Arrays.stream(cfs).forEach(cf-&gt;{
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
                hColumnDescriptor.setMaxVersions(1);
                hTableDescriptor.addFamily(hColumnDescriptor);
            });
            admin.createTable(hTableDescriptor);
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){
    try(Table table = HBaseConn.getTable(tableName)){
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
        table.put(put);
    }catch (IOException ioe){
        ioe.printStackTrace();
    }
    return true;
}

public static Boolean putRows(String tableName,List&lt;Put&gt; puts){
    try(Table table = HBaseConn.getTable(tableName)){
        table.put(puts);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return true;
}

public static Result getRow(String tableName, String rowKey){
    try(Table table = HBaseConn.getTable(tableName)){
        Get get = new Get(Bytes.toBytes(rowKey));
        return table.get(get);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

public static Result getRow(String tableName, String rowKey, FilterList filterList){
    try(Table table = HBaseConn.getTable(tableName)){
        Get get = new Get(Bytes.toBytes(rowKey));
        get.setFilter(filterList);
        return table.get(get);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

public static ResultScanner getScanner(String tableName){
    try(Table table = HBaseConn.getTable(tableName)){
        Scan scan = new Scan();
        scan.setCaching(1000);
        return table.getScanner(scan);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){
    try(Table table = HBaseConn.getTable(tableName)){
        Scan  scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRowKey));
        scan.setStopRow(Bytes.toBytes(endRowKey));
        scan.setCaching(1000);
        return table.getScanner(scan);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

// 使用过滤器
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){
    try(Table table = HBaseConn.getTable(tableName)){
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRowKey));
        scan.setStopRow(Bytes.toBytes(endRowKey));
        scan.setCaching(1000);
        scan.setFilter(filterList);
        return table.getScanner(scan);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}

// 删除
public static Boolean deleteRow(String tableName,String rowKey){
    try(Table table = HBaseConn.getTable(tableName)){
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        return true;
    }catch(IOException ioe){ioe.printStackTrace();}
    return null;
}

// 删除 列簇 用 admin
public static Boolean deleteColumnFamily(String tableName,String cfName){
    try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
        admin.deleteColumn(tableName,cfName);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return true;
}
//删除 某列 用 table
public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){
    try(Table table = HBaseConn.getTable(tableName)){
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier));
        table.delete(delete);
        return true;
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
    return null;
}
}


原文链接:https://blog.51cto.com/u_16099325/6353666

posted on   sunny123456  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示