Java操作HBase对象

来源:互联网 发布:手机淘宝积分怎么查询 编辑:程序博客网 时间:2024/06/08 02:43

Java操作HBase对象

Windows系统下,使用Eclipse工具操作HBase对象,使用Java对HBase进行增删改查操作

  1. 新建Java工程
    将下载的HBase包解压到Windows系统目录下,右键新建的Java工程,选择Properties-Java Build Path-Libraries-Add External JARs…,选择解压后的HBase目录下lib目录中的所有jar包导入
  2. 代码
import java.io.IOException;import java.util.ArrayList;import java.util.List;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.MasterNotRunningException;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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.junit.After;import org.junit.Before;import org.junit.Test;public class HBaseTest {    //表名    private static String TABLE_NAME = "stu";    //列簇名    private static String FAMILY_NAME = "f1";    //行键    private static String ROW_KEY1 = "r1";    private static String ROW_KEY2 = "r2";    //列名    private static String COLUMN_NAME = "name";    private static String COLUMN_AGE = "age";    Configuration conf;    HBaseAdmin hBaseAdmin;    HTable hTable;    @Before    public void before() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {        //构造能够访问Hbase的Configuration对象        conf = HBaseConfiguration.create();        //HBase集群配置里的文件内容        conf.set("hbase.rootDir", "hdfs://192.168.0.200:9000/hbase");        conf.set("hbase.zookeeper.quorum", "spark");        //HBaseAdmin是对HBase进行DDL操作的核心类        hBaseAdmin = new HBaseAdmin(conf);        //使用HTable可以对HBase的表中的数据进行增删改查        hTable = new HTable(conf, TABLE_NAME);    }    @After    public void after() throws IOException {        hTable.close();        hBaseAdmin.close();    }    @Test    public void createTable() throws IOException {        if (!hBaseAdmin.tableExists(TABLE_NAME)) {            HTableDescriptor hTableDescriptor = new HTableDescriptor(TABLE_NAME);            hTableDescriptor.addFamily(new HColumnDescriptor(FAMILY_NAME));            hBaseAdmin.createTable(hTableDescriptor);            System.out.println("table created");        } else {            System.out.println("table exists");        }    }    @Test    public void add() throws IOException {        List<Put> listPut = new ArrayList<Put>();        //添加数据        Put put1 = new Put(ROW_KEY1.getBytes());        put1.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), "zhangsan".getBytes());        put1.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), "23".getBytes());        listPut.add(put1);        Put put2 = new Put(ROW_KEY2.getBytes());        put2.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), "lisi".getBytes());        put2.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), "24".getBytes());        listPut.add(put2);        hTable.put(listPut);        System.out.println("添加成功");    }    @Test    public void get() throws IOException {        //获取数据        Get get = new Get(ROW_KEY1.getBytes());        Result result = hTable.get(get);        //传入列簇名和列名获取列的value值        String name = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));        String age = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));        System.out.println(result+"\t name:"+name+",age:"+age);    }    @Test    public void scan() throws IOException {        //获取所有数据        Scan scan = new Scan();        //限定条件 类似于where        //scan.setStartRow(ROW_KEY1.getBytes());        //scan.setStopRow(ROW_KEY2.getBytes());        //限定条件列名为name        scan.addColumn(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes());        ResultScanner resultScanner = hTable.getScanner(scan);        for (Result result2 : resultScanner) {            String rowKey = new String(result2.getRow());            //传入列簇名和列名获取列的value值            String name2 = new String(result2.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));            System.out.println(rowKey+"\t name:"+name2);        }    }    @Test    public void delete() throws IOException {        //删除行键ROW_KEY1        Delete delete = new Delete(ROW_KEY1.getBytes());        hTable.delete(delete);        scan();    }    @Test    public void deleteTable() throws IOException {        //删除表的操作 -- 用HBaseAdmin删除表之前要对表进行disable操作        hBaseAdmin.disableTable(TABLE_NAME);        hBaseAdmin.deleteTable(TABLE_NAME);    }}
0 0
原创粉丝点击