Java访问HBase
实验任务
编写Java程序代码,实现对hbase的访问,要求如下:
1. 在Eclipse中创建Java Project,并进行开发环境的配置。
2. 创建student表,表结构包含info和course列族。使用hbase shell命令验证。
3. 删除表。使用hbase shell命令验证。
4. 修改表结构,增加(删除)列族。使用hbase shell命令验证。
5. 输入数据,要求至少包括以下列
info列族:name、age、sex、dept
course列族:english、math、physics
6. 使用get进行数据查询。
实验环境
(章鱼大数据)HBase、Eclipse
实验步骤
1.首先检查Hadoop相关进程,是否已经启动。若未启动,切换到/apps/hadoop/sbin目录下,启动Hadoop。
当Hadoop相关进程启动后,进入HBase的bin目录下,启动HBase服务。
2.切换到/data/hbase2目录下,如不存在需提前创建hbase2文件夹。
3.使用wget命令,下载http://192.168.1.100:60000/allfiles/hbase2中的文件。
4.解压/data/hbase2中的hbasedemolib.tar.gz包到/data/hbase2中。
5.打开Eclipse,创建java项目,名为hbasedemo。
在hbasedemo项目下,创建包,包名为myhbase。
添加项目依赖的jar包,右击hbasedemo,选择import。
进入下面界面,选择General中的File System,点击Next。
进入以下界面,选择/data/hbase2中的hbasedemolib文件夹,并勾选Create top-level folder,点击Finish。
然后,选中hbasedemolib里面的所有文件,单击右键Build Path=>Add to Build Path选项,就将所有jar包加载到项目里面了。
6.创建表的API
创建类,名为CreateMyTable,功能为在HBase中创建名为student,列族为info和course的表。
package myhbase;
import java.io.IOException;
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.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class CreateMyTable {
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
String tableName = "student";
String columnFamily1 = "info";
String columnFamily2 = "course";
//调用create方法创建表
create(tableName, columnFamily1,columnFamily2);
}
//建立连接方法
public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
// 设置HBase数据在HDFS上的根目录
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
// 设置配置,zookeeper的节点列表
conf.set("hbase.zookeeper.quorum", "localhost");
// 返回实例
return conf;
}
//创建表方法
public static void create(String tableName, String columnFamily1, String columnFamily2) throws MasterNotRunningException, ZooKeeperConnectionException,IOException { // 返回根据配置初始化的HBaseAdmin实例
HBaseAdmin hBaseAdmin = new HBaseAdmin(getConfiguration());
If (hBaseAdmin.tableExists(tableName)) {
System.err.println("Table exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily1));
tableDesc.addFamily(new HColumnDescriptor(columnFamily2));
hBaseAdmin.createTable(tableDesc);
System.err.println("Create Table SUCCESS!");
}
}
}
在Eclipse中执行程序代码,在CreateMyTable类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。
然后查看HBase中新创建的student表,先启动hbase shell命令行模式。
执行list,列出当前HBase中的表。
创建类,命名为DeleteMyTable,功能为将HBase中表student删除。
package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class DeleteMyTable {
public static void main(String[] args) throws IOException {
String tableName = "student";
//调用delete方法删除表
delete(tableName);
}
//建立连接方法
public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}
public static void delete(String tableName) throws IOException {
HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());
// tableExists(tableName)方法用于判断是否存在tableName指定表
if(hAdmin.tableExists(tableName)){
try {
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.err.println("Delete table Success");
} catch (IOException e) {
System.err.println("Delete table Failed ");
}
}else{
System.err.println("table not exists");
}
}
}
在DeleteMyTable类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。
在Eclipse中执行完成,然后在hbase中查看结果, 查看mytb表是否被删除。
8. 修改表结构,增加(删除)列族。使用hbase shell命令验证。
package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class AlterData {
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
String tableName = "student";
String columnFamily1 = "info";
put(tableName, "2018001", columnFamily1, "age:","20");
}
//建立连接方法
public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}
//修改表结构
public static void put(String tableName, String row, String columnFamily1, String column, String data) throws IOException {
HTable table = new HTable(getConfiguration(), tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily1),
Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(put);
System.err.println("SUCCESS");
}
}
9. 输入数据,要求至少包括以下列
info列族:name、age、sex、dept
course列族:english、math、physics
package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class PutData {
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
String tableName = "student";
String columnFamily1 = "info";
String columnFamily2 = "course";
//调用put()方法输入数据
put(tableName, "2018001", columnFamily1, "name:","wang");
put(tableName, "2018001", columnFamily1, "age:","19");
put(tableName, "2018001", columnFamily1, "sex:","nan");
put(tableName, "2018001", columnFamily1, "dept:","math");
put(tableName, "2018002", columnFamily1, "name:","li");
put(tableName, "2018002", columnFamily1, "age:","17");
put(tableName, "2018002", columnFamily1, "sex:","nv");
put(tableName, "2018002", columnFamily1, "dept:","english");
put(tableName, "2018001", columnFamily2, "english:","50");
put(tableName, "2018001", columnFamily2, "math:","80");
put(tableName, "2018001", columnFamily2, "physics:","70");
put(tableName, "2018002", columnFamily2, "english:","80");
put(tableName, "2018002", columnFamily2, "math:","65");
put(tableName, "2018002", columnFamily2, "physics:","85");
}
//建立连接方法
public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}
//插入数据列表
public static void put(String tableName, String row, String columnFamily1,String columnFamily2, String column, String data) throws IOException {
HTable table = new HTable(getConfiguration(), tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily),
Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(put);
System.err.println("SUCCESS");
}
}
10. 使用get进行数据查询。
创建类GetData
package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class GetData {
public static void main(String[] args) throws IOException {
String tableName = "student";
get(tableName, "2018001");
}
//建立连接方法
public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}
get()方法查询
public static void get(String tableName, String rowkey) throws IOException {
//根据配置信息和表名,初始化一个新的表的引用
HTable table = new HTable(getConfiguration(), tableName);
//使用指定的行键构建Get实例,将行键转换为字节数组类型数据
Get get = new Get(Bytes.toBytes(rowkey));
//从HBase中获取指定行键的行数据
Result result = table.get(get);
byte[] value1 = result.getValue("info".getBytes(), "name".getBytes());
byte[] value2 = result.getValue("info".getBytes(), "sex".getBytes());
System.err.println("line1:SUCCESS");
System.err.println("line2:" + new String(value1) + " " + new String(value2));
}
}