hbase基礎的增刪查改
阿新 • • 發佈:2018-12-03
table 自行更改包名
package table;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
student 自行更改類名
public class student{ public static Configuration configuration; public static Connection connection; public static Admin admin; //主函式中的語句請逐句執行,只需刪除其前的//即可,如:執行insertRow時請將其他語句註釋 public static void main(String[] args)throws IOException{ 建立一個表,表名為mxy,列族為name,age,sex,course // createTable("mxy",new String[]{"name","age","sex","course"}); //在Score表中插入一條資料,其行鍵為95001,sname為Mary(因為sname列族下沒有子列所以第四個引數為空) //等價命令:put 'Score','95001','sname','Mary' //在終端執行的命令 insertRow("mxy", "95001", "name","","mxy"); //在Score表中插入一條資料,其行鍵為95001,course:Math為88(course為列族,Math為course下的子列) //等價命令:put 'Score','95001','score:Math','88' insertRow("mxy", "95001", "age", "", "18"); //在Score表中插入一條資料,其行鍵為95001,course:English為85(course為列族,English為course下的子列) //等價命令:put 'Score','95001','score:English','85' insertRow("mxy", "95003", "course", "English", "85"); insertRow("mxy", "95002", "course", "Math", "59"); //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("mxy", "95001", "course", ""); //3、刪除Score表中指定行資料,其行鍵為95001 //執行這句程式碼前請deleteRow方法的定義中,將刪除指定列資料的程式碼註釋,以及將刪除制定列族的程式碼註釋 //等價命令:deleteall 'Score','95001' //deleteRow("mxy", "95001","",""); //查詢Score表中,行鍵為95001,列族為course,列為Math的值 //getData("mxy", "95001", "course", "Math"); //查詢Score表中,行鍵為95001,列族為sname的值(因為sname列族下沒有子列所以第四個引數為空) //getData("mxy", "95001","course",""); //刪除mxy表 //deleteTable("mxy"); // 追加插入(將原有value的後面追加新的value,如原有value=a追加value=bc則最後的value=abc) //appendData("mxy","95003","course","English","1"); //全域性掃描顯示 scanTable("mxy"); }
全域性掃描顯示
/** * 全表掃描 * @param result */ public static void scanTable(String tableName) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); for (Result result : results) { for (Cell cell : result.rawCells()) { System.out.println( "行鍵:" + new String(CellUtil.cloneRow(cell)) + "\t" + "列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" + "列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" + "值:" + new String(CellUtil.cloneValue(cell)) + "\t" + "時間戳:" + cell.getTimestamp()); } } }
// 追加插入(將原有value的後面追加新的value,如原有value=a追加value=bc則最後的value=abc) public static void appendData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException { init(); // 獲取表 Table table = connection.getTable(TableName.valueOf(tableName)); // 通過rowkey建立一個append物件 Append append = new Append(rowKey.getBytes()); // 在append物件中設定列族、列、值 append.add(columnFamily.getBytes(), column.getBytes(), value.getBytes()); // 追加資料 table.append(append); // 關閉資源 table.close(); }
//建立連線
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();
}
}
/**
* 建表。HBase的表中會有一個系統預設的屬性作為主鍵,主鍵無需自行建立,預設為put命令操作中表名後第一個資料,因此此處無需建立id列
* @param myTableName 表名
* @param colFamily 列族名
* @throws IOException
*/
public static void createTable(String myTableName,String[] colFamily) 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:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("create table success");
}
close();
}
/**
* 刪除指定表
* @param tableName 表名
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
/**
* 檢視已有表
* @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
* 向某一行的某一列插入資料
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名(如果其列族下沒有子列,此引數可為空)
* @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();
}
/**
* 刪除資料
* @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());
//刪除指定列族的所有資料
//delete.addFamily(colFamily.getBytes());
//刪除指定列的資料
//delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
table.close();
close();
}
/**
* 根據行鍵rowkey查詢資料
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
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:"+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))+" ");
}
}
}