大数据技术原理与应用实验2——熟悉常用的Hbase操作
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
这里写目录标题
一、实验目的
1理解HBase在Hadoop体系结构中的角色
2熟练使用HBase操作常用的Shell命令
3熟悉HBase操作常用的Java API。
二、实验环境
1Linux操作系统CentOS7.5
2VMware Workstation Pro 15.5
3远程终端工具Xshell7
4Xftp7传输工具
5Hadoop版本3.1.3
6HBase版本2.2.2
7JDK版本1.8
8Java IDEIdea。
三、实验内容
一编程实现以下指定功能并用Hadoop提供的HBase Shell命令完成相同任务
1 列出HBase所有的表的相关信息例如表名
2 在终端打印出指定的表的所有记录数据
3 向已经创建好的表添加和删除指定的列族或列
4 清空指定的表的所有记录数据
5 统计表的行数。
1. 列出HBase所有的表的相关信息例如表名
Shell命令
hbase> list
代码
package com.xusheng.HBase.shiyan1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* 1. 编程实现以下指定功能并用Hadoop提供的HBase Shell命令完成相同任务
*1 列出HBase所有的表的相关信息例如表名、创建时间等
*2 在终端打印出指定的表的所有记录数据
*3 向已经创建好的表添加和删除指定的列族或列
*4 清空指定的表的所有记录数据
*5 统计表的行数。
*/
public class shiyan11 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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();
}
}
//(1)列出HBase所有的表的相关信息例如表名、创建时间等
public static void listTables(String stu) throws IOException {
init();//建立连接
List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
for(TableDescriptor tableDescriptor : tableDescriptors){
TableName tableName = tableDescriptor.getTableName();
System.out.println("Table:" + tableName);
}
close();//关闭连接
}
public static void main(String[] args) throws IOException {
listTables("s1");
}
}
结果
2. 在终端打印出指定的表的所有记录数据
Shell命令
hbase> scan 's1'
代码
package com.xusheng.HBase.shiyan1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 1. 编程实现以下指定功能并用Hadoop提供的HBase Shell命令完成相同任务
*1 列出HBase所有的表的相关信息例如表名、创建时间等
*2 在终端打印出指定的表的所有记录数据
*3 向已经创建好的表添加和删除指定的列族或列
*4 清空指定的表的所有记录数据
*5 统计表的行数。
*/
public class shiyan12 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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();
}
}
//(2)在终端打印出指定的表的所有记录数据
public static void getData(String tableName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);//获取行的遍历器
for (Result result:scanner){
printRecoder(result);
}
close();
}
//打印一条记录的详情
public static void printRecoder(Result result)throws IOException{
for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(Bytes.toString(cell.getRowArray(),cell.getRowOffset(), cell.getRowLength())));
System.out.print("列簇: "+new String( Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getFamilyLength()) ));
System.out.print(" 列: "+new String(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(), cell.getQualifierLength())));
System.out.print(" 值: "+new String(Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength())));
System.out.println("时间戳: "+cell.getTimestamp());
}
}
public static void main(String[] args) throws IOException {
getData("s1");
}
}
结果
3. 向已经创建好的表添加和删除指定的列族或列
Shell命令
先在Shell中创建表s1作为示例表命令如下
hbase> create 's1','score'
然后可以在s1中添加数据命令如下
hbase> put 's1','zhangsan','score:Math','69'
之后可以执行如下命令删除指定的列
hbase> delete 's1','zhangsan','score:Math'
代码
package com.xusheng.HBase.shiyan1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 1. 编程实现以下指定功能并用Hadoop提供的HBase Shell命令完成相同任务
*1 列出HBase所有的表的相关信息例如表名、创建时间等
*2 在终端打印出指定的表的所有记录数据
*3 向已经创建好的表添加和删除指定的列族或列
*4 清空指定的表的所有记录数据
*5 统计表的行数。
*/
public class shiyan13 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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();
}
}
//(3)想已经创建好的表添加和删除指定的列族或列
//向表添加数据
public static void insterRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
close();
}
//删除数据
public static void deleRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
//删除指定列族
delete.addFamily(Bytes.toBytes(colFamily));
//删除指定列
delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);
table.close();
close();
}
public static void main(String[] args) throws IOException {
insterRow("s1","1001","score1","english","33");
deleRow("s1","zhangsan","score","Math");
}
}
结果
4. 清空指定的表的所有记录数据
Shell命令
hbase> truncate 's1'
代码
package com.xusheng.HBase.shiyan1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 1. 编程实现以下指定功能并用Hadoop提供的HBase Shell命令完成相同任务
*1 列出HBase所有的表的相关信息例如表名、创建时间等
*2 在终端打印出指定的表的所有记录数据
*3 向已经创建好的表添加和删除指定的列族或列
*4 清空指定的表的所有记录数据
*5 统计表的行数。
*/
public class shiyan14 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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();
}
}
//(4)清空指定的表的所有记录数据
public static void clearRows(String tableName)throws IOException{
init();
TableName tablename = TableName.valueOf(tableName);
admin.disableTable(tablename);
admin.deleteTable(tablename);
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
admin.createTable(tableDescriptor.build());
close();
}
public static void main(String[] args) throws IOException {
clearRows("tableName");
}
}
5. 统计表的行数
Shell命令
hbase> count 'Student'
代码
package com.xusheng.HBase.shiyan1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 1. 编程实现以下指定功能并用Hadoop提供的HBase Shell命令完成相同任务
*1 列出HBase所有的表的相关信息例如表名、创建时间等
*2 在终端打印出指定的表的所有记录数据
*3 向已经创建好的表添加和删除指定的列族或列
*4 清空指定的表的所有记录数据
*5 统计表的行数。
*/
public class shiyan15 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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();
}
}
//(5)统计表的行数
public static void countRows(String tableName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
int num = 0;
for (Result result = scanner.next();result!=null;result=scanner.next()){
num++;
}
System.out.println("行数:"+ num);
scanner.close();
close();
}
public static void main(String[] args) throws IOException {
countRows("Student");
}
}
结果
6. 整合代码
代码
package com.xusheng.HBase.shiyan;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.ClusterConnection;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Scanner;
public class xu {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static long ts;
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws IOException{
while (true){
System.out.println("\n1、列出所有表的信息;");
System.out.println("2、打印指定表的所有记录数据;");
System.out.println("3、向创建好的表添加或删除指定的列族或列;");
System.out.println("4、清空指定表的所有记录数据;");
System.out.println("5、通过表的行数;");
System.out.println("请输入你的选择");
int no=sc.nextInt();
if (no==1){
First();
}else if(no==2){
System.out.println("输入你要查询的表");
String tablename=sc.next();
Second(tablename);
}else if (no==3){
System.out.println("输入你要操作的表");
String tablename=sc.next();
System.out.println("输入你要操作的行建名");
String rowKey=sc.next();
System.out.println("输入你要操作的列组名");
String colFamily=sc.next();
System.out.println("输入你要操作的列限定符名");
String col=sc.next();
System.out.println("输入你要操作的参数值");
String val=sc.next();
Third(tablename,rowKey,colFamily,col,val);
}else if (no==4){
System.out.println("输入你要操作的表");
String tablename=sc.next();
Fourth(tablename);
System.out.println("成功清空");
}
else if (no==5){
System.out.println("输入你要操作的表");
String tablename=sc.next();
fift(tablename);
}
}
}
public static void init(){
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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 First() throws IOException{
init();
HTableDescriptor hTableDescriptor[]=admin.listTables();
for (HTableDescriptor s:hTableDescriptor ){
System.out.println(s.getNameAsString());
}
close();
}
public static void Second(String tablename) throws IOException{
init();
Table table=connection.getTable(TableName.valueOf(tablename));
Scan scan=new Scan();
ResultScanner res=table.getScanner(scan);
for (Result result:res){
showCell(result);
}
}
public static void Third(String tableName, String row, String column,String c ,String val) throws IOException{
System.out.println("1、添加列;2、删除列");
int no=sc.nextInt();
if (no==1){
insertRow(tableName,row,column,c,val);
}else if (no==2){
deleteRow(tableName,row,column,c);
}
}
public static void Fourth(String tablename) throws IOException{
init();
//HBaseAdmin admin1=new HBaseAdmin(configuration);
//HTableDescriptor ht=admin1.getTableDescriptor(TableName.valueOf(Bytes.toBytes(tablename)));
TableName tableName=TableName.valueOf(tablename);
admin.disableTable(tableName);
admin.deleteTable(tableName);
//admin.createTable(ht);
close();
}
public static void fift(String tablename) throws IOException{
init();
Table table=connection.getTable(TableName.valueOf(tablename));
Scan scan=new Scan();
ResultScanner scanner=table.getScanner(scan);
int n=0;
for (Result result=scanner.next();result!=null;result=scanner.next()){
n++;
}
System.out.println("行数有"+n);
scanner.close();
close();
}
public static void insertRow(String tableName, String row, String column,String c ,String val) throws IOException {
init();
Table table=connection.getTable(TableName.valueOf(tableName));
Put put=new Put(row.getBytes());
put.addColumn(column.getBytes(), c.getBytes(), val.getBytes());
table.put(put);
System.out.println("成功添加");
table.close();
close();
}
public static void deleteRow(String tableName, String row, String column,String c) throws IOException{
init();
Table table=connection.getTable(TableName.valueOf(tableName));
System.out.println("1、删除列族;2、删除列限定符");
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
Delete delete=new Delete(row.getBytes());
if (no==1){
delete.addFamily(Bytes.toBytes(column));
System.out.println("成功删除"+column+"这个列族");
}else if(no==2){
delete.addColumn(Bytes.toBytes(column), Bytes.toBytes(c));
System.out.println("成功删除"+c+"这个列限定符");
}
table.delete(delete);
table.close();
close();
}
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
}
结果
1 列出HBase所有的表的相关信息例如表名
2 在终端打印出指定的表的所有记录数据
3 向已经创建好的表添加和删除指定的列族或列
前后对比
4 清空指定的表的所有记录数据
5 统计表的行数。
二HBase数据库操作
1. 现有以下关系型数据库中的表和数据见表14-3到表14-5要求将其转换为适合于HBase存储的表并插入数据
表14-3 学生表Student
1学生Student表
创建表的HBase Shell命令语句如下
create 'Student','S_No','S_Name','S_Sex','S_Age'
插入数据的HBase Shell命令如下
第一行数据
put 'Student','s001','S_No','2015001'
put 'Student','s001','S_Name','Zhangsan'
put 'Student','s001','S_Sex','male'
put 'Student','s001','S_Age','23'
第二行数据
put 'Student','s002','S_No','2015002'
put 'Student','s002','S_Name','Mary'
put 'Student','s002','S_Sex','female'
put 'Student','s002','S_Age','22'
第三行数据
put 'Student','s003','S_No','2015003'
put 'Student','s003','S_Name','Lisi'
put 'Student','s003','S_Sex','male'
put 'Student','s003','S_Age','24'
表14-4 课程表Course
2课程Course表
创建表的HBase Shell命令语句如下
create 'Course','C_No','C_Name','C_Credit'
插入数据的HBase Shell命令如下
第一行数据
put 'Course','c001','C_No','123001'
put 'Course','c001','C_Name','Math'
put 'Course','c001','C_Credit','2.0'
第二行数据
put 'Course','c002','C_No','123002'
put 'Course','c002','C_Name','Computer'
put 'Course','c002','C_Credit','5.0'
第三行数据
put 'Course','c003','C_No','123003'
put 'Course','c003','C_Name','English'
put 'Course','c003','C_Credit','3.0'
表14-5 选课表SC
3选课表
创建表的HBase Shell命令语句如下
create 'SC','SC_Sno','SC_Cno','SC_Score'
插入数据的HBase Shell命令如下
第一行数据
put 'SC','sc001','SC_Sno','2015001'
put 'SC','sc001','SC_Cno','123001'
put 'SC','sc001','SC_Score','86'
第二行数据
put 'SC','sc002','SC_Sno','2015001'
put 'SC','sc002','SC_Cno','123003'
put 'SC','sc002','SC_Score','69'
第三行数据
put 'SC','sc003','SC_Sno','2015002'
put 'SC','sc003','SC_Cno','123002'
put 'SC','sc003','SC_Score','77'
第四行数据
put 'SC','sc004','SC_Sno','2015002'
put 'SC','sc004','SC_Cno','123003'
put 'SC','sc004','SC_Score','99'
第五行数据
put 'SC','sc005','SC_Sno','2015003'
put 'SC','sc005','SC_Cno','123001'
put 'SC','sc005','SC_Score','98'
第六行数据
put 'SC','sc006','SC_Sno','2015003'
put 'SC','sc006','SC_Cno','123002'
put 'SC','sc006','SC_Score','95'
2. 请编程实现以下功能
1createTable(String tableName, String[] fields)
创建表参数tableName为表的名称字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候先删除原有的表然后再创建新的表。
代码
package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.util.Bytes;
import java.io.IOException;
public class CreateTable {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void createTable(String tableName,String[] fields) throws IOException {
init();
TableName tablename = TableName.valueOf(tableName);
if(admin.tableExists(tablename)){
System.out.println("table is exists!");
admin.disableTable(tablename);
admin.deleteTable(tablename);//删除原来的表
}
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
for(String str : fields){
tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build());
admin.createTable(tableDescriptor.build());
}
close();
}
//建立连接
public static void init() {
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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 main(String[] args) {
String[] fields = {"Score"};
try {
createTable("person", fields);
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果
2addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row用S_Name表示和字符串数组fields指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话用“columnFamily:column”表示。例如同时向“Math”、“Computer Science”、“English”三列添加成绩时字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”}数组values存储这三门课的成绩。
代码
package com.xusheng.HBase.shiyan31;
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 addRecord {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
for (int i = 0; i != fields.length; i++) {
Put put = new Put(row.getBytes());
String[] cols = fields[i].split(":");
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
table.put(put);
}
table.close();
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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 main(String[] args) {
String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};
String[] values = {"99", "80", "100"};
try {
addRecord("tableName", "Score", fields, values);
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果
3scanColumn(String tableName, String column)
浏览表tableName某一列的数据如果某一行记录中该列数据不存在则返回null。要求当参数column为某一列族名称时如果底下有若干个列限定符则要列出每个列限定符代表的列的数据当参数column为某一列具体名称例如“Score:Math”时只需要列出该列的数据。
代码
package com.xusheng.HBase.shiyan31;
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.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class scanColumn {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void scanColumn(String tableName, String column) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes(column));
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); result != null; result = scanner.next()) {
showCell(result);
}
table.close();
close();
}
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp:" + cell.getTimestamp() + " ");
System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
}
}
public static void init() {
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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 main(String[] args) {
try {
scanColumn("tableName", "Score");
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果
4modifyData(String tableName, String row, String column)
修改表tableName行row可以用学生姓名S_Name表示列column指定的单元格的数据。
代码
package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class modifyData {
public static long ts;
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void modifyData(String tableName, String row, String column, String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result r : resultScanner) {
for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
ts = cell.getTimestamp();
}
}
put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
table.put(put);
table.close();
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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 main(String[] args) {
try {
modifyData("tableName", "Score", "Math", "100");
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果
5deleteRow(String tableName, String row)
删除表tableName中row指定的行的记录。
代码
package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class deleteRow {
public static long ts;
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void deleteRow(String tableName, String row) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete=new Delete(row.getBytes());
table.delete(delete);
table.close();
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
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 main(String[] args) {
try {
deleteRow("tableName", "Score");
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果
对比运行前和运行后的结果可以看出删除一行数据成功
四、心得体会
1HB ase 定义
HBase 是一种分布式、可扩展、支持海量数据存储的 NoSQL 数据库。
2HBase 数据模型
逻辑上HBase 的数据模型同关系型数据库很类似数据存储在一张表中有行有列。
但从 HBase 的底层物理存储结构K-V来看HBase 更像是一个 multi-dimensional map。
3HBase 逻辑结构
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |