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())); } } }}
阅读全文
0 0
- HBase 0.98增删改查java代码实现
- JAVA增删改查代码
- JAVA增删改查代码
- hbase 增删改查
- hbase 增删改查 java示例
- HBase java 增删改查操作
- java操作HBase增删改查基础
- Hbase 的java 增删改查操作
- HBase java 增删改查操作
- Hbase中java中增删改查
- Java实现增删改查
- java连接mysql实现增删改查代码
- MongoDB使用java代码实现增删改查
- java 根据数据表实现增删改查代码自动生成
- hibernate中的增删改查实现代码
- hbase数据增删改查
- hbase 增删查改操作
- HbaseDaoImp HBASE 增删改查
- mac安装thrift-0.9.1
- linux之sed用法
- .Net学习框架
- Python 字符串转python json.decoder.JSONDecodeError
- git roll back the previous version
- HBase 0.98增删改查java代码实现
- lintcode刷题——整数转罗马数字
- javaweb:判断当前请求是否为移动设备访问
- 新学期新开始
- Js之原型模式
- Qt之自定义界面添加最小化、最大化和关闭按钮
- jq中自定义属性取值 data-info
- centos7 安装mysql以及常见问题的解决
- POI X Sums(同余最短路)