程式設計實現以下指定功能,並用hadoop提供的hbase shell命令完成相同任務:
(1) 列出hbase所有的表的相關資訊,例如表名;
public static void main(string args) throws ioexception
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
(2) 在終端列印出指定的表的所有記錄資料;
public static void main(string args) throws ioexceptionelse}}
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
(3) 向已經建立好的表新增和刪除指定的列族或列;
public static void main(string args) throws ioexception
(4) 清空指定的表的所有記錄資料;
public static void main(string args) throws ioexceptionelse
if(admin != null)
admin.close();
if(connection != null)
connection.close();
}
(5) 統計表的行數。
public static void main(string args) throws ioexception
現有以下關係型資料庫中的表和資料,要求將其轉換為適合於hbase儲存的表並插入資料: 學號
姓名性別
年齡2018001
zhangsan
male
232018002
marry
female
222018003
lisi
male24
課程號課程名
學分12301
math
3.012002
computer science
3.312003
english
3.5
學號課程號
成績2018001
123001
862018001
123003
692018002
123-2
77
建立hbase格式的表
public static void insertcolumn(table table ,string row,string columnzoo,string column,string data ) throws ioexception
public static void addata(table table) throws ioexception
public static void main(string args) throws ioexception
table table = connection.gettable(tablename);
addata(table);
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
同時,請程式設計完成以下指定功能:
(1)createtable(string tablename, string fields)
建立表,引數tablename為表的名稱,字串陣列fields為儲存記錄各個網域名稱稱的陣列。要求當hbase已經存在名為tablename的表的時候,先刪除原有的表,然後再建立新的表。
public static void createtable(string tablename ,string fields) throws ioexception
htabledescriptor htabledescriptor = new htabledescriptor(tablena);
for(string s :fields)
admin.createtable(htabledescriptor);
system.out.println("new table is create successfully");
if(admin != null)
admin.close();
if(connection != null)
connection.close();
}
(2)addrecord(string tablename, string row, string fields, string values)
向表tablename、行row(用s_name表示)和字串陣列files指定的單元格中新增對應的資料values。其中fields中每個元素如果對應的列族下還有相應的列限定符的話,用「columnfamily:column」表示。例如,同時向「math」、「computer science」、「english」三列新增成績時,字串陣列fields為,陣列values儲存這三門課的成績。
public static void addrecord(string tablename,string row,string fileds,string values) throws ioexception
if(connection != null)
connection.close();
}
(3)scancolumn(string tablename, string column)
瀏覽表tablename某一列的資料,如果某一行記錄中該列資料不存在,則返回null。要求當引數column為某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表的列的資料;當引數column為某一列具體名稱(例如「score:math」)時,只需要列出該列的資料。
public static void scancolumn(string tablename,string column) throws ioexception
} if(connection!=null)
connection.close();
}
(4)modifydata(string tablename, string row, string column,string val)
修改表tablename,行row(可以用學生姓名s_name表示),列column指定的單元格的資料。
public static void modifydata(string tablename,string row ,string column,string val) throws ioexception
}
(5)deleterow(string tablename, string row)
刪除表tablename中row指定的行的記錄。
public static void deleterow(string tablename,string row) throws ioexception
《HBase實戰》一導讀
hbase實戰 2008年秋季我開始和hbase結緣,當時它還是乙個新生專案,一年前剛剛發布。早期版本出來時,hbase表現很不錯,但是也不是沒有令人尷尬的缺陷。hbase專案當時有近10個軟體commiffer,作為乙個apache子專案還算不錯。接下來是nosql宣傳的高潮。當時專有名詞nosq...
Hbase實戰踩坑
寫幾個hbase踩過的坑吧 問題1 truncate table 後region 個數為1 故不要 truncate table。hbase 盡力不要執行truncate table。一旦執行 region個數就會變為1,之前的預分割槽就沒有用了。那麼如果想要刪除 清空資料 只能重新建表!問題2 t...
HBase隔離方案實戰 rsgroup
本文參考 這篇文章寫的很好,就是命令沒法用,一度以為我們的集群不支援rsgroup,無意中試出了幫助命令,才發現,命令不太對,不知道是作者粗心還是每個版本的命令不一樣,這裡做了一些修正,我們的集群是cdh5.12.1,hbase是1.2.0 hbase適合用於海量資料的儲存,橫向擴充套件非常方便,隨...