HBase1.2.4新版api操作示例

 

       用java操作hbase,hbase1.2.4新版对api做了很多调整,当然原来的api也能用,只是会提示你is  deprecated,对象上还会被划一道横线,看着不爽,旧api早晚会淘汰,所以有必要使用新api:


1.代码如下

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;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by hasee on 2017/3/3.
 */
public class HBaseDao {
    public static Configuration conf;
    public static Connection connection;
    static{
        Configuration HBASE_CONFIG=new Configuration();
        HBASE_CONFIG.set("hbase.zookeeper.quorum","192.168.73.1,192.168.73.2,192.168.73.3");
        HBASE_CONFIG.set("hbase.zookeeper.property.clientPort","2181");
        conf= HBaseConfiguration.create(HBASE_CONFIG);
        try {
            connection=ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建表
     * @param tableName
     * @param familys
     * @throws IOException
     */
    public static void createTable(String tableName,String[] familys) throws IOException {
        //HBaseAdmin admin=new HBaseAdmin(conf);
       // Connection connection= ConnectionFactory.createConnection(conf);
        Admin admin=connection.getAdmin();
        if(admin.tableExists(TableName.valueOf(tableName))){
            System.out.println("table already exists!");
        }else {
            HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
            for(int i=0;i<familys.length;i++){
                tableDescriptor.addFamily(new HColumnDescriptor(familys[i]));
            }
            admin.createTable(tableDescriptor);
            System.out.println("create table "+tableName+" ok.");
        }
    }

    /**
     * 删除表
     * @param tableName
     * @throws IOException
     */
    public static void deleteTable(String tableName) throws Exception {
        try {
            Admin admin=connection.getAdmin();
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("delete table "+tableName+" ok.");
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        }catch (ZooKeeperConnectionException e){
            e.printStackTrace();
        }
    }

    /**
     * 插入数据
     * @param tableName
     * @param rowKey
     * @param family
     * @param qualifier
     * @param value
     * @throws Exception
     */
    public static void addRecord(String tableName,String rowKey,String family,String qualifier,String value) throws Exception {
        try {
            Table table=connection.getTable(TableName.valueOf(tableName));
            Put put=new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
            table.put(put);
            System.out.println("insert record  "+rowKey+" to table "+tableName+" ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据
     * @param tableName
     * @param rowKey
     * @throws Exception
     */
    public static void getOneRecord(String tableName,String rowKey) throws IOException {

            Table table=connection.getTable(TableName.valueOf(tableName));
            Get get=new Get(rowKey.getBytes());
            Result rs=table.get(get);
            for(Cell cell:rs.rawCells()){
                System.out.println(new String(cell.getRowArray())+" ");
                System.out.println(new String(cell.getFamilyArray())+":");
                System.out.println(new String(cell.getQualifierArray())+" ");
                System.out.println(cell.getTimestamp()+" ");
                System.out.println(new String(cell.getValueArray())+" ");
            }

    }

    /**
     * 浏览每一行
     * @param tableName
     */
    public static void getAllRecord(String tableName) {

        try {
            Table table=connection.getTable(TableName.valueOf(tableName));
            Scan scan=new Scan();
            ResultScanner rs=table.getScanner(scan);
            for (Result r:rs) {
                for (Cell cell : r.rawCells()) {
                    System.out.println(new String(cell.getRowArray()) + " ");
                    System.out.println(new String(cell.getFamilyArray()) + ":");
                    System.out.println(new String(cell.getQualifierArray()) + " ");
                    System.out.println(cell.getTimestamp() + " ");
                    System.out.println(new String(cell.getValueArray()) + " ");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void deleteRecord(String tableName,String rowKey) throws IOException {

        Table table=connection.getTable(TableName.valueOf(tableName));
        List list=new ArrayList();
        Delete delete=new Delete(rowKey.getBytes());
        list.add(delete);
        table.delete(list);
        System.out.println("delete record "+rowKey+" ok.");
    }

    public static void main(String[] args) throws Exception {
        HBaseDao hBaseDao=new HBaseDao();
        //hBaseDao.createTable("hello",new String[]{"ai","bi"});
        //hBaseDao.addRecord("hello","row2","bi","521","yangqing");
        //hBaseDao.getOneRecord("hello","row2");
        //hBaseDao.getAllRecord("hello");
        hBaseDao.deleteRecord("hello","row1");
    }
}


2.另外我是在idea中的maven项目中开发的,下面贴上java操作hbase的maven依赖:

<dependencies>
    <!--Hadoop main artifact-->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-common</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.2.4</version>
    </dependency>

    <!--Unit test artifacts-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.8</version>
    </dependency>
</dependencies>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值