网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
3.6 查询历史记录
- 先创建一个
teacher
表
create 'teacher',{NAME=>'username',VERSIONS=>5}
- 不断put数据
查询时,默认情况下回显示当前最新版本的数据,如果要查询历史数据,需要指定查询的历史版本数,由于上面设置了保存版本数为5,所以,在查询时制定的历史版本数的有效取值为1到5,具体命令如下:
get 'teacher','91001', {COLUMN=>'username',VERSIONS=>3}
下面是查询版本号为3的
3.7 退出HBase数据库
exit
4. HBase编程实践
- 在IDEA创建项目
- 为项目添加需要用到的JAR包
JAR包位于HBase安装目录下
如位于:/usr/local/hbase/lib目录下,单击界面中的Libraries选项卡,再单击界面右侧的Add External JARs按钮,选中/usr/local/hbase/lib目录下的所有JAR包,点击OK,继续添加JAR包,选中client-facing-thirdparty下的所有JAR文件,点击OK。
- 编写Java应用程序
如果程序里面示例网址“hdfs://localhost:9000/hbase”,运行时出错,可以把” localhost ”改成” master ”。 - 编译运行
4.1 编程题 API文档
4.1.1 第一题
利用4中的程序,创建上表:表scores的概念视图如上图所示
- 用学生的名字name作为行键(rowKey)
- 年级grade是一个只有一个列的列族
- score是一个列族。
- 每一门课程都是score的一个列,如English、math、Chinese等。score 的列可以随时添加。
添加如下数据:
Name | Grade | English | Math | Chinese |
---|---|---|---|---|
Leelei | 6 | 78 | 88 | 90 |
Dandan | 6 | 95 | 100 | 92 |
Sansan | 6 | 67 | 99 | 60 |
Zhanshan | 6 | 66 | 66 | 66 |
- 创建表
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.\*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.HBaseConfiguration;
import java.io.IOException;
public class Create {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void CreateTable(String tableName) throws IOException {
if (admin.tableExists(TableName.valueOf(tableName))) {
System.out.println("Table Exists!!!");
}
else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor("grade"));
tableDesc.addFamily(new HColumnDescriptor("score"));
tableDesc.addFamily(new HColumnDescriptor("score.english"));
tableDesc.addFamily(new HColumnDescriptor("score.math"));
tableDesc.addFamily(new HColumnDescriptor("score.chinese"));
admin.createTable(tableDesc);
System.out.println("Create Table Successfully .");
}
}
public static void main(String[] args) {
String tableName = "scores\_zqc";
try {
init();
CreateTable(tableName);
close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 插入数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.\*;
import java.io.IOException;
public class Insert {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void InsertRow(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
System.out.println("Insert Data Successfully");
table.put(put);
table.close();
}
public static void main(String[] args) {
String tableName = "scores\_zqc";
String[] RowKeys = {
"dandan",
"sansan",
};
String[] Grades = {
"6",
"6",
};
String[] English = {
"95",
"87"
};
String[] Math = {
"100",
"95",
};
String[] Chinese = {
"92",
"98",
};
try {
init();
int i = 0;
while (i < RowKeys.length){
InsertRow(tableName, RowKeys[i], "grade", "", Grades[i]);
InsertRow(tableName, RowKeys[i], "score", "english", English[i]);
InsertRow(tableName, RowKeys[i], "score", "math", Math[i]);
InsertRow(tableName, RowKeys[i], "score", "chinese", Chinese[i]);
i++;
} //031904102 zqc
close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- hbase shell中查看数据
4.1.2 第二题
创建并插入相关数据后,查看Hbase java api 文档
在ExampleForHBase 类中添加两个个函数分别实现一个rowKey 过滤器(RowFilter)以及一个单列值过滤器(SingleColumValueFilter);
之后通过这两个函数分别做如下查询:
- 插入成功
- 查询Zhangshan 的年级以及相关成绩,打印在控制台中并截图。
- 查询数学成绩低于100的所有人的名字,打印在控制台中并截图。
- 插入数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.\*;
import java.io.IOException;
public class InsertTwo {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void InsertRow(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
System.out.println("Insert Data Successfully");
table.put(put);
table.close();
}
public static void main(String[] args) {
String tableName = "scores\_zqc\_two";
String[] RowKeys = {
"Leelei",
"Dandan",
"Sansan",
"Zhanshan",
};
String[] Grades = {
"6",
"6",
"6",
"6",
};
String[] English = {
"78",
"95",
"67",
"66",
};
String[] Math = {
"78",
"95",
"100",
"66",
};
String[] Chinese = {
"90",
"92",
"60",
"66",
};
try {
init();
int i = 0;
while (i < RowKeys.length){
InsertRow(tableName, RowKeys[i], "grade", "", Grades[i]);
InsertRow(tableName, RowKeys[i], "score", "english", English[i]);
InsertRow(tableName, RowKeys[i], "score", "math", Math[i]);
InsertRow(tableName, RowKeys[i], "score", "chinese", Chinese[i]);
i++;
}
close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 查询
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
public class FindTwo {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://192.168.0.108:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if(admin !=null) {
admin.close();
}
if(connection !=null) {
connection.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
public static void findRowFilter(String tablename,String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tablename));
Scan scan = new Scan();
System.out.println("RowKey: "+rowkey);
Filter filter = new RowFilter(CompareOp.EQUAL,new SubstringComparator(rowkey));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result res : scanner) {
List<Cell> cells = res.listCells();
for (Cell cell : cells) {
// 打印rowkey,family,qualifier,value
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
+ ": " + Bytes.toString(CellUtil.cloneFamily(cell))
+ ": "+Bytes.toString(CellUtil.cloneQualifier(cell))
+": "+ Bytes.toString(CellUtil.cloneValue(cell)) );
}
}
scanner.close();
}
public static void findSingleColumValueFilter(String tablename,String colFamily,String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tablename));
Scan scan = new Scan();
System.out.println("\nthe value of math in colFamily < 100:");
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(colFamily),
Bytes.toBytes(col),
CompareOp.GREATER,
Bytes.toBytes("100"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result res : scanner) {
System.out.println("name: "+Bytes.toString(res.getRow()));
}
scanner.close();
}
public static void main(String[] args) throws IOException {
init();
findRowFilter("scores\_zqc\_two","Zhanshan");
findSingleColumValueFilter("scores\_zqc\_two","score","math");
![img](https://img-blog.csdnimg.cn/img_convert/e89e1d59160868fb8cb77338a52ef13c.png)
![img](https://img-blog.csdnimg.cn/img_convert/baf52b46c4415920c2f9128c3408aaa3.png)
![img](https://img-blog.csdnimg.cn/img_convert/0e60f5a8b39f8d02285f7a26111e76db.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
s IOException {
init();
findRowFilter("scores\_zqc\_two","Zhanshan");
findSingleColumValueFilter("scores\_zqc\_two","score","math");
[外链图片转存中...(img-X5AE72UT-1715001317080)]
[外链图片转存中...(img-x9zKjJXx-1715001317080)]
[外链图片转存中...(img-itmPiOwd-1715001317080)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**