hbase1.1.1客户端使用入门
jopen
9年前
工作中用到hbase,作为一个程序员首先就是要知道怎么使用,快速入门:
-
引入maven依赖:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.1.1</version> </dependency>
2. hbase配置初始化时候会检查classpath路径下的hbase-default.xml、hbase-site.xml,hbase-default.xml在hbase-common包中,我们在classpath路径下加入文件hbase-site.xml,该文件最精简配置:
<property> <name>hbase.zookeeper.property.clientPort</name> <value>2181</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>127.0.0.1</value> </property>
hbase.zookeeper.quorum: zookeeper集群地址,多个用逗号隔开
hbase.zookeeper.property.clientPort: zookeeper端口
3.代码中创建连接后就可以进行建表及crud操作:
private static ThreadLocal<Map<String, Table>> threadLocal = new ThreadLocal<Map<String, Table>>(); // 初始化配置 static { conf = HBaseConfiguration.create(); try { connection = ConnectionFactory.createConnection(conf); } catch (IOException e) { logger.error("", e); } } //获取表 public static Table getTable(String tableName) throws IOException { Map<String, Table> tables = threadLocal.get(); if(tables == null) { tables = new HashMap<String, Table>(2); threadLocal.set(tables); } Table table = null; if(!tables.containsKey(tableName)) { table = connection.getTable(TableName.valueOf(tableName)); tables.put(tableName, table); } else { table = tables.get(tableName); } return table; } //创建表 public static void createTable(String tableName) { HBaseAdmin hBaseAdmin = null; try { hBaseAdmin = (HBaseAdmin) connection.getAdmin(); if (hBaseAdmin.tableExists(tableName)) { System.out.println(tableName + " is exist...."); return ; } HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); tableDescriptor.addFamily(new HColumnDescriptor(Constants.FAMILY1)); hBaseAdmin.createTable(tableDescriptor); } catch (Exception e) { logger.error("", e); } finally { if(hBaseAdmin != null) try { hBaseAdmin.close(); } catch (IOException e) { logger.error("", e); } } } // 添加多条数据 public static void addBatch(String tableName, List<Put> puts) { Table table = null; try { table = getTable(tableName); table.put(puts); } catch (Exception e) { logger.error("", e); } } // 添加一条数据 public static void addRow(String tableName, Put put) { Table table = null; try { table = getTable(tableName); table.put(put); } catch (Exception e) { logger.error("", e); } } // 删除多条数据 public static void delMultiRows(String tableName, String[] rows) { Table table = null; try { table = getTable(tableName); List<Delete> delList = new ArrayList<Delete>(); for (String row : rows) { Delete del = new Delete(Bytes.toBytes(row)); delList.add(del); } table.delete(delList); } catch (Exception e) { logger.error("", e); } } // 删除一条数据 public static void delSingleRow(String tableName, String row) { Table table = null; try { table = getTable(tableName); Delete del = new Delete(Bytes.toBytes(row)); table.delete(del); } catch (Exception e) { logger.error("", e); } } // 获取一条数据 public static Result getRow(String tableName, String row) { Table table = null; try { table = getTable(tableName); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); return result; } catch (Exception e) { logger.error("", e); return null; } } // 条件查询 public static ResultScanner scanTable(String tableName, Scan scan) { Table table = null; try { table = getTable(tableName); ResultScanner results = table.getScanner(scan); return results; } catch (Exception e) { logger.error("", e); return null; } }
4. 需要在部署机器上添加hbase对应的hosts