hbase 的基本概念
- 表:HBase采用表来组织数据,表由行和列组成,列划分为若干个列族;
- 行:每个HBase表都由若干行组成,每个行由行键(row key)来标识;
- 列族:一个HBase表被分组成许多“列族”(Column Family)的集合,它是基本的访问控制单元,创建表的时候创建;
- 列限定符:列族里的数据通过列限定符(或列)来定位;
- 单元格:在HBase表中,通过行、列族和列限定符确定一个“单元格”(cell);
- 时间戳:每个单元格都保存着同一份数据的多个版本,这些版本采用时间戳进行索引;
hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://mycluster/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>xiemeng-01,xiemeng-02,xiemeng-03,xiemeng-04</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/xiemeng/software/hadoop-3.2.0/app/apache-zookeeper-3.5.8-bin/data</value>
</property>
<property>
<name>hbase.tmp.dir</name>
<value>/home/xiemeng/software/hbase-2.2.6/tmp</value>
</property>
</configuration>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>${hbase.version}</version>
</dependency>
class StudentBean {
private Integer studentNo;
private String name;
private String studentName;
private Integer studentAge;
}
/**
* HBabse 测试类
*/
public class HBaseTest {
/**
* 获取连接对象
* @return
* @throws IOException
*/
private static Connection connection = null;
/**
* 日志类
*/
private static final Logger LOGGER = LoggerFactory.getLogger(HBaseTest.class);
/**
* 获取连接
* @return
* @throws IOException
*/
public static Connection getConnection() throws IOException {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort","2181");
configuration.set("hbase.zookeeper.quorum","192.168.64.128,192.168.64.130,192.168.64.131,192.168.64.133");
configuration.set("hbase.master","192.168.64.128:16010");
synchronized(HBaseTest.class){
if(null==connection){
connection = ConnectionFactory.createConnection(configuration);
}
}
return connection;
}
/**
* 创建表
* @param tableName
* @param cols
* @throws IOException
*/
public static void createTable(String tableName,String [] cols) throws IOException {
TableName name = TableName.valueOf(tableName);
Admin admin = getConnection().getAdmin();
if(admin.tableExists(name)){
System.out.println("表已经存在");
}else {
TableDescriptorBuilder.ModifyableTableDescriptor descriptor = (TableDescriptorBuilder.ModifyableTableDescriptor) TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)).build();
for(String col: cols){
ColumnFamilyDescriptor columnDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(col.getBytes()).build();
descriptor.setColumnFamily(columnDescriptor);
}
admin.createTable(descriptor);
}
}
/**
* 插入数据
* @param tableName
* @param student
* @throws IOException
*/
public static void insert(String tableName, StudentBean student) throws IOException {
Table table = getConnection().getTable(TableName.valueOf(tableName));
Put put = new Put(("stu"+student.getStudentNo()).getBytes());
put.addColumn("stu_name".getBytes(),"student_name".getBytes(),student.getStudentName().getBytes());
put.addColumn("stu_name".getBytes(),"name".getBytes(),student.getStudentName().getBytes());
put.addColumn("stu_age".getBytes(),"student_age".getBytes(),(""+student.getStudentAge()).getBytes());
table.put(put);
}
public static List<String> getColumnFamilyNames(String tableName) throws IOException {
Table table = getConnection().getTable(TableName.valueOf(tableName));
TableDescriptor tableDescriptor = table.getDescriptor();
List<String> columnFamilyNames = new ArrayList<>();
for(ColumnFamilyDescriptor columnFamilyDescriptor :tableDescriptor.getColumnFamilies()){
columnFamilyNames.add(columnFamilyDescriptor.getNameAsString());
}
return columnFamilyNames;
}
public static Set<String> getFamilyNames(String tableName) throws IOException {
Table table = getConnection().getTable(TableName.valueOf(tableName));
TableDescriptor descriptor = table.getDescriptor();
Set<byte[]> familySet = descriptor.getColumnFamilyNames();
Set<String> familyNameSet = familySet.stream().map(byteValue -> Bytes.toString(byteValue)).collect(Collectors.toSet());
return familyNameSet;
}
public static List<StudentBean> getAllData(String tableName) throws IOException {
Table descriptor = getConnection().getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setStartRow("stu1".getBytes());
scan.setStopRow("stu1000".getBytes());
scan.addColumn("stu_name".getBytes(),"student_name".getBytes());
ResultScanner results = descriptor.getScanner(scan);
List<StudentBean> studentBeans = new ArrayList<>();
for(Result result: results){
if(!result.isEmpty()){
StudentBean studentBean = new StudentBean();
String rowKey = new String(result.getRow());
int studentNo = Integer.parseInt(rowKey.substring(3));
studentBean.setStudentNo(studentNo);
for(Cell cell: result.rawCells()){
String name = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
if(name.equals("student_name")){
studentBean.setStudentName(value);
}
if(name.equals("student_age")){
studentBean.setStudentAge(Integer.parseInt(value));
}
if(name.equals("name")){
studentBean.setName(value);
}
}
studentBeans.add(studentBean);
}
}
return studentBeans;
}
public static StudentBean getRowByRowKey(String tableName,Integer rowKey) throws IOException {
StudentBean studentBean = new StudentBean();
studentBean.setStudentNo(rowKey);
Get get = new Get(("stu"+rowKey).getBytes());
Table table = getConnection().getTable(TableName.valueOf(tableName));
if(!get.isCheckExistenceOnly()){
Result result = table.get(get);
for(Cell cell: result.rawCells()){
String name = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
if(name.equals("student_name")){
studentBean.setStudentName(value);
}
if(name.equals("student_age")){
studentBean.setStudentAge(Integer.parseInt(value));
}
}
}
return studentBean;
}
public static void updateTable(String tableName) throws IOException {
HTable table = (HTable)getConnection().getTable(TableName.valueOf(tableName));
Put put = new Put("stu-1".getBytes());
put.addColumn("stu_name".getBytes(),"name".getBytes(),UUID.randomUUID().toString().getBytes());
table.put(put);
table.clearRegionCache();
}
public void deteleTable(String tableName) throws IOException {
Table table = getConnection().getTable(TableName.valueOf(tableName));
}
public static void main(String[] args) throws IOException {
String [] cols = new String [] {"stu_no","stu_name","stu_age"};
//createTable("student",cols);
long begin = System.currentTimeMillis();
/*for(int i=1;i<100;i++){
StudentBean student = new StudentBean(i, UUID.randomUUID().toString(), RandomUtils.nextInt());
putData("student",student);
}*/
long end = System.currentTimeMillis();
System.out.println(String.format("插入数据时长%d",(end-begin)));
//StudentBean studentBean = getRowByRowKey("student",1);
//LOGGER.info(studentBean.toString());
updateTable("student");
List<StudentBean> studentBeans = getAllData("student");
//studentBeans.stream().forEach(System.out::println);
//createTable("student2",cols);
//getColumnFamilyNames("student").forEach(System.out::println);
//System.out.println("获取所有列簇==========================================");
//getFamilyNames("student").forEach(System.out::println);
}
}
hbase 的表设计原则
微信朋友圈实现