@Test public void testCreateTable() throws Exception { //1. 去读取加载classpath下的hbase-site.xml配置文件 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.0.208:2181,192.168.0.209:2181,192.168.0.210:2181"); //2. 拿到一个hbase的ddl客户端----建表、删表、禁用表、启用表。。。。。。。 HBaseAdmin hBaseAdmin = new HBaseAdmin(conf); //3.创建表 TableName tableName = TableName.valueOf("user_info"); HTableDescriptor tableDesc = new HTableDescriptor(tableName); //4.创建列 HColumnDescriptor family = new HColumnDescriptor("base_info"); family.setMaxVersions(3);//设置列族最多存几个版本 tableDesc.addFamily(family); hBaseAdmin.createTable(tableDesc); hBaseAdmin.close(); }
/** * 插入数据 * * @throws IOException */ @Test public void testPut() throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hdp-node01:2181,hdp-node02:2181,hdp-node03:2181"); //1. 拿一个hbase的DML客户端,针对一个指定的表 HTable userInfo = new HTable(conf, "user_info"); //2. 将要插入的kv数据封装到一个put对象中 Put put = new Put(Bytes.toBytes("rk-0001"));//行健 put.add(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("刘亦菲"));// 列族 字段 字段参数 put.add(Bytes.toBytes("base_info"), Bytes.toBytes("boyfriend"), Bytes.toBytes("任我行")); Put put2 = new Put(Bytes.toBytes("rk-0002")); put2.add(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("汤唯")); put2.add(Bytes.toBytes("base_info"), Bytes.toBytes("boyfriend"), Bytes.toBytes("任我行")); Put put3 = new Put(Bytes.toBytes("rk-0003")); put3.add(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("杨颖")); put3.add(Bytes.toBytes("base_info"), Bytes.toBytes("boyfriend"), Bytes.toBytes("任我行")); ArrayList<Put> puts = new ArrayList<Put>(); puts.add(put); puts.add(put2); puts.add(put3); userInfo.put(puts); userInfo.close(); }
/** * 删除表中的数据 * * @throws Exception */ @Test public void testDelete() throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.0.208:2181,192.168.0.209:2181,192.168.0.210:2181"); // 拿一个hbase的DML客户端,针对一个指定的表 HTable userInfo = new HTable(conf, "user_info"); // 如果delete中不指定要删除的列,则会删除整行 Delete delete = new Delete(Bytes.toBytes("rk-0003")); delete.deleteColumn(Bytes.toBytes("base_info"), Bytes.toBytes("boyfriend")); userInfo.delete(delete); userInfo.close(); }
@Test public void testUpdate() throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.0.208:2181,192.168.0.209:2181,192.168.0.210:2181"); // 拿一个hbase的DML客户端,针对一个指定的表 HTable userInfo = new HTable(conf, "user_info"); Put put = new Put(Bytes.toBytes("rk-0003")); put.add(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("我的最爱")); userInfo.put(put); userInfo.close(); }
/** * 查询一行中的数据 * * @throws Exception */ @Test public void testGet() throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.0.208:2181,192.168.0.209:2181,192.168.0.210:2181"); // 拿一个hbase的DML客户端,针对一个指定的表 HTable userInfo = new HTable(conf, "user_info"); // 根据行键构造一个get参数对象,如果get对象中不指定具体的列,则会返回整行 Get get = new Get(Bytes.toBytes("rk-0002")); // 指定返回base_info中的所有列(kv) get.addFamily(Bytes.toBytes("base_info")); Result result = userInfo.get(get); // 从结果集中取一个指定的列 // byte[] boyfriendBytes = result.getValue("base_info".getBytes(), // "boyfriend".getBytes()); // // String boyfriend = new String(boyfriendBytes); // System.out.println(boyfriend); /** * 遍历result中所有的列 */ List<KeyValue> kvs = result.list(); for (KeyValue kv : kvs) { System.out.println(Bytes.toString(kv.getFamily())); // 列族 System.out.println(Bytes.toString(kv.getQualifier())); // 列 System.out.println(Bytes.toString(kv.getValue())); // 值 } userInfo.close(); }
/** * 范围查询,可以查询指定行键范围内的所有数据 * * @throws IOException */ @Test public void testScan() throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.0.208:2181,192.168.0.209:2181,192.168.0.210:2181"); // 拿一个hbase的DML客户端,针对一个指定的表 HTable userInfo = new HTable(conf, "user_info"); //行键范围中,含头不含尾 Scan scan = new Scan(Bytes.toBytes("rk-0001"), Bytes.toBytes("rk-0003"+"\0x00")); ResultScanner scanner = userInfo.getScanner(scan); //用迭代的方式来遍历scanner /* Iterator<Result> iterator = scanner.iterator(); while(iterator.hasNext()){ Result result = iterator.next(); }*/ //用增强for循环遍历scanner for (Result result : scanner) { List<KeyValue> kvs = result.list(); for (KeyValue kv : kvs) { System.out.println(Bytes.toString(kv.getFamily())); // 列族 System.out.println(Bytes.toString(kv.getQualifier())); // 列 System.out.println(Bytes.toString(kv.getValue())); // 值 } } userInfo.close(); }
本文出自 “为了手指那个方向” 博客,谢绝转载!