【IT168 技术】本文以HBase 0.90.2为例,介绍如何在Windows系统,Eclipse IDE集成环境下,使用Java语言,进行HBase客户端编程,包含建立表、删除表、插入记录、删除记录、各种方式下的查询操作等。
1. 准备工作
(1)下载后安装jdk包(这里使用的是jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008);
(2)下载eclipse,解压到本地(这里使用的是eclipse-java-helios-SR2-win32);
(3)下载HBase包,解压安装包到本地(这里使用的是hbase-0.90.2)。
2. 搭建开发环境
(1)运行Eclipse,创建一个新的Java工程“HBaseClient”,右键项目根目录,选择 “Properties”->“Java Build Path”->“Library”->“Add External JARs”,将HBase解压后根目录下的hbase-0.90.2.jar、hbase-0.90.2-tests.jar和lib子目录下所有jar 包添加到本工程的Classpath下。
(2)按照步骤1中的操作,将自己所连接的HBase的配置文件hbase-site.xml添加到本工程的Classpath中,如下所示为配置文件的一个示例:
1 <configuration>
2 <property>
3 <name>hbase.rootdir</name>
4 <value>hdfs://hostname:9000/hbase</value>
5 </property>
6 <property>
7 <name>hbase.cluster.distributed</name>
8 <value>true</value>
9 </property>
10 <property>
11 <name>hbase.zookeeper.quorum</name>
12 <value>*.*.*.*, *.*.*.*, *.*.*.*</value>
13 </property>
14 <property skipInDoc="true">
15 <name>hbase.defaults.for.version</name>
16 <value>0.90.2</value>
17 </property>
18 </configuration>
2 <property>
3 <name>hbase.rootdir</name>
4 <value>hdfs://hostname:9000/hbase</value>
5 </property>
6 <property>
7 <name>hbase.cluster.distributed</name>
8 <value>true</value>
9 </property>
10 <property>
11 <name>hbase.zookeeper.quorum</name>
12 <value>*.*.*.*, *.*.*.*, *.*.*.*</value>
13 </property>
14 <property skipInDoc="true">
15 <name>hbase.defaults.for.version</name>
16 <value>0.90.2</value>
17 </property>
18 </configuration>
(3)下面可以在Eclipse环境下进行HBase编程了。
3. HBase基本操作代码示例
(1)初始化配置
1 private static Configuration conf = null;
2 /**
3 * 初始化配置
4 */
5 static {
6 conf = HBaseConfiguration.create();
7 }
2 /**
3 * 初始化配置
4 */
5 static {
6 conf = HBaseConfiguration.create();
7 }
(2)创建表
1 /**
2 * 创建表操作
3 * @throws IOException
4 */
5 public void createTable(String tablename, String[] cfs) throws IOException {
6 HBaseAdmin admin = new HBaseAdmin(conf);
7 if (admin.tableExists(tablename)) {
8 System.out.println("表已经存在!");
9 }
10 else {
11 HTableDescriptor tableDesc = new HTableDescriptor(tablename);
12 for (int i = 0; i < cfs.length; i++) {
13 tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
14 }
15 admin.createTable(tableDesc);
16 System.out.println("表创建成功!");
17 }
18 }
2 * 创建表操作
3 * @throws IOException
4 */
5 public void createTable(String tablename, String[] cfs) throws IOException {
6 HBaseAdmin admin = new HBaseAdmin(conf);
7 if (admin.tableExists(tablename)) {
8 System.out.println("表已经存在!");
9 }
10 else {
11 HTableDescriptor tableDesc = new HTableDescriptor(tablename);
12 for (int i = 0; i < cfs.length; i++) {
13 tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
14 }
15 admin.createTable(tableDesc);
16 System.out.println("表创建成功!");
17 }
18 }
(3)删除表
1 /**
2 * 删除表操作
3 * @param tablename
4 * @throws IOException
5 */
6 public void deleteTable(String tablename) throws IOException {
7 try {
8 HBaseAdmin admin = new HBaseAdmin(conf);
9 admin.disableTable(tablename);
10 admin.deleteTable(tablename);
11 System.out.println("表删除成功!");
12 } catch (MasterNotRunningException e) {
13 e.printStackTrace();
14 } catch (ZooKeeperConnectionException e) {
15 e.printStackTrace();
16 }
17 }
2 * 删除表操作
3 * @param tablename
4 * @throws IOException
5 */
6 public void deleteTable(String tablename) throws IOException {
7 try {
8 HBaseAdmin admin = new HBaseAdmin(conf);
9 admin.disableTable(tablename);
10 admin.deleteTable(tablename);
11 System.out.println("表删除成功!");
12 } catch (MasterNotRunningException e) {
13 e.printStackTrace();
14 } catch (ZooKeeperConnectionException e) {
15 e.printStackTrace();
16 }
17 }
(4)插入一行记录
1 /**
2 * 插入一行记录
3 * @param tablename
4 * @param cfs
5 */
6 public void writeRow(String tablename, String[] cfs) {
7 try {
8 HTable table = new HTable(conf, tablename);
9 Put put = new Put(Bytes.toBytes("rows1"));
10 for (int j = 0; j < cfs.length; j++) {
11 put.add(Bytes.toBytes(cfs[j]),
12 Bytes.toBytes(String.valueOf(1)),
13 Bytes.toBytes("value_1"));
14 table.put(put);
15 }
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19 }
2 * 插入一行记录
3 * @param tablename
4 * @param cfs
5 */
6 public void writeRow(String tablename, String[] cfs) {
7 try {
8 HTable table = new HTable(conf, tablename);
9 Put put = new Put(Bytes.toBytes("rows1"));
10 for (int j = 0; j < cfs.length; j++) {
11 put.add(Bytes.toBytes(cfs[j]),
12 Bytes.toBytes(String.valueOf(1)),
13 Bytes.toBytes("value_1"));
14 table.put(put);
15 }
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19 }
(5)删除一行记录
1 /**
2 * 删除一行记录
3 * @param tablename
4 * @param rowkey
5 * @throws IOException
6 */
7 public void deleteRow(String tablename, String rowkey) throws IOException {
8 HTable table = new HTable(conf, tablename);
9 List list = new ArrayList();
10 Delete d1 = new Delete(rowkey.getBytes());
11 list.add(d1);
12 table.delete(list);
13 System.out.println("删除行成功!");
14 }
2 * 删除一行记录
3 * @param tablename
4 * @param rowkey
5 * @throws IOException
6 */
7 public void deleteRow(String tablename, String rowkey) throws IOException {
8 HTable table = new HTable(conf, tablename);
9 List list = new ArrayList();
10 Delete d1 = new Delete(rowkey.getBytes());
11 list.add(d1);
12 table.delete(list);
13 System.out.println("删除行成功!");
14 }
(6)查找一行记录
1 /**
2 * 查找一行记录
3 * @param tablename
4 * @param rowkey
5 */
6 public static void selectRow(String tablename, String rowKey)
7 throws IOException {
8 HTable table = new HTable(conf, tablename);
9 Get g = new Get(rowKey.getBytes());
10 Result rs = table.get(g);
11 for (KeyValue kv : rs.raw()) {
12 System.out.print(new String(kv.getRow()) + " ");
13 System.out.print(new String(kv.getFamily()) + ":");
14 System.out.print(new String(kv.getQualifier()) + " ");
15 System.out.print(kv.getTimestamp() + " ");
16 System.out.println(new String(kv.getValue()));
17 }
18 }
2 * 查找一行记录
3 * @param tablename
4 * @param rowkey
5 */
6 public static void selectRow(String tablename, String rowKey)
7 throws IOException {
8 HTable table = new HTable(conf, tablename);
9 Get g = new Get(rowKey.getBytes());
10 Result rs = table.get(g);
11 for (KeyValue kv : rs.raw()) {
12 System.out.print(new String(kv.getRow()) + " ");
13 System.out.print(new String(kv.getFamily()) + ":");
14 System.out.print(new String(kv.getQualifier()) + " ");
15 System.out.print(kv.getTimestamp() + " ");
16 System.out.println(new String(kv.getValue()));
17 }
18 }
(7)查询表中所有行
1 /**
2 * 查询表中所有行
3 * @param tablename
4 */
5 public void scaner(String tablename) {
6 try {
7 HTable table = new HTable(conf, tablename);
8 Scan s = new Scan();
9 ResultScanner rs = table.getScanner(s);
10 for (Result r : rs) {
11 KeyValue[] kv = r.raw();
12 for (int i = 0; i < kv.length; i++) {
13 System.out.print(new String(kv[i].getRow()) + " ");
14 System.out.print(new String(kv[i].getFamily()) + ":");
15 System.out.print(new String(kv[i].getQualifier()) + " ");
16 System.out.print(kv[i].getTimestamp() + " ");
17 System.out.println(new String(kv[i].getValue()));
18 }
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 }
23 }
2 * 查询表中所有行
3 * @param tablename
4 */
5 public void scaner(String tablename) {
6 try {
7 HTable table = new HTable(conf, tablename);
8 Scan s = new Scan();
9 ResultScanner rs = table.getScanner(s);
10 for (Result r : rs) {
11 KeyValue[] kv = r.raw();
12 for (int i = 0; i < kv.length; i++) {
13 System.out.print(new String(kv[i].getRow()) + " ");
14 System.out.print(new String(kv[i].getFamily()) + ":");
15 System.out.print(new String(kv[i].getQualifier()) + " ");
16 System.out.print(kv[i].getTimestamp() + " ");
17 System.out.println(new String(kv[i].getValue()));
18 }
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 }
23 }