文章目录
1.需求
主要实践建表、插入数据、删除数据、查询等功能。要求建立一个如下所示的表:
- 表名:$your_name:student
- 空白处自行填写, 姓名学号一律填写真实姓名和学号
2.思路
2.1 建立远程Hbase集群连接 – HbaseInit.java
package com.jike.bigdata;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
/**
* @author: fz
* @create: 2021-08-06 23:45
* Hbase客户端的初始化
**/
public class HbaseInit {
public static Connection createConnection() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "jikehadoop01,jikehadoop02,jikehadoop03");
conf.set("hbase.zookeeper.property.clientPort", "2181");
return ConnectionFactory.createConnection(conf);
}
}
2.2 创建表结构–CreateTable.java
package com.jike.bigdata;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import java.io.IOException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author: fz
* @create: 2021-08-06 23:49
* 创建表
**/
public class CreateTable {
public static void create(Connection connection, String tableName, String... columnFamilies) throws IOException {
Admin admin = connection.getAdmin();
if (tableName == null || columnFamilies == null) {
return;
}
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
for (int i = 0; i < columnFamilies.length; i++) {
if (columnFamilies[i] == null)
continue;
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamilies[i]);
columnDescriptor.setMaxVersions(1);
table.addFamily(columnDescriptor);
}
admin.createTable(table);
System.out.println("成功创建表 " + table + ", column family: " + Arrays.toString(columnFamilies));
}
}
//创建表空间 TableNameSpace:TableName--> xiaoyuer:student
public class CreateNameSpace {
public static void createNamespace(Connection connection, String tablespace) throws IOException {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
admin.createNamespace(NamespaceDescriptor.create(tablespace).build());
System.out.println("成功创建表空间 " + tablespace);
}
}
2.3 插入数据 – PutRow.java
package com.jike.bigdata;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
/**
* @author: fz
* @create: 2021-08-06 23:58
* 插入行数据
**/
public class PutRow {
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);
}
}
2.4 查询浏览数据–ScanTable.java
package com.jike.bigdata;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* @author: fz
* @create: 2021-08-06 00:09
* 打印表所有数据
**/
public class ScanTable {
public static void scan(Connection connection, String tableName) throws IOException {
//Admin admin = connection.getAdmin();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Result tmp;
System.out.println("Row\t\t\tColumn\tvalue");
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 + "\t\tcolumn:" + cf + ":" + column + ",value=" + value);
}
}
}
}
2.5 删除数据–DeleteRow.java
package com.jike.bigdata;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* @author: fz
* @create: 2021-08-06 00:34
**/
public class DeleteRow {
public static boolean deleteRow(Connection connection, String tableName, String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
table.delete(delete);
System.out.println("成功删除 " + tableName + " " + rowkey);
return true;
}
}
2.6 删除表–DropTable.java
package com.jike.bigdata;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import java.io.IOException;
/**
* @author: fz
* @create: 2021-08-06 00:11
**/
public class DropTable {
public static void drop(Connection connection, String tableName) throws IOException {
Admin admin = connection.getAdmin();
if (admin.isTableDisabled(TableName.valueOf(tableName))) {
System.out.println("table不存在");
return;
}
if (!admin.isTableDisabled(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
}
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("成功删除表 " + tableName);
}
}
3.测试并查看执行结果
3.1 创建表 xiaoyuer:student
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
import java.io.IOException;
public class CreateNameSpaceTest {
@Test
public void create() {
try (Connection connection = HbaseInit.createConnection()) {
CreateNameSpace.createNamespace(connection,"xiaoyuer");
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
import java.io.IOException;
public class CreateTableTest {
@Test
public void create() {
try (Connection connection = HbaseInit.createConnection()) {
CreateTable.create(connection,"xiaoyuer:student", "name", "info", "score");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2 插入数据
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
import java.io.IOException;
public class PutRowTest {
@Test
public void insert() {
try (Connection connection = HbaseInit.createConnection()) {
PutRow.insert(connection, "xiaoyuer:student", "row1", "name", "", "Tom");
PutRow.insert(connection, "xiaoyuer:student", "row1", "info", "student_id", "20210000000001");
PutRow.insert(connection, "xiaoyuer:student", "row1", "info", "class", "1");
PutRow.insert(connection, "xiaoyuer:student", "row1", "score", "understanding", "75");
PutRow.insert(connection, "xiaoyuer:student", "row1", "score", "programming", "82");
PutRow.insert(connection, "xiaoyuer:student", "row2", "name", "", "Jerry");
PutRow.insert(connection, "xiaoyuer:student", "row2", "info", "student_id", "20210000000002");
PutRow.insert(connection, "xiaoyuer:student", "row2", "info", "class", "1");
PutRow.insert(connection, "xiaoyuer:student", "row2", "score", "understanding", "85");
PutRow.insert(connection, "xiaoyuer:student", "row2", "score", "programming", "67");
PutRow.insert(connection, "xiaoyuer:student", "row3", "name", "", "Jack");
PutRow.insert(connection, "xiaoyuer:student", "row3", "info", "student_id", "20210000000003");
PutRow.insert(connection, "xiaoyuer:student", "row3", "info", "class", "2");
PutRow.insert(connection, "xiaoyuer:student", "row3", "score", "understanding", "80");
PutRow.insert(connection, "xiaoyuer:student", "row3", "score", "programming", "80");
PutRow.insert(connection, "xiaoyuer:student", "row4", "name", "", "Rose");
PutRow.insert(connection, "xiaoyuer:student", "row4", "info", "student_id", "20210000000004");
PutRow.insert(connection, "xiaoyuer:student", "row4", "info", "class", "2");
PutRow.insert(connection, "xiaoyuer:student", "row4", "score", "understanding", "60");
PutRow.insert(connection, "xiaoyuer:student", "row4", "score", "programming", "61");
PutRow.insert(connection, "xiaoyuer:student", "row5", "name", "", "FuZhuang");
PutRow.insert(connection, "xiaoyuer:student", "row5", "info", "student_id", "G20210735010249");
PutRow.insert(connection, "xiaoyuer:student", "row5", "info", "class", "3");
PutRow.insert(connection, "xiaoyuer:student", "row5", "score", "understanding", "70");
PutRow.insert(connection, "xiaoyuer:student", "row5", "score", "programming", "60");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.3 浏览数据
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
public class ScanTableTest {
@Test
public void scan() {
try (Connection connection = HbaseInit.createConnection()){
ScanTable.scan(connection,"xiaoyuer:student");
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行结果
3.4 删除某一行数据
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
import java.io.IOException;
public class DeleteRowTest {
@Test
public void deleteRow() {
try (Connection connection = HbaseInit.createConnection()){
DeleteRow.deleteRow(connection, "xiaoyuer:student", "row1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.5 获取某一行数据
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
public class GetRowTest {
@Test
public void printRowValue() {
try (Connection connection = HbaseInit.createConnection()) {
GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "name");
GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "info", "class");
GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "info", "student_id");
GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "score", "programming");
GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "score", "understanding");
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.6 删除表 xiaoyuer:student
package com.jike.bigdata;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
import java.io.IOException;
public class DropTableTest {
@Test
public void drop() {
try (Connection connection = HbaseInit.createConnection()) {
DropTable.drop(connection, "xiaoyuer:student");
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.参考资料
1.https://github.com/DeanVincent/boldness
2.http://c.biancheng.net/view/3599.html