在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
hbase的完全分布式建立起来了, 可以试下好使不 1, 导包, {HBASE_HOME}/lib 下所有的jar包, 导入2, 使用junit测试, 会报错, 因为缺少一个jar3, 获取链接, 只需要提供zookeeper的地址即可/** * 获取链接, 只需要zookeeper的链接就可了 */ @Before public void beforeTest() throws IOException { Configuration conf = HBaseConfiguration.create(); // hbase端口默认2181, 可以使用hbase.zookeeper.property.clientPort设置 conf.set("hbase.zookeeper.quorum", "192.168.208.106,192.168.208.107,192.168.208.108"); // 去的数据库连接对象 connect = ConnectionFactory.createConnection(conf); } /** * 关闭资源 */ @After public void afterTest() throws IOException { if (connect != null) { connect.close(); } }
4, 新建表@Test public void testCreate() throws IOException { //去的一个数据库元操作对象 Admin admin = connect.getAdmin(); if (admin.tableExists(table)) { // 删除需要先disable admin.disableTable(table); admin.deleteTable(table); } // 创建表描述对象 HTableDescriptor htable = new HTableDescriptor(table); // 创建列族描述对象 HColumnDescriptor hColumn = new HColumnDescriptor("cf1".getBytes()); hColumn.setMaxVersions(5); hColumn.setBlockCacheEnabled(true); hColumn.setBlocksize(1800000); // 数据库中新建一个列族 htable.addFamily(hColumn); // 新建数据库 admin.createTable(htable); } 5, 插入模拟数据/** * 数据插入 */ @Test public void testInsert() throws IOException { Table table = connect.getTable(tableName); List<Put> list = new ArrayList<>(); for (int i = 0; i < 1000; i++) { int month = i % 3; int day = i % 9; Put put = new Put(getRowKey("138", month, day).getBytes()); // 列1 put.addColumn("cf1".getBytes(), "address".getBytes(), "bj".getBytes()); // 列2 put.addColumn("cf1".getBytes(), "type".getBytes(), String.valueOf(random.nextInt(2)).getBytes()); list.add(put); } table.put(list); 生成模拟rowkey的方法为: /** * rowkey 生成策略, 电话号 + 日期 */ public String getRowKey(String prePhone, int month, int day) { return prePhone + random.nextInt(99999999) + "_2017" + month + day; } 6, 根据rowkey进行查询因为, rowkey 默认按照字典进行排序 /** * 根据rowkey 查询 * @throws IOException */ @Test public void scan() throws IOException { Table table = connect.getTable(tableName); //rowkey 按照字典排序, 所以可以截取 Scan scan = new Scan("13862288854_201700".getBytes(), "13899338829_201714".getBytes()); ResultScanner scanner = table.getScanner(scan); for (Iterator<Result> iterator = scanner.iterator(); iterator.hasNext(); ) { Result next = iterator.next(); byte[] value = next.getValue("cf1".getBytes(), "type".getBytes()); System.out.println(new String(value, "utf8")); } } 7, filter条件过滤更多可见: http://hbase.apache.org/book.html#client.filter 第67条, 82.8 条 更多filter, 找了个比较详细的博客: http://blog.csdn.net/cnweike/article/details/42920547 @Test public void filter() throws IOException { // All表示全部匹配, 相当于and, one 相当于 or FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); // 查询某一个电话, type 等于 1 PrefixFilter prefixFilter = new PrefixFilter("1388".getBytes()); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("cf1".getBytes(), "type".getBytes(), CompareFilter.CompareOp.EQUAL, "1".getBytes()); // 过滤器是对结果排序, 所以顺序影响效率 filterList.addFilter(prefixFilter); filterList.addFilter(singleColumnValueFilter); // 创建查询对象 Scan scan = new Scan(); scan.setFilter(filterList); Table table = connect.getTable(tableName); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { byte[] value = result.getValue("cf1".getBytes(), "type".getBytes()); System.out.println(new String(value, "utf8")); } } 8, 根据单个rowkey查询@Test public void testGet() throws IOException { Table table = connect.getTable(tableName); Get get = new Get("13899126419_201717".getBytes()); Result result = table.get(get); Cell cell = result.getColumnLatestCell("cf1".getBytes(), "address".getBytes()); System.out.println(new String(cell.getValueArray(), "utf8")); }
系列来自尚学堂视频
|
请发表评论