HBase 0.98增删改查java代码实现

来源:互联网 发布:大连知润 编辑:程序博客网 时间:2024/05/17 10:53

HBase0.98代码

增删改查工具用例

package HBase;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.ArrayList;import java.util.List;/** * Created by LiuWenSheng on 2017/8/7. */public class HBaseUtils {    /**     * 创建一个表并制定列族,同一个表只能创建一次,下次如果创建相同的表则会报出异常     * @param admin     * @param tableName 表名     * @param columns 列族的字符串数组     * @throws IOException     */    public void createTable(HBaseAdmin admin,String tableName,String[] columns) throws IOException {        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(Bytes.toBytes(tableName)));        for (String column :columns){            descriptor.addFamily(new HColumnDescriptor(column));        }        admin.createTable(descriptor);    }    /**     * 插入数据     * @param conf 配置信息     * @param tableName 表名     * @param rowKey rowkey     * @param colFamily 列族名     * @param column 列名     * @param value 插入的值     * @throws IOException     */    public void insertData(Configuration conf,String tableName,String rowKey,String colFamily,String column,String value) throws IOException {        HTable table = new HTable(conf, tableName);        Put put = new Put(Bytes.toBytes(rowKey));        put.add(Bytes.toBytes(colFamily),Bytes.toBytes(column),Bytes.toBytes(value));        table.put(put);    }    /**     * 删除表     * @param admin     * @param talbeName     * @throws IOException     */    public void deleteTable(HBaseAdmin admin,String talbeName)throws  IOException{        admin.disableTable(Bytes.toBytes(talbeName));        admin.deleteTable(Bytes.toBytes(talbeName));    }    public void deleteRow(Configuration conf,String tableName ,String row) throws IOException {        HTable table = new HTable(conf,tableName);        Delete del = new Delete(Bytes.toBytes(row));        table.delete(del);    }    public List<Cell[]> findByFilter(Configuration conf, String tableName, Filter filter) throws IOException, IllegalAccessException, InstantiationException {        HTable table = new HTable(conf,tableName);        List<Cell[]> list = new ArrayList<Cell[]>();        Scan scaner = new Scan();        scaner.setFilter(filter);        ResultScanner results = table.getScanner(scaner);        for (Result result:results){            list.add(result.rawCells());        }        return list;    }    public  List<Cell[]>findByScan(Configuration conf,String tableName)throws IOException {        HTable table = new HTable(conf,tableName);        List<Cell[]> list = new ArrayList<Cell[]>();        Scan scaner = new Scan();        ResultScanner results = table.getScanner(scaner);        for (Result result:results){            list.add(result.rawCells());        }        return list;    }}
Hbase中只能说对用户的每一个操作都进行了封装,put是一个类,delete是一个类、等我们可以创建这些类,然后给这些累添加属性。最后使用这些类就可以完成增删改查操作。

测试用例

package test;import HBase.HBaseUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;import org.apache.hadoop.hbase.util.Bytes;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.util.List;/** * Created by Administrator on 2017/8/7. */public class MyHBaseTest {    private HBaseUtils hBaseUtils = new HBaseUtils();    private Configuration  conf = HBaseConfiguration.create();    private HBaseAdmin admin = null;    @Before    public void beforeTest() throws IOException {        conf.set("hbase.zookeeper.quorum", "127.0.0.1");//此处应填你的IP地址,如果在windows下运行必须要填写        conf.set("hbase.zookeeper.property.clientPort", "2181");        admin = new HBaseAdmin(conf);    }    @Test    public void test_createTable() throws IOException {        String[] strs = new String[]{"one","two"};        hBaseUtils.createTable(admin,"test",strs);    }    @Test    public void test_insertData()throws IOException{        hBaseUtils.insertData(conf,"test","rowkey","one","name","xingming");    }    @Test    public void test_findbyFilter()throws Exception{        ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("rowkey"));        List<Cell[]> test = hBaseUtils.findByFilter(conf, "test", filter);        if (test.isEmpty()){            System.out.print("EMPOTY");        }        for (Cell[] cell:test){            for (Cell c:cell){                System.out.print(c.getRow());            }        }    }    @Test    public void test_findByScan()throws Exception{        List<Cell[]> test = hBaseUtils.findByScan(conf, "test");        if (test.isEmpty()){            System.out.print("EMPOTY");        }        for (Cell[] cell:test){            for (Cell c:cell){                System.out.println(new String(c.getRowArray(),c.getRowOffset(),c.getRowLength()));                System.out.println(new String(c.getValueArray(),c.getValueOffset(),c.getValueLength()));                System.out.println(new String(c.getQualifierArray(),c.getQualifierOffset(),c.getQualifierLength()));            }        }    }}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 请问去意大利要怎么办护照 在意大利护照丢了怎么办 考研二战档案打回原籍怎么办 脸上的肉往下掉怎么办 眼镜带了往下掉怎么办 孩子捅别的孩子眼睛了怎么办 眼睛不小心捅伤怎么办 我的爸爸是小偷怎么办 违停罚款忘记交怎么办 顺风车无人接单怎么办 来例假腰特别疼怎么办 把人撞死了全责怎么办 朝鲜与美合作对付中国怎么办 申请美国大学gpa不够怎么办 武装突袭3有地雷怎么办 辐射4狗肉跟丢了怎么办 洛奇英雄传死绑S怎么办 在老挝遇到坏人带枪怎么办 买了sd卡卡槽塞不下怎么办 现役军人家庭被邻居欺服怎么办 地铁买票买多了怎么办 免税店买的东西转机怎么办 绿能电动车坏了怎么办? 永久单车收不到验证码怎么办 24速山地车档乱了怎么办 新电瓶车被偷了怎么办 汽车前风挡玻璃砸出洞怎么办 凯迪拉克xt5钥匙掉了怎么办 凯迪拉克xt5媒体不好用怎么办 晒黄的白鞋怎么办 白鞋子被晒黄了怎么办? 耐克空军一号白色发黄怎么办 空军一号破皮了怎么办 匡威鞋帆布破了怎么办 脚腕起疙瘩很痒怎么办 跑步后脚踝微疼怎么办 跑步跑得脚疼怎么办 nike air 鞋头脱胶怎么办 耐克空军一号磨脚怎么办 白鞋刷完变黄了怎么办 乔丹气垫坏了怎么办