nosql实验四HBaseShell API操作.docx
- 文档编号:12672438
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:23
- 大小:721.66KB
nosql实验四HBaseShell API操作.docx
《nosql实验四HBaseShell API操作.docx》由会员分享,可在线阅读,更多相关《nosql实验四HBaseShell API操作.docx(23页珍藏版)》请在冰豆网上搜索。
nosql实验四HBaseShellAPI操作
实验四、HBaseShellAPI操作
一、创建工程
1.启动hadoop,启动hbase
2.新建JavaProject——>新建Class
3.在工程中导入外部jar包:
二、编写基本API
//建立连接
publicstaticvoidinit(){
configuration=HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs:
//localhost:
9000/hbase");
try{
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
}catch(IOExceptione){
e.printStackTrace();
}
}
//关闭连接
publicstaticvoidclose(){
try{
if(admin!
=null){
admin.close();
}
if(null!
=connection){
connection.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
(1)建表
/**
*建表。
HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
*@parammyTableName表名
*@paramcolFamily列族名
*@throwsIOException
*/
publicstaticvoidcreateTable(StringmyTableName,String[]colFamily)throwsIOException{
init();
TableNametableName=TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("talbeisexists!
");
}else{
HTableDescriptorhTableDescriptor=newHTableDescriptor(tableName);
for(Stringstr:
colFamily){
HColumnDescriptorhColumnDescriptor=newHColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("createtablesuccess");
}
close();
}
测试
运行
(2)查看所有表
/**
*查看已有表
*@throwsIOException
*/
publicstaticvoidlistTables()throwsIOException{
init();
HTableDescriptorhTableDescriptors[]=admin.listTables();
for(HTableDescriptorhTableDescriptor:
hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
测试
运行
(3)插入数据
/**
*向某一行的某一列插入数据
*@paramtableName表名
*@paramrowKey行键
*@paramcolFamily列族名
*@paramcol列名(如果其列族下没有子列,此参数可为空)
*@paramval值
*@throwsIOException
*/
publicstaticvoidinsertRow(StringtableName,StringrowKey,StringcolFamily,Stringcol,Stringval)throwsIOException{
init();
Tabletable=connection.getTable(TableName.valueOf(tableName));
Putput=newPut(rowKey.getBytes());
put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());
table.put(put);
table.close();
close();
}
测试
运行
(4)删除数据
/**
*删除数据
*@paramtableName表名
*@paramrowKey行键
*@paramcolFamily列族名
*@paramcol列名
*@throwsIOException
*/
publicstaticvoiddeleteRow(StringtableName,StringrowKey,StringcolFamily,Stringcol)throwsIOException{
init();
Tabletable=connection.getTable(TableName.valueOf(tableName));
Deletedelete=newDelete(rowKey.getBytes());
//删除指定列族的所有数据
//delete.addFamily(colFamily.getBytes());
//删除指定列的数据
//delete.addColumn(colFamily.getBytes(),col.getBytes());
table.delete(delete);
table.close();
close();
}
删除前
删除指定列数据
测试
运行
删除指定列族数据
测试
运行
删除指定行数据
测试
运行
(5)获取数据
/**
*根据行键rowkey查找数据
*@paramtableName表名
*@paramrowKey行键
*@paramcolFamily列族名
*@paramcol列名
*@throwsIOException
*/
publicstaticvoidgetData(StringtableName,StringrowKey,StringcolFamily,Stringcol)throwsIOException{
init();
Tabletable=connection.getTable(TableName.valueOf(tableName));
Getget=newGet(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Resultresult=table.get(get);
showCell(result);
table.close();
close();
}
测试
运行
(6)删除表
/**
*删除指定表
*@paramtableName表名
*@throwsIOException
*/
publicstaticvoiddeleteTable(StringtableName)throwsIOException{
init();
TableNametn=TableName.valueOf(tableName);
if(admin.tableExists(tn)){
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
测试
运行
/**
*格式化输出
*@paramresult
*/
publicstaticvoidshowCell(Resultresult){
Cell[]cells=result.rawCells();
for(Cellcell:
cells){
System.out.println("RowName:
"+newString(CellUtil.cloneRow(cell))+"");
System.out.println("Timetamp:
"+cell.getTimestamp()+"");
System.out.println("columnFamily:
"+newString(CellUtil.cloneFamily(cell))+"");
System.out.println("rowName:
"+newString(CellUtil.cloneQualifier(cell))+"");
System.out.println("value:
"+newString(CellUtil.cloneValue(cell))+"");
}
}
}
三、完整代码
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.*;
importorg.apache.hadoop.hbase.client.*;
importjava.io.IOException;
publicclassExampleForHbase{
publicstaticConfigurationconfiguration;
publicstaticConnectionconnection;
publicstaticAdminadmin;
//主函数中的语句请逐句执行,只需删除其前的//即可,如:
执行insertRow时请将其他语句注释
publicstaticvoidmain(String[]args)throwsIOException{
//创建一个表,表名为Score,列族为sname,course
createTable("Score",newString[]{"sname","course"});
//在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
//等价命令:
put'Score','95001','sname','Mary'
//insertRow("Score","95001","sname","","Mary");
//在Score表中插入一条数据,其行键为95001,course:
Math为88(course为列族,Math为course下的子列)
//等价命令:
put'Score','95001','score:
Math','88'
//insertRow("Score","95001","course","Math","88");
//在Score表中插入一条数据,其行键为95001,course:
English为85(course为列族,English为course下的子列)
//等价命令:
put'Score','95001','score:
English','85'
//insertRow("Score","95001","course","English","85");
//1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math
//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释
//等价命令:
delete'Score','95001','score:
Math'
//deleteRow("Score","95001","course","Math");
//2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)
//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释
//等价命令:
delete'Score','95001','score'
//deleteRow("Score","95001","course","");
//3、删除Score表中指定行数据,其行键为95001
//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释
//等价命令:
deleteall'Score','95001'
//deleteRow("Score","95001","","");
//查询Score表中,行键为95001,列族为course,列为Math的值
//getData("Score","95001","course","Math");
//查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
//getData("Score","95001","sname","");
//删除Score表
//deleteTable("Score");
}
//建立连接
publicstaticvoidinit(){
configuration=HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs:
//localhost:
9000/hbase");
try{
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
}catch(IOExceptione){
e.printStackTrace();
}
}
//关闭连接
publicstaticvoidclose(){
try{
if(admin!
=null){
admin.close();
}
if(null!
=connection){
connection.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*建表。
HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
*@parammyTableName表名
*@paramcolFamily列族名
*@throwsIOException
*/
publicstaticvoidcreateTable(StringmyTableName,String[]colFamily)throwsIOException{
init();
TableNametableName=TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("talbeisexists!
");
}else{
HTableDescriptorhTableDescriptor=newHTableDescriptor(tableName);
for(Stringstr:
colFamily){
HColumnDescriptorhColumnDescriptor=newHColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("createtablesuccess");
}
close();
}
/**
*删除指定表
*@paramtableName表名
*@throwsIOException
*/
publicstaticvoiddeleteTable(StringtableName)throwsIOException{
init();
TableNametn=TableName.valueOf(tableName);
if(admin.tableExists(tn)){
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
/**
*查看已有表
*@throwsIOException
*/
publicstaticvoidlistTables()throwsIOException{
init();
HTableDescriptorhTableDescriptors[]=admin.listTables();
for(HTableDescriptorhTableDescriptor:
hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
*向某一行的某一列插入数据
*@paramtableName表名
*@paramrowKey行键
*@paramcolFamily列族名
*@paramcol列名(如果其列族下没有子列,此参数可为空)
*@paramval值
*@throwsIOException
*/
publicstaticvoidinsertRow(StringtableName,StringrowKey,StringcolFamily,Stringcol,Stringval)throwsIOException{
init();
Tabletable=connection.getTable(TableName.valueOf(tableName));
Putput=newPut(rowKey.getBytes());
put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());
table.put(put);
table.close();
close();
}
/**
*删除数据
*@paramtableName表名
*@paramrowKey行键
*@paramcolFamily列族名
*@paramcol列名
*@throwsIOException
*/
publicstaticvoiddeleteRow(StringtableName,StringrowKey,StringcolFamily,Stringcol)throwsIOException{
init();
Tabletable=connection.getTable(TableName.valueOf(tableName));
Deletedelete=newDelete(rowKey.getBytes());
//删除指定列族的所有数据
//delete.addFamily(colFamily.getBytes());
//删除指定列的数据
//delete.addColumn(colFamily.getBytes(),col.getBytes());
table.delete(delete);
table.close();
close();
}
/**
*根据行键rowkey查找数据
*@paramtableName表名
*@paramrowKey行键
*@paramcolFamily列族名
*@paramcol列名
*@throwsIOException
*/
publicstaticvoidgetData(StringtableName,StringrowKey,StringcolFamily,Stringcol)throwsIOException{
init();
Tabletable=connection.getTable(TableName.valueOf(tableName));
Getget=newGet(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Resultresult=table.get(get);
showCell(result);
table.close();
close();
}
/**
*格式化输出
*@paramresult
*/
publicstaticvoidshowCell(Resultresult){
Cell[]cells=result.rawCells();
for(Cellcell:
cells){
System.out.println("RowName:
"+newString(CellUtil.cloneRow(cell))+"");
System.out.println("Timetamp:
"+cell.getTimestamp()+"");
System.out.println("columnFamily:
"+newStrin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- nosql实验四HBaseShell API操作 nosql 实验 HBaseShell API 操作
![提示](https://static.bdocx.com/images/bang_tan.gif)