Eclipse 操作Hbase 代码一

本文将介绍如何在Eclipse环境中进行Hbase的开发工作,从创建项目到编写连接和操作Hbase的代码,帮助初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.gense;


import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;


public class HbaseTest_01
{
    private static Configuration conf = null;
    
    /**
     * static hadoop configuration object
     */
    static 
    {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.146.129");
    }
    
    /**
     * create hbase table
     * @param tableName
     * @param columnFamilys
     * @throws IOException
     * @see [类、类#方法、类#成员]
     */
    private static void createTable(String tableName,String[] columnFamilys) throws IOException
    {
        HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
        //判断tableName是否存在
        if(hbaseAdmin.tableExists(tableName))
        {
           System.out.println("the "+tableName +" is exists");
        }
        else
        {
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);
            for(String column:columnFamilys)
            {
                tableDesc.addFamily(new HColumnDescriptor(column));
            }
            hbaseAdmin.createTable(tableDesc);
            System.out.println("create table "+tableName+" success");
        }
    }
    
    /**
     * delete hbase table 
     * @param tableName
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    private static void deleteTable(String tableName) throws IOException
    {
        HBaseAdmin admin = new HBaseAdmin(conf);
        //判断tableName是否存在
        if(!admin.tableExists(tableName))
        {
            System.out.println("the "+tableName+" not exists");
        }
        else
        {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("the "+tableName+" delete success");
        }
        
    }
    
    /**
     * insert data into table
     * @param tableName
     * @param row
     * @param family
     * @param qualifier
     * @param value
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    private static void put(String tableName,String row,String family,String qualifier,String value) throws IOException
    {
        HTable hTable = new HTable(conf, tableName);
        
        Put put = new Put(Bytes.toBytes(row));
        put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
        hTable.put(put);
        System.out.println("insert record "+row +" to table  "+tableName+" success");
    }
    
    /**
     * delete record
     * @param tableName
     * @param rowkey
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    private static void delete(String tableName,String rowkey) throws IOException
    {
        HTable hTable = new HTable(conf, tableName);
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        hTable.delete(delete);
        
        System.out.println("delete record "+rowkey +" from table "+tableName+" success");
        
    }
    
    /**
     * get data from table
     * @param tableName
     * @param rowkey
     * @param family
     * @param qualifier
     * @return 
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    private static void get(String tableName,String rowkey,String family,String qualifier) throws IOException
    {
        HTable table = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowkey));
        //column family
        if(family!=null)
        {
            //qualifier
            if(qualifier!=null)
            {
                get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier));
            }else
            {
                get.addFamily(Bytes.toBytes(family));
            }
        }
        
        Result result = table.get(get);
        for(KeyValue value : result.raw())
        {
            System.out.println(new String(value.getRow())+"\t"+new String(value.getFamily())+":"+new String(value.getQualifier())+"\t"+new String(value.getValue())+"\t"+value.getTimestamp());
        }
    }
    
    /**
     * scan all data
     * @param tableName
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    private static void scan(String tableName) throws IOException
    {
        HTable table = new HTable(conf, tableName);
        //defined scan
        Scan scan = new Scan();
        //scan table 
        ResultScanner scanner = table.getScanner(scan);
        
        for(Result result : scanner)
        {
            for(KeyValue value : result.raw())
            {
                System.out.println(new String(value.getRow())+"\t"+new String(value.getFamily())+":"+new String(value.getQualifier())+"\t"+new String(value.getValue())+"\t"+value.getTimestamp());
            }
        }
    }
    
    /** <一句话功能简述>
     * <功能详细描述>
     * @param args
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args) throws IOException
    {
        //hbase create table
        //createTable("test1", new String[]{"baseinfo"});
        //delete table 
        //deleteTable("test");
        //insert data
        //put("test1", "kitty", "baseinfo", "", "kitty");
        //put("test1", "kitty", "baseinfo", "age", "22");
        //delete("test1", "kitty");
        //get data
        //get("test1","kitty",null,null);
        
        //scan all data
        scan("test1");
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值