HBaseJava10000

来源:互联网 发布:股票行情交易软件 编辑:程序博客网 时间:2024/05/18 23:13
package cn.test_hbase;import java.util.ArrayList;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.TableName;import org.apache.hadoop.hbase.ZooKeeperConnectionException;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;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.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;import org.apache.hadoop.hbase.filter.CompareFilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.FilterList.Operator;import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;import org.apache.hadoop.hbase.filter.RegexStringComparator;import org.apache.hadoop.hbase.filter.RowFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.util.Bytes;import org.junit.After;import org.junit.Before;import org.junit.Test;public class Hbase1Test {//配置static Configuration config = null;private Connection connection = null;private Table table = null;//关联连接写在下面 @after@Beforepublic void init() throws Exception {config = HBaseConfiguration.create();// 配置,分布式嘛,要告诉你很多信息//hbase要连接那些zk   // zookeeper地址config.set("hbase.zookeeper.quorum", "master,work1,work2");// zookeeper端口2181  2888数据端口 3888心跳端口config.set("hbase.zookeeper.property.clientPort", "2181");connection = ConnectionFactory.createConnection(config);//给个表名通过连接池获得表的连接table = connection.getTable(TableName.valueOf("user"));}//创建一个表,测试用//一般不用api做表的创建,就一句话至于这麻烦么@Testpublic void createTable() throws Exception {// 创建表管理器HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理// 表名称TableName tableName = TableName.valueOf("test3"); //创建表描述类HTableDescriptor desc = new HTableDescriptor(tableName);// 创建列族的描述类HColumnDescriptor family = new HColumnDescriptor("info"); // 列族// 将列族添加到表中desc.addFamily(family);HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族// 将列族添加到表中desc.addFamily(family2);// 创建表admin.createTable(desc); // 创建表}//删除表@Test@SuppressWarnings("deprecation")public void deleteTable() throws MasterNotRunningException,ZooKeeperConnectionException, Exception {HBaseAdmin admin = new HBaseAdmin(config);admin.disableTable("test3");admin.deleteTable("test3");admin.close();}//向hbase中增加数据  put@SuppressWarnings({ "deprecation", "resource" })@Testpublic void insertData() throws Exception {//table.put(arrayList);//table.setAutoFlushTo(false);//table.setWriteBufferSize(534534534);////插入一条就不用搞arraylist了ArrayList<Put> arrayList = new ArrayList<Put>();for (int i = 21; i < 50; i++) {//Bytes.toBytes( hbase帮你提供的转二进制工具//set是只加一条,add是可以加多条Put put = new Put(Bytes.toBytes("1234"+i));//rowkey一样就覆盖//列族:列:值put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"+i));//注意数字和字符串存进去转码后是不一样的put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234+i));arrayList.add(put);}//插入数据table.put(arrayList);//提交//table.flushCommits();}//修改数据 就是 插入覆盖@Testpublic void uodateData() throws Exception {Put put = new Put(Bytes.toBytes("1234"));put.add(Bytes.toBytes("info"), Bytes.toBytes("namessss"), Bytes.toBytes("lisi1234"));put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234));//插入数据table.put(put);//提交//table.flushCommits();}//删除数据 delete 也可删除多个listdelete@Testpublic void deleteDate() throws Exception {Delete delete = new Delete(Bytes.toBytes("1234"));//rowkey一行都没了table.delete(delete);//Delete delete = new Delete(Bytes.toBytes("1234"));//delete.addFamily(Bytes.toBytes("info1")); //指定要删除的列族//table.delete(delete);//Delete delete = new Delete(Bytes.toBytes("1234"));//指定要删除的列族下的列,不传时间戳所有版本都删了//delete.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));//table.delete(delete);//table.flushCommits();}//单条查询 get@Testpublic void queryData() throws Exception {//创建封装查询条件的类Get get = new Get(Bytes.toBytes("1234"));Result result = table.get(get);//可以把结果拿出来放到一个javabean里//拿出来是个byte[],将其转成相应的类型,空可不能转哦 空就不用转直接输出了System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("namessss"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex"))));//查询一条数据的某一个列族//Get get = new Get(Bytes.toBytes("1234"));//get.addFamily(Bytes.toBytes("info1"));//get.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));//Result result1 = table.get(get);}//全表扫描   scan,整个表都会扫@Testpublic void scanData() throws Exception {Scan scan = new Scan();//scan.addFamily(Bytes.toBytes("info"));//单独查某一列族//scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"));//单独查某一列,可以放到javabean的属性上//scan.setStartRow(Bytes.toBytes("wangsf_0"));//也可以指定一个段去扫描 ,注意字典序设置这两个(区间)//scan.setStopRow(Bytes.toBytes("wangwu"));//z也行ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {//列族,列名都在这里面//System.out.println(Bytes.toString(result.getRow());System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));//System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("password"))));//System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"))));}}/** * 全表扫描的过滤器  *注意是列值 * 列值过滤器:等于,不等于,一个范围的过滤 */@Testpublic void scanDataByFilter1() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//过滤器:列值过滤器   要过滤,肯定是在scan里过滤//就是过滤列族info的列name=zhangsan2的记录(CompareOp.EQUAL是等于)SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,Bytes.toBytes("zhangsan2"));// 设置过滤器scan.setFilter(filter);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {//如果这个输的值返回为null不能直接输出Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))//得写成String aa=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));//然后输出System.out.println(aa);否责会报错System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));//System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("password"))));//System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"))));}}//列名前缀过滤器   *注意是列名@Testpublic void scanDataByFilter3() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//匹配rowkey以na开头的ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));// 设置过滤器scan.setFilter(filter);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println("rowkey:" + Bytes.toString(result.getRow()));System.out.println("info:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name"))));// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {System.out.println("info:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {System.out.println("infi:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("sex"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {System.out.println("info2:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("name"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {System.out.println("info2:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {System.out.println("info2:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("sex"))));}}}//多个列名前缀过滤器—MultipleColumnPrefixFilter@Testpublic void scanMultipleColumnPrefixFilter() throws Exception {Scan scan = new Scan();//MultipleColumnPrefixFilter 和 ColumnPrefixFilter 行为差不多,但可以指定多个前缀byte[][] prefixes = new byte[][] {Bytes.toBytes("name_"),Bytes.toBytes("age_")};//MultipleColumnPrefixFilter filter = new MultipleColumnPrefixFilter(prefixes);//和上面是子类父类接收而已Filter filter1 = new MultipleColumnPrefixFilter(prefixes);scan.setFilter(filter1);ResultScanner scanner = table.getScanner(scan);//...}//rowkey过滤器, rowkey的正则表达式  *注意是正则表达式   用的最多@Testpublic void scanDataByFilter2() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//匹配rowkey=以12341开头的所有,*表示后面还有很多RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^12341*"));// 设置过滤器scan.setFilter(filter);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));//System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("password"))));//System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"))));}}//过滤器集合  *即要过滤列族以什么开头,又要过滤name=zs的,把过滤器串起来@Testpublic void scanDataByFilter4() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//过滤器集合:MUST_PASS_ALL(and),MUST_PASS_ONE(or)FilterList filterList = new FilterList(Operator.MUST_PASS_ONE);//匹配rowkey以wg开头的RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^wg"));//匹配name的值等于lisiSingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"), CompareOp.EQUAL,Bytes.toBytes("lisi"));filterList.addFilter(filter);filterList.addFilter(filter2);// 设置过滤器scan.setFilter(filterList);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println("rowkey:" + Bytes.toString(result.getRow()));System.out.println("info:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name"))));// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {System.out.println("info:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {System.out.println("infi:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("sex"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {System.out.println("info2:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("name"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {System.out.println("info2:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {System.out.println("info2:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("sex"))));}}}@Afterpublic void close() throws Exception {table.close();connection.close();}}

0 0
原创粉丝点击