HBase Java API編程
一、任務要求:
(1)createTable(String tableName, String[] fields)
創建表,參數tableName爲表的名稱,字符串數組fields爲存儲記錄各個域名稱的數組。要求當HBase已經存在名爲tableName的表的時候,先刪除原有的表,然後再創建新的表。
主要代碼:
運行結果:
用HBase shell中查看
(2)addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row(用S_Name表示)和字符串數組files指定的單元格中添加對應的數據values。其中fields中每個元素如果對應的列族下還有相應的列限定符的話,用“columnFamily:column”表示。例如,同時向“Math”、“Computer Science”、“English”三列添加成績時,字符串數組fields爲{“Score:Math”,”Score;Computer Science”,”Score:English”},數組values存儲這三門課的成績。
主要代碼:
運行結果:
用HBase shell中查看
確實添加了三組數據
(3)scanColumn(String tableName, String column)
瀏覽表tableName某一列的數據,如果某一行記錄中該列數據不存在,則返回null。要求當參數column爲某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表的列的數據;當參數column爲某一列具體名稱(例如“Score:Math”)時,只需要列出該列的數據。
主要代碼:
運行結果:
與用HBase shell中查看的結果一致。
(4)modifyData(String tableName, String row, String column)
修改表tableName,行row(可以用學生姓名S_Name表示),列column指定的單元格的數據。
主要代碼:
運行結果用HBase shell查看:
Math的分數由100變成了60,修改成功。
(5)deleteRow(String tableName, String row)
刪除表tableName中row指定的行的記錄。
主要代碼:
運行結果用HBase shell 查看
沒有數據,成功刪除了。
二、完整代碼實現
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.Scanner;
public class HBaseAPI {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static long ts;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
while (true) {
System.out
.println("******************HBase對數據庫的操作******************");
System.out.println("1.創建一個表");
System.out.println("2.添加數據");
System.out.println("3.瀏覽數據");
System.out.println("4.修改數據");
System.out.println("5.刪除表中row指定的行的記錄");
int a = in.nextInt();
switch (a) {
case 1:
createTable("tableName", new String[] { "Score" });
break;
case 2:
String[] fields = { "Score:Math", "Score:Computer Science",
"Score:English" };
String[] values = { "100", "90", "80" };
addRecord("tableName", "Score", fields, values);
break;
case 3:
scanColumn("tableName", "Score");
break;
case 4:
modifyData("tableName", "Score", "Math", "60");
break;
case 5:
deleteRow("tableName", "Score");
break;
}
}
}
// 建立連接
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 myTableName, String[] fields)
throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println("talbe is exists!");
} else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for (String str : fields) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("create table success");
}
close();
}
// 添加數據
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 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 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();
}
//刪除表中row指定的行的記錄
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();
}
}