前言
上文讨论了hdfs以及hbase和hadoop的操作,本篇旨在简单尝试使用hbase。
下载安装
首先下载mirrors.bfsu.edu.cn/apache/hbas… 下载以后打开解压文件,放在我们上次一样的文件夹然后解压打开 首先修改hbase-env.sh
,把java_home 编辑修改进去
然后打开bin文件地址输入指令
./start-hbase.sh
复制代码
这样就是启动成功了。
Hbase主要注意的点只有两个:一个是修改配置文件。第二个是注意启动的hadoop集群的状态,如果看到日志中
master.HMaster: Failed to become active master
java.net.ConnectException: Call From xuejiamengdeMacBook-Pro.local/192.168.2.40 to localhost:9000 failed on connection exception
复制代码
注意自己的hadoop启动状态,重新修复以后再启动就可以了。
在浏览器访问 localhost:16010 可以看到运行情况
使用
首先编写配置,因为我们是本地测试就直接设置 localhost
public static Connection createConnection() throws IOException {
Configuration cfg = HBaseConfiguration.create();
cfg.set("hbase.zookeeper.quorum", "localhost");
return ConnectionFactory.createConnection(cfg);
}
复制代码
然后先编写创建表和命名空间的代码
public static void createTable(Connection connection,String myTableName,String[] colFamily) throws IOException{
Admin admin = connection.getAdmin();
TableName tableName=TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println("table is exist");
}else {
List<ColumnFamilyDescriptor> colFamilyList=new ArrayList<>();
TableDescriptorBuilder tableDesBuilder=TableDescriptorBuilder.newBuilder(tableName);
for(String str:colFamily) {
ColumnFamilyDescriptor colFamilyDes=ColumnFamilyDescriptorBuilder.newBuilder(str.getBytes()).build();
colFamilyList.add(colFamilyDes);
}
TableDescriptor tableDes=tableDesBuilder.setColumnFamilies(colFamilyList).build();
admin.createTable(tableDes);
}
}
public static void createNamespace(Connection connection, String tablespace) throws IOException {
Admin admin = connection.getAdmin();
admin.createNamespace(NamespaceDescriptor.create(tablespace).build());
System.out.println("成功创建表空间 " + tablespace);
}
复制代码
测试一下
@Test
public void create() {
try (Connection connection = HbaseService.createConnection();) {
HbaseService.createNamespace(connection,"xuejm");
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
创建表
public static void createTable(Connection connection,String myTableName,String[] colFamily) throws IOException{
Admin admin = connection.getAdmin();
TableName tableName=TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println("table is exist");
}else {
List<ColumnFamilyDescriptor> colFamilyList=new ArrayList<>();
TableDescriptorBuilder tableDesBuilder=TableDescriptorBuilder.newBuilder(tableName);
for(String str:colFamily) {
ColumnFamilyDescriptor colFamilyDes=ColumnFamilyDescriptorBuilder.newBuilder(str.getBytes()).build();
colFamilyList.add(colFamilyDes);
}
TableDescriptor tableDes=tableDesBuilder.setColumnFamilies(colFamilyList).build();
admin.createTable(tableDes);
}
}
复制代码
插入数据
public static void insert(Connection connection, String tableName, String rowKey, String columnFamily, String column,
String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes());
table.put(put);
}
复制代码
查询数据
public static void scan(Connection connection, String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Result tmp;
while ((tmp = scanner.next()) != null) {
List<Cell> cells = tmp.listCells();
for (Cell cell : cells) {
String rk = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String column = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(rk + "column:" + cf + ":" + column + ",value=" + value);
}
}
}
复制代码
测试一下插入以及查询数据
@Test
public void insert() {
try (Connection connection = HbaseService.createConnection()) {
HbaseService.insert(connection, "xuejm:student", "row1", "name", "", "xue");
HbaseService.insert(connection, "xuejm:student", "row1", "info", "student_id", "1");
HbaseService.insert(connection, "xuejm:student", "row1", "info", "class", "1");
HbaseService.insert(connection, "xuejm:student", "row1", "score", "math", "75");
HbaseService.insert(connection, "xuejm:student", "row2", "name", "", "wang");
HbaseService.insert(connection, "xuejm:student", "row2", "info", "student_id", "2");
HbaseService.insert(connection, "xuejm:student", "row2", "info", "class", "2");
HbaseService.insert(connection, "xuejm:student", "row2", "score", "math", "80");
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
测试一下删除row
@Test
public void deleteRow() {
try (Connection connection = HbaseService.createConnection()){
HbaseService.deleteRow(connection, "xuejm:student", "row1");
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
再度查询数据果然被删除了
这样我们就完成了一个简单的增删改查的hbase的demo。