完整原版實(shí)驗(yàn)報(bào)告word文件:實(shí)驗(yàn)三:熟悉常用的HBase操作
—————————————————————————————————
"大數(shù)據(jù)技術(shù)原理與應(yīng)用"課程實(shí)驗(yàn)報(bào)告
題目:實(shí)驗(yàn)三:熟悉常用的HBase操作
姓名:朱小凡
日期:2022/3/29
1、實(shí)驗(yàn)環(huán)境:
設(shè)備名稱(chēng) LAPTOP-9KJS8HO6
處理器 Intel? Core? i5-10300H CPU @ 2.50GHz 2.50 GHz
機(jī)帶 RAM 16.0 GB (15.8 GB 可用)
主機(jī)操作系統(tǒng) Windows 10 家庭中文版
虛擬機(jī)操作系統(tǒng) ubuntukylin-16.04
Hadoop 版本 3.1.3
JDK 版本 1.8
Java IDE:Eclipse
系統(tǒng)類(lèi)型 64 位操作系統(tǒng), 基于 x64 的處理器
筆和觸控 沒(méi)有可用于此顯示器的筆或觸控輸入
2、實(shí)驗(yàn)內(nèi)容與完成情況:
1.編程實(shí)現(xiàn)以下指定功能,并用 Hadoop提供的 HBase Shell
命令完成相同任務(wù)
(1) 列出 HBase所有的表的相關(guān)信息,例如表名。
a.Shell命令
HBase Shell:List
圖1.列出表名
b.java命令
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
public class test1 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* @param args
* 建立連接
*/
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();
}
}
//關(guān)閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
/*
* 查看已有表
* @throws IOException
*/
public static void listTables() throws IOException{
init();
@SuppressWarnings(“deprecation”)
HTableDescriptor hTableDescriptors [] = admin.listTables();
for(HTableDescriptor hTableDescriptor : hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test1 t = new test1();
try{
System.out.println(“以下為Hbase數(shù)據(jù)庫(kù)中所存的表信息”);
t.listTables();
}catch (IOException e){
e.printStackTrace();
}
}
}
圖2.列出表名(java)
(2)在終端打印出指定的表的所有記錄數(shù)據(jù)。
a.Shell命令
scan ‘student’
圖3.打印指定表數(shù)據(jù)
b.java代碼
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Scanner;
public class Test_2 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* @param args
*/
//建立連接
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();
}
}
//關(guān)閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 根據(jù)表名查找表信息
*/
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)
{
showCell((result));
}
close();
}
/**
* 格式化輸出
* @param result
*/
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(時(shí)間戳):”+cell.getTimestamp()+" ");
System.out.println(“column Family(列簇):”+new
String(CellUtil.cloneFamily(cell))+" ");
System.out.println(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");
System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");
System.out.println();
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test_2 t =new Test_2();
System.out.println(“請(qǐng)輸入要查看的表名”);
Scanner scan = new Scanner(System.in);
String tableName=scan.nextLine();
System.out.println(“信息如下:”);
t.getData(tableName);
}
}
圖4.打印指定表數(shù)據(jù)(java)
(3)向已經(jīng)創(chuàng)建好的表添加和刪除指定的列族或列。
a.Shell命令
put ‘student’,‘95001’,‘Sname’,‘LiYing’
put ‘student’,‘95001’,‘Ssex’,‘male’
put ‘student’,‘95001’,‘Sage’,‘22’
put ‘student’,‘95001’,‘Sdept’,‘CS’
put ‘student’,‘95001’,‘course:math’,‘80’
圖5.添加或刪除指定列族或列
b.java代碼
import java.io.IOException;
import java.util.Scanner;
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.Delete;
import org.apache.hadoop.hbase.client.Put;
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;
public class Test_3 {
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();
}
}
//關(guān)閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 向某一行的某一列插入數(shù)據(jù)
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名(如果其列族下沒(méi)有子列,此參數(shù)可為空)
* @param val 值
* @throws IOException
*/
public static void insertRow(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();
}
/**
* 根據(jù)表名查找表信息
*/
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)
{
showCell((result));
}
close();
}
/**
* 格式化輸出
* @param result
*/
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(時(shí)間戳):”+cell.getTimestamp()+" ");
System.out.println(“column Family(列簇):”+new
String(CellUtil.cloneFamily(cell))+" ");
System.out.println(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");
System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");
System.out.println();
}
}
/**
* 刪除數(shù)據(jù)
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey,String
colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
boolean flag2 =true;
while(flag2)
{
System.out.println(“請(qǐng)輸入你的選擇 1-刪除列族的所有數(shù)據(jù)
2-指定列的數(shù)據(jù)”);
Scanner scanner=new Scanner(System.in);
String chooseString = scanner.nextLine();
switch (chooseString) {
case “1”:
{
//刪除指定列族的所有數(shù)據(jù)
delete.addFamily(colFamily.getBytes());
table.delete(delete);
table.close();
close();
break;
}
case “2”:
{
//刪除指定列的數(shù)據(jù)
delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
table.close();
close();
break;
}
default:
{
System.out.println(" 你的輸入有誤 !?。?");
table.close();
close();
break;
}
}
System.out.println(" 你要繼續(xù)操作嗎? 是-true 否-false ");
flag2=scanner.nextBoolean();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test_3 t =new Test_3();
boolean flag =true;
while(flag)
{
System.out.println(“------------向已經(jīng)創(chuàng)建好的表中添加和刪除指定的列簇或列--------------------”);
System.out.println(" 請(qǐng)輸入您要進(jìn)行的操作 1- 添加 2-刪除 ");
Scanner scan = new Scanner(System.in);
String choose1=scan.nextLine();
switch (choose1) {
case “1”:
{
System.out.println(“請(qǐng)輸入要添加的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要添加的表的行鍵”);
String rowKey=scan.nextLine();
System.out.println(“請(qǐng)輸入要添加的表的列簇”);
String colFamily=scan.nextLine();
System.out.println(“請(qǐng)輸入要添加的表的列名”);
String col=scan.nextLine();
System.out.println(“請(qǐng)輸入要添加的值”);
String val=scan.nextLine();
try {
t.insertRow(tableName, rowKey, colFamily, col, val);
System.out.println(“插入成功:”);
t.getData(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.getMessage();
}
break;
}
case “2”:
{
System.out.println(“請(qǐng)輸入要?jiǎng)h除的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要?jiǎng)h除的表的行鍵”);
String rowKey=scan.nextLine();
System.out.println(“請(qǐng)輸入要?jiǎng)h除的表的列簇”);
String colFamily=scan.nextLine();
System.out.println(“請(qǐng)輸入要?jiǎng)h除的表的列名”);
String col=scan.nextLine();
try {
System.out.println(“----------------------表的原本信息如下---------------------”);
t.getData(tableName);
System.out.println(“____________________________正在執(zhí)行刪除操作…\n”);
t.deleteRow(tableName, rowKey, colFamily, col);
System.out.println(“____________________________刪除成功_______________\n”);
System.out.println(“---------------------刪除后
表的信息如下---------------------”);
t.getData(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.getMessage();
}
break;
}
default:
{
System.out.println(" 你的操作有誤 ?。?! ");
break;
}
}
System.out.println(" 你要繼續(xù)操作嗎? 是-true 否-false ");
flag=scan.nextBoolean();
}
System.out.println(" 程序已退出! ");
}
}
圖5.添加或刪除指定列族或列(java)
圖6.添加或刪除指定列族或列(java)
圖7.添加或刪除指定列族或列(java)
(4)清空指定的表的所有記錄數(shù)據(jù)。
a.Shell命令
truncate ‘student’
圖8.清空指定表的所有記錄
b.java代碼
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.HBaseAdmin;
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.util.Bytes;
public class Test_4 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* @param args
*/
//建立連接
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();
}
}
//關(guān)閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 清空制定的表的所有記錄數(shù)據(jù)
* @param args
* @throws IOException
*/
public static void clearRows(String tableName) throws IOException{
init();
// HBaseAdmin admin1=new HBaseAdmin(configuration);
// HTableDescriptor tDescriptor
=admin1.getTableDescriptor(Bytes.toBytes(tableName));//讀取了之前表的表名
列簇等信息,然后再進(jìn)行刪除操作。
總思想是先將原表結(jié)構(gòu)保留下來(lái),然后進(jìn)行刪除,再重新依據(jù)保存的信息重新創(chuàng)建表。
//備份表列族名
TableName tablename=TableName.valueOf(tableName);
HTableDescriptor tDescriptor = new
HTableDescriptor(TableName.valueOf(tableName));
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
List<String> cloFamily = new ArrayList<String>();
for(Result result:scanner)
{
Cell[] cells = result.rawCells();
for(Cell cell:cells){
// System.out.println(“column Family(列簇):”+new
String(CellUtil.cloneFamily(cell))+" ");
cloFamily.add(new String(CellUtil.cloneFamily(cell)));
}
}
//刪除表
admin.disableTable(tablename);
admin.deleteTable(tablename);
//重新建表
// HTableDescriptor tDescriptor = new
// HTableDescriptor(TableName.valueOf(tableName));
// tDescriptor.addFamily(new HColumnDescriptor(“name”));
for(String cf : cloFamily)
{
tDescriptor.addFamily(new HColumnDescriptor(cf));
}
admin.createTable(tDescriptor);
close();
}
/**
* 根據(jù)表名查找表信息
*/
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)
{
showCell((result));
}
close();
}
/**
* 格式化輸出
* @param result
*/
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(時(shí)間戳):”+cell.getTimestamp()+" ");
System.out.println(“column Family(列簇):”+new
String(CellUtil.cloneFamily(cell))+" ");
System.out.println(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");
System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test_4 test_4=new Test_4();
Scanner scan = new Scanner(System.in);
System.out.println(“請(qǐng)輸入要清空的表名”);
String tableName=scan.nextLine();
try {
System.out.println(“表原來(lái)的信息:”);
test_4.getData(tableName);
test_4.clearRows(tableName);
System.out.println(“表已清空:”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
圖9.清空指定表的所有記錄(java)
(5)統(tǒng)計(jì)表的行數(shù)
a.Shell命令
count ‘s1’
圖10.統(tǒng)計(jì)表的行數(shù)
b.java代碼
import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
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;
public class Test_5 {
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();
}
}
//關(guān)閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
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(“行數(shù):”+num);
scanner.close();
close();
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test_5 test_5=new Test_5();
Scanner scan = new Scanner(System.in);
System.out.println(“請(qǐng)輸入要統(tǒng)計(jì)行數(shù)的表名”);
String tableName=scan.nextLine();
test_5.countRows(tableName);
}
}
圖11.統(tǒng)計(jì)表的行數(shù)(java)
2.HBase數(shù)據(jù)庫(kù)操作
(1)現(xiàn)有以下關(guān)系型數(shù)據(jù)庫(kù)中的表和數(shù)據(jù)(見(jiàn)表A-1~表A-3),要求將其轉(zhuǎn)換為適合于HBase存儲(chǔ)的表并插人數(shù)據(jù)。
a.創(chuàng)建學(xué)生表
create ‘student’,‘S_No’,‘S_Name’,‘S_Sex’,‘S_Age’
插入數(shù)據(jù):
插入shell命令
第一行數(shù)據(jù) put ‘Student’,‘s001’,‘S_No’,‘2015001’?
put ‘Student’,‘s001’,‘S_Name’,‘Zhangsan’?
put ‘Student’,‘s001’,‘S_Sex’,‘male’?
put ‘Student’,‘s001’,‘S_Age’,‘23’?
第二行數(shù)據(jù) put ‘Student’,‘s002’,‘S_No’,‘2015002’?
put ‘Student’,‘s002’,‘S_Name’,‘Mary’?
put ‘Student’,‘s002’,‘S_Sex’,‘female’?
put ‘Student’,‘s002’,‘S_Age’,‘22’?
第三行數(shù)據(jù) put ‘Student’,‘s003’,‘S_No’,‘2015003’?
put ‘Student’,‘s003’,‘S_Name’,‘Lisi’?
put ‘Student’,‘s003’,‘S_Sex’,‘male’?
put ‘Student’,‘s003’,‘S_Age’,‘24’
圖12.創(chuàng)建學(xué)生表
b.創(chuàng)建選課表
create ‘Course’,‘C_No’,‘C_Name’,‘C_Credit’
創(chuàng)建Course表
±----------------------------------±----------------------------------+
| | 插入shell命令 |
第一行數(shù)據(jù) put ‘Course’,‘c001’,‘C_No’,‘123001’
put ‘Course’,‘c001’,‘C_Name’,‘Math’
put ‘Course’,‘c001’,‘C_Credit’,‘2.0’
第二行數(shù)據(jù) put ‘Course’,‘c002’,‘C_No’,‘123002’
put ‘Course’,‘c002’,‘C_Name’,‘Computer Science’
put ‘Course’,‘c002’,‘C_Credit’,‘5.0’
第三行數(shù)據(jù) put ‘Course’,‘c003’,‘C_No’,‘123003’
put ‘Course’,‘c003’,‘C_Name’,‘English’
put ‘Course’,‘c003’,‘C_Credit’,‘3.0’
圖13.創(chuàng)建課程表
c.創(chuàng)建選課表
create ‘SC’,‘SC_Sno’,‘SC_Cno’,‘SC_Score’
插入數(shù)據(jù):
±----------------------------------±----------------------------------+
| | 插入shell命令 |
+=+=+
第一行數(shù)據(jù) put ‘SC’,‘sc001’,‘SC_Sno’,‘2015001’
put ‘SC’,‘sc001’,‘SC_Cno’,‘123001’
put ‘SC’,‘sc001’,‘SC_Score’,‘86’
第二行數(shù)據(jù) put ‘SC’,‘sc002’,‘SC_Sno’,‘2015001’
put ‘SC’,‘sc002’,‘SC_Cno’,‘123003’
put ‘SC’,‘sc002’,‘SC_Score’,‘69’
第三行數(shù)據(jù) put ‘SC’,‘sc003’,‘SC_Sno’,‘2015002’
put ‘SC’,‘sc003’,‘SC_Cno’,‘123002’
put ‘SC’,‘sc003’,‘SC_Score’,‘77’
第四行數(shù)據(jù) put ‘SC’,‘sc004’,‘SC_Sno’,‘2015002’
put ‘SC’,‘sc004’,‘SC_Cno’,‘123003’
put ‘SC’,‘sc004’,‘SC_Score’,‘99’
第五行數(shù)據(jù) put ‘SC’,‘sc005’,‘SC_Sno’,‘2015003’
put ‘SC’,‘sc005’,‘SC_Cno’,‘123001’
put ‘SC’,‘sc005’,‘SC_Score’,‘98’
第六行數(shù)據(jù) put ‘SC’,‘sc006’,‘SC_Sno’,‘2015003’
put ‘SC’,‘sc006’,‘SC_Cno’,‘123002’
put ‘SC’,‘sc006’,‘SC_Score’,‘95’
圖13.創(chuàng)建選課表
2.請(qǐng)編程實(shí)現(xiàn)以下功能
1.createTable(String tableName, String[] fields)
創(chuàng)建表,參數(shù) tableName 為表的名稱(chēng),字符串?dāng)?shù)組 fields 為存儲(chǔ)記錄各個(gè)字段名稱(chēng)的數(shù)組。要求當(dāng) HBase 已經(jīng)存在名為 tableName
的表的時(shí)候,先刪除原有的表,然后再創(chuàng)建新的表。
2.addRecord(String tableName, String row, String[] fields,
String[] values)
向表 tableName、行 row(用 S_Name 表示)和字符串?dāng)?shù)組 fields
指定的單元格中添加對(duì)應(yīng)的數(shù)據(jù) values。其中,fields
中每個(gè)元素如果對(duì)應(yīng)的列族下還有相應(yīng)的列限定符的話(huà),用"columnFamily:column"表示。例如,同時(shí)向"Math"、“Computer
Science”、"English"三列添加成績(jī)時(shí),字符串?dāng)?shù)組 fields 為{“Score:Math”,
“Score:Computer Science”, “Score:English”},數(shù)組
values 存儲(chǔ)這三門(mén)課的成績(jī)。
3.scanColumn(String tableName, String column)
瀏覽表 tableName 某一列的數(shù)據(jù),如果某一行記錄中該列數(shù)據(jù)不存在,則返回 null。要求當(dāng)參數(shù) column 為某一列族名稱(chēng)時(shí),如果底下有若干個(gè)列限定符,則要列出每個(gè)列限定符代表的列的數(shù)據(jù);當(dāng)參數(shù) column
為某一列具體名稱(chēng)(例如"Score:Math")時(shí),只需要列出該列的數(shù)據(jù)。
4.modifyData(String tableName, String row, String column)
修改表 tableName,行 row(可以用學(xué)生姓名 S_Name 表示),列 column
指定的單元格的數(shù)據(jù)。
5.deleteRow(String tableName, String row)
刪除表 tableName 中 row 指定的行的記錄。
java代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.Delete;
import org.apache.hadoop.hbase.client.Put;
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.util.Bytes;
public class Test_Two {
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();
}
}
//關(guān)閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
*
建表。參數(shù)tableName為表的名稱(chēng),字符串?dāng)?shù)組fields為存儲(chǔ)記錄各個(gè)域名稱(chēng)的數(shù)組。
* 要求當(dāng)HBase已經(jīng)存在名為tableName的表時(shí),先刪除原有的表,然后再
* 創(chuàng)建新的表 field:列族
* @param myTableName 表名
* @param colFamily 列族名
* @throws IOException
*/
public static void createTable(String tableName,String[] fields)
throws IOException {
init();
TableName tablename = TableName.valueOf(tableName);
if(admin.tableExists(tablename)){
System.out.println(“表已存在,將執(zhí)行刪除原表,重建新表!”);
admin.disableTable(tablename);
admin.deleteTable(tablename);//刪除原來(lái)的表
}
// HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
HTableDescriptor hTableDescriptor = new
HTableDescriptor(TableName.valueOf(tableName));
for(String str:fields){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println(“表已創(chuàng)建成功”);
close();
}
/**
* 向表 tableName、行 row(用 S_Name 表示)和字符串?dāng)?shù)組 fields
指定的單元格中
* 添加對(duì)應(yīng)的數(shù)據(jù) values。
* 其中,fields 中每個(gè)元素如果對(duì)應(yīng)的列族下還有相應(yīng)的列限定符的話(huà),
* 用"columnFamily:column"表示。
* 例如,同時(shí)向"Math"、“Computer Science”、"English"三列添加成績(jī)時(shí),
* 字符串?dāng)?shù)組 fields 為{“Score:Math”, “Score:Computer Science”,
“Score:English”},
* 數(shù)組values 存儲(chǔ)這三門(mén)課的成績(jī)。
*/
public static void addRecord(String tableName,String rowKey,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(rowKey.getBytes());
String [] cols = fields[i].split(“:”);
if(cols.length==1)
{
put.addColumn(cols[0].getBytes(), “”.getBytes(),
values[i].getBytes());//因?yàn)楫?dāng)輸入的是單列族,split僅讀出一個(gè)字符字符串,即cols僅有一個(gè)元素
}
else {
put.addColumn(cols[0].getBytes(), cols[1].getBytes(),
values[i].getBytes());
}
table.put(put);
}
table.close();
close();
}
/**
* 根據(jù)表名查找表信息
*/
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)
{
showCell((result));
}
close();
}
/**
* 格式化輸出
* @param result
*/
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(時(shí)間戳):”+cell.getTimestamp()+" ");
System.out.println(“column Family(列簇):”+new
String(CellUtil.cloneFamily(cell))+" ");
System.out.println(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");
System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");
System.out.println();
}
}
/**
* 瀏覽表 tableName 某一列的數(shù)據(jù),如果某一行記錄中該列數(shù)據(jù)不存在,則返回
null。
* 要求當(dāng)參數(shù) column
為某一列族名稱(chēng)時(shí),如果底下有若干個(gè)列限定符,則要列出每個(gè)列限定符代表的列的數(shù)據(jù);
* 當(dāng)參數(shù) column
為某一列具體名稱(chēng)(例如"Score:Math")時(shí),只需要列出該列的數(shù)據(jù)。
* @param tableName
* @param column
* @throws IOException
*/
public static void scanColumn (String tableName,String column) throws
IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
String [] cols = column.split(“:”);
if(cols.length==1)
{
scan.addFamily(Bytes.toBytes(column));
}
else {
scan.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]));
}
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); result !=null;result =
scanner.next()) {
showCell(result);
}
table.close();
close();
}
/**
* 修改表 tableName,行 row(可以用學(xué)生姓名 S_Name 表示),列 column
指定的單元格的數(shù)據(jù)。
* @throws IOException
*/
public static void modifyData(String tableName,String rowKey,String
column,String value) throws IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
String [] cols = column.split(“:”);
if(cols.length==1)
{
put.addColumn(column.getBytes(),“”.getBytes() ,
value.getBytes());//qualifier:列族下的列名
}
else {
put.addColumn(cols[0].getBytes(),cols[1].getBytes() ,
value.getBytes());//qualifier:列族下的列名
}
table.put(put);
table.close();
close();
}
/**
* 刪除表 tableName 中 row 指定的行的記錄。
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey) throws
IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
table.delete(delete);
table.close();
close();
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test_Two test_Two = new Test_Two();
boolean flag =true;
while(flag)
{
System.out.println(“------------------------------------------------提供以下功能----------------------------------------------”);
System.out.println(" 1- createTable(創(chuàng)建表 ,提供表名、列族名) ");
System.out.println(" 2-addRecord (向已知表名、行鍵、列簇的表添加值)
");
System.out.println(" 3- ScanColumn(瀏覽表 某一列的數(shù)據(jù)) ");
System.out.println(" 4- modifyData(修改某表
某行,某一列,指定的單元格的數(shù)據(jù)) ");
System.out.println(" 5- deleteRow(刪除 某表 某行的記錄) ");
System.out.println(“------------------------------------------------------------------------------------------------------------------”);
Scanner scan = new Scanner(System.in);
String choose1=scan.nextLine();
switch (choose1) {
case “1”:
{
System.out.println(“請(qǐng)輸入要?jiǎng)?chuàng)建的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要?jiǎng)?chuàng)建的表的列族個(gè)數(shù)”);
int Num=scan.nextInt();
String [] fields = new String[Num];
System.out.println(“請(qǐng)輸入要?jiǎng)?chuàng)建的表的列族”);
/* Scanner scanner = new Scanner(System.in); scanner.next
如不是全局,即會(huì)記得上一次輸出。相同地址讀入值時(shí)*/
for(int i=0;i< fields.length;i++)
{
/*BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
fields[i] = in.readLine();*/
/*fields[i]=scan.next(); 因?yàn)橹皼](méi)有輸入過(guò),所以可以讀入新值*/
scan = new Scanner(System.in);
fields[i]=scan.nextLine();
}
System.out.println(“正在執(zhí)行創(chuàng)建表的操作”);
test_Two.createTable(tableName,fields);
break;
}
case “2”:
{
System.out.println(“請(qǐng)輸入要添加數(shù)據(jù)的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要添加數(shù)據(jù)的表的行鍵”);
String rowKey=scan.nextLine();
System.out.println(“請(qǐng)輸入要添加數(shù)據(jù)的表的列的個(gè)數(shù)”);
int num =scan.nextInt();
String fields[]=new String[num];
System.out.println(“請(qǐng)輸入要添加數(shù)據(jù)的表的列信息 共”+num+“條信息”);
for(int i=0;i< fields.length;i++)
{
BufferedReader in3= new BufferedReader(new
InputStreamReader(System.in));
fields[i] = in3.readLine();
/*fields[i]=scan.next(); 因?yàn)橹皼](méi)有輸入過(guò),所以可以讀入新值*/
}
System.out.println(“請(qǐng)輸入要添加的數(shù)據(jù)信息 共”+num+“條信息”);
String values[]=new String[num];
for(int i=0;i< values.length;i++)
{
BufferedReader in2 = new BufferedReader(new
InputStreamReader(System.in));
values[i] = in2.readLine();
}
System.out.println(“原表信息”);
test_Two.getData(tableName);
System.out.println(“正在執(zhí)行向表中添加數(shù)據(jù)的操作…\n”);
test_Two.addRecord(tableName, rowKey, fields, values);
System.out.println(“\n添加后的表的信息…”);
test_Two.getData(tableName);
break;
}
case “3”:
{
System.out.println(“請(qǐng)輸入要查看數(shù)據(jù)的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要查看數(shù)據(jù)的列名”);
String column=scan.nextLine();
System.out.println(“查看的信息如下:…\n”);
test_Two.scanColumn(tableName, column);
break;
}
case “4”:
{
System.out.println(“請(qǐng)輸入要修改數(shù)據(jù)的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要修改數(shù)據(jù)的表的行鍵”);
String rowKey=scan.nextLine();
System.out.println(“請(qǐng)輸入要修改數(shù)據(jù)的列名”);
String column=scan.nextLine();
System.out.println("請(qǐng)輸入要修改的數(shù)據(jù)信息 ");
String value=scan.nextLine();
System.out.println(“原表信息如下:…\n”);
test_Two.getData(tableName);
System.out.println(“正在執(zhí)行向表中修改數(shù)據(jù)的操作…\n”);
test_Two.modifyData(tableName, rowKey, column, value);
System.out.println(“\n修改后的信息如下:…\n”);
test_Two.getData(tableName);
break;
}
case “5”:
{
System.out.println(“請(qǐng)輸入要?jiǎng)h除指定行的表名”);
String tableName=scan.nextLine();
System.out.println(“請(qǐng)輸入要?jiǎng)h除指定行的行鍵”);
String rowKey=scan.nextLine();
System.out.println(“原表信息如下:…\n”);
test_Two.getData(tableName);
System.out.println(“正在執(zhí)行向表中刪除數(shù)據(jù)的操作…\n”);
test_Two.deleteRow(tableName, rowKey);
System.out.println(“\n刪除后的信息如下:…\n”);
test_Two.getData(tableName);
break;
}
default:
{
System.out.println(" 你的操作有誤 ?。?! ");
break;
}
}
System.out.println(" 你要繼續(xù)操作嗎? 是-true 否-false ");
flag=scan.nextBoolean();
}
System.out.println(" 程序已退出! ");
}
}
圖14.創(chuàng)建表功能(java)
圖15.增加記錄功能(java)
圖16.瀏覽表功能(java)
圖17.修改表功能(java)
圖18.刪除表功能(java)
3、出現(xiàn)的問(wèn)題:
1、安裝HBase2.2.2并測(cè)試HBase版本的時(shí)候遇到
錯(cuò)誤: 找不到或無(wú)法加載主類(lèi)
org.apache.hadoop.hbase.util.GetJavaProperty
2、用java寫(xiě)清空數(shù)據(jù)表的時(shí)候遇到報(bào)錯(cuò):
Table should have at least one column family. Set
hbase.table.sanity.checks to false at conf or table descriptor if you
want to bypass sanity checks
3、按照教材寫(xiě)java代碼時(shí),遇到一個(gè)錯(cuò)誤
Multiple markers at this line
- The type HTableDescriptor is deprecated
- The constructor HTableDescriptor(String) is
undefined
4、解決方案:
1、問(wèn)題原因是:因?yàn)?Hbase 沒(méi)有將其自身的依賴(lài)包添加到 classpath
配置路徑所以才會(huì)導(dǎo)致找不到自身主類(lèi)的報(bào)錯(cuò)。
查看了博客:https://blog.csdn.net/weixin_45702261/article/details/120587547?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_antiscanv2&utm_relevant_index=1
進(jìn)入/usr/local/hbase/conf/hbase-env文件,將最后一行不允許注釋?zhuān)瑔?wèn)題得到解決
2、錯(cuò)誤的原因是:創(chuàng)建Hbase數(shù)據(jù)表時(shí)至少要有一個(gè)列族名,而清空數(shù)據(jù)表的java代碼其實(shí)是刪除了整個(gè)數(shù)據(jù)表再重新建一個(gè)有著原表列族名的空表,此時(shí)要先將原表的列族名用String數(shù)組備份,再重新建表的時(shí)候依次重新添加列族名即可,代碼如下:
3、教材的Hbase版本較老,我使用的的是HBase2.2.2版本較新,那一句的語(yǔ)法API已經(jīng)修改了,用HTableDescriptor
hTableDescriptor =new HTableDescriptor(TableName.valueOf(tableName));文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-403227.html
代替HTableDescriptor hTableDescriptor = new
HTableDescriptor(tableName);即可。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-403227.html
到了這里,關(guān)于實(shí)驗(yàn)三:熟悉常用的HBase操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!