java API进行hbase操作

判断表是否存在


//表是否存在
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;

public class Hbase {
    public static boolean isExist(String tableName) throws IOException {
        HBaseConfiguration configuration=new HBaseConfiguration();
        configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03");
        HBaseAdmin hBaseAdmin=new HBaseAdmin(configuration);
        boolean exist=hBaseAdmin.tableExists(tableName);
        hBaseAdmin.close();
        return exist;
    }
    public static void main(String[] args) throws IOException{
        System.out.println("aaaaaaaaa");
        System.out.println(isExist("mytable2"));
        System.out.println("bbb");
    }
}

创建表


//创建表
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.junit.Before;

import java.io.IOException;

public class HbaseTest {


    public static void main(String[] args) {
        String createTableName = "mytable2";
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "centos01,centos02,centos03");
        //configuration.set("hbase.master", "192.168.31.128:600000");
        System.out.println("start create table ......");
        try {
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
            HTableDescriptor tableDescriptor = new HTableDescriptor(createTableName);
            tableDescriptor.addFamily(new HColumnDescriptor("column1"));
            tableDescriptor.addFamily(new HColumnDescriptor("column2"));
            tableDescriptor.addFamily(new HColumnDescriptor("column3"));
            hBaseAdmin.createTable(tableDescriptor);
            hBaseAdmin.close();
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end create table ......");
    }

}

插入数据


//插入数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.util.Bytes;

import java.io.IOException;

public class HbaseDataAdd {
    public static void main(String[] args) {
        String tablename = "mytable2";
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03");
        HBaseAdmin admin;
        try {
            admin = new HBaseAdmin(configuration);
            if (admin.tableExists(tablename)){
                HTable table = new HTable(configuration,tablename);
                Put put = new Put(Bytes.toBytes("8"));
                put.add(Bytes.toBytes("column1"),Bytes.toBytes("name"),Bytes.toBytes("xiaoming"));
                table.put(put);
                System.out.println("add success!");
            }else {
                System.out.println(tablename+" does not exist!");
            }
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

获取表中的数据


//读取表中的数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.yecht.Data;

import java.io.IOException;

public class HbaseAddData {
    public static void main(String[] args) {
        String tablename = "mytable2";
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03");
        HTable table;
        try {
            table = new HTable(configuration,tablename);
            Scan scan = new Scan();
            ResultScanner results = table.getScanner(scan);
            for (Result result:results){
                for (Cell cell:result.rawCells()){
                    System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
                    System.out.println("timetamp:"+cell.getTimestamp()+" ");
                    System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
                    System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
                    System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

读一行数据


//读一行数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.yecht.Data;

import java.io.IOException;

public class HbaseGetData {
    public static void main(String[] args) {
        String tablename = "mytable2";
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03");
        HTable table;
        try {
            table = new HTable(configuration,tablename);
            Get get = new Get(Bytes.toBytes("1"));
            Result result = table.get(get);
            for (Cell cell:result.rawCells()){
                System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
                System.out.println("Timetamp"+cell.getTimestamp()+" ");
                System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
                System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
                System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

删除数据


//删除数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HbaseDataDelete {
    public static void main(String[] args) {
        String tablename = "mytable2";
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "centos01,centos02,centos03");
        HBaseAdmin admin;
        try {
            admin = new HBaseAdmin(configuration);
            if (admin.tableExists(tablename)){
                HTable table = new HTable(configuration,tablename);
                Delete delete = new Delete(Bytes.toBytes("1"));
                table.delete(delete);
                System.out.println("delete success!");
            }
            else {
                System.out.println("Table does not exist");
            }
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

删除单行数据


//删除单行数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
import org.yecht.Data;

import java.io.IOException;

public class HbaseDeleteData {
    public static void main(String[] args) {
        String tableName = "mytable2";
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "centos01,centos02,centos03");
        HBaseAdmin admin;
        try {
            admin = new HBaseAdmin(configuration);
            if (admin.tableExists(tableName)){
                HTable table = new HTable(configuration,tableName);
                Delete delete = new Delete(Bytes.toBytes(2));
                delete.deleteColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
                table.delete(delete);
                System.out.println("delete success");
            }else {
                System.out.println("Table does not exist");
            }
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注:在运行程序前需打开hdfs、zookeeper、hbase,并关闭防火墙,对本地的hosts(C:\Windows\System32\drivers\etc\hosts)进行配置,在hosts文件后边加上虚拟机ip及名称,然后在项目中引入hbase客户端的moven依赖。
此项目我加入的依赖是

<dependencies>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>1.3.1</version>
    </dependency>
</dependencies>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

且从容.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值