网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
- 表: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;
}
![img](https://img-blog.csdnimg.cn/img_convert/c3d625f8de8fd4256c0f0d3f41f1579c.png)
![img](https://img-blog.csdnimg.cn/img_convert/50d4e83228a97bb70d264dafc5cb65e7.png)
![img](https://img-blog.csdnimg.cn/img_convert/46f12bdb4341a91e7a9dd449ed3af185.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**