hbase

1.hbase的偽分布安裝
1.1 下載hbase
http://archive.apache.org/dist/hbase/
1.2 sudo tar -zxf ~/Downloads/hbase-1.2.4-bin.tar.gz -C /usr/local
1.3 sudo mv /usr/local/hbase-1.2.4 /usr/local/hbase
1.4 添加hbase路徑,vi ~/.bashrc,

Paste_Image.png

source ~/.bashrc
1.5 查看安裝情況魔招,cd /usr/local
sudo chown -R hadoop ./hbase
hbase version
1.6 配置
vi /usr/local/hbase/conf/hbase-env.sh
內(nèi)容:
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export HBASE_CLASSPATH=/usr/local/hadoop/conf
export HBASE_MANAGES_ZK=true
vi /usr/local/hbase/conf/hbase-site.xml
內(nèi)容:
Paste_Image.png

start-hbase.sh
hbase shell
2.hbase shell

2.1 創(chuàng)建表砂碉,添加元素

2.2查詢

Paste_Image.png

2.3全表查詢

Paste_Image.png

2.4表的狀態(tài)

Paste_Image.png

2.5刪除
hbase(main):021:0> deleteall 'person','jack'
0 row(s) in 0.1380 seconds

hbase(main):022:0> scan 'person'
ROW COLUMN+CELL
row1 column=address:home, timestamp=1485324730116, value=xian s
treet
row1 column=address:work, timestamp=1485324766972, value=shangh
ai street
row1 column=name:, timestamp=1485325047451, value=jack
row2 column=name:, timestamp=1485325244571, value=zjn
2 row(s) in 0.1600 seconds

hbase(main):023:0> deleteall 'person','row2'
0 row(s) in 0.0480 seconds

hbase(main):024:0> scan 'person'
ROW COLUMN+CELL
row1 column=address:home, timestamp=1485324730116, value=xian s
treet
row1 column=address:work, timestamp=1485324766972, value=shangh
ai street
row1 column=name:, timestamp=1485325047451, value=jack
1 row(s) in 0.1260 seconds

hbase(main):025:0> delete 'person','row1','name'
0 row(s) in 0.0490 seconds

hbase(main):026:0> scan 'person'
ROW COLUMN+CELL
row1 column=address:home, timestamp=1485324730116, value=xian s
treet
row1 column=address:work, timestamp=1485324766972, value=shangh
ai street
1 row(s) in 0.0970 seconds

hbase(main):027:0> disable 'person'
0 row(s) in 2.4530 seconds

hbase(main):028:0> drop 'person'
0 row(s) in 2.4570 seconds

hbase(main):029:0> list
TABLE
0 row(s) in 0.0340 seconds

=> []

3 java api 操作hbase
'''
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.
;
import java.io.IOException;

public class Hbase_base {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

    //主函數(shù)中的語句請逐句執(zhí)行,只需刪除其前的//即可特碳,如:執(zhí)行insertRow時請將其他語句注釋
    public static void main(String[] args)throws IOException{
        //創(chuàng)建一個表诚亚,表名為Score,列族為sname,course
        createTable("Score",new String[]{"sname","course"});
 
        //在Score表中插入一條數(shù)據(jù)午乓,其行鍵為95001,sname為Mary(因為sname列族下沒有子列所以第四個參數(shù)為空)
        //等價命令:put 'Score','95001','sname','Mary'
        insertRow("Score", "95001", "sname", "", "Mary");
        //在Score表中插入一條數(shù)據(jù)站宗,其行鍵為95001,course:Math為88(course為列族,Math為course下的子列)
        //等價命令:put 'Score','95001','score:Math','88'
        insertRow("Score", "95001", "course", "Math", "88");
        //在Score表中插入一條數(shù)據(jù)益愈,其行鍵為95001,course:English為85(course為列族梢灭,English為course下的子列)
        //等價命令:put 'Score','95001','score:English','85'
        insertRow("Score", "95001", "course", "English", "85");
 
        //1、刪除Score表中指定列數(shù)據(jù)腕唧,其行鍵為95001,列族為course或辖,列為Math
        //執(zhí)行這句代碼前請deleteRow方法的定義中,將刪除指定列數(shù)據(jù)的代碼取消注釋注釋枣接,將刪除制定列族的代碼注釋
        //等價命令:delete 'Score','95001','score:Math'
        //deleteRow("Score", "95001", "course", "Math");
 
        //2颂暇、刪除Score表中指定列族數(shù)據(jù),其行鍵為95001,列族為course(95001的Math和English的值都會被刪除)
        //執(zhí)行這句代碼前請deleteRow方法的定義中但惶,將刪除指定列數(shù)據(jù)的代碼注釋耳鸯,將刪除制定列族的代碼取消注釋
        //等價命令:delete 'Score','95001','score'
        //deleteRow("Score", "95001", "course", "");
 
        //3、刪除Score表中指定行數(shù)據(jù)膀曾,其行鍵為95001
        //執(zhí)行這句代碼前請deleteRow方法的定義中县爬,將刪除指定列數(shù)據(jù)的代碼注釋,以及將刪除制定列族的代碼注釋
        //等價命令:deleteall 'Score','95001'
        //deleteRow("Score", "95001", "", "");
 
        //查詢Score表中添谊,行鍵為95001财喳,列族為course,列為Math的值
        getData("Score", "95001", "course", "Math");
        //查詢Score表中斩狱,行鍵為95001耳高,列族為sname的值(因為sname列族下沒有子列所以第四個參數(shù)為空)
        //getData("Score", "95001", "sname", "");
 
        //刪除Score表
        //deleteTable("Score");
    }
 
    //建立連接
    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();
        }
    }
 
    /**
     * 建表。HBase的表中會有一個系統(tǒng)默認的屬性作為主鍵所踊,主鍵無需自行創(chuàng)建泌枪,默認為put命令操作中表名后第一個數(shù)據(jù),因此此處無需創(chuàng)建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();
    }
    /**
     * 向某一行的某一列插入數(shù)據(jù)
     * @param tableName 表名
     * @param rowKey 行鍵
     * @param colFamily 列族名
     * @param col 列名(如果其列族下沒有子列秕岛,此參數(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();
    }
 
    /**
     * 刪除數(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());
        //刪除指定列族的所有數(shù)據(jù)
        //delete.addFamily(colFamily.getBytes());
        //刪除指定列的數(shù)據(jù)
        //delete.addColumn(colFamily.getBytes(), col.getBytes());
 
        table.delete(delete);
        table.close();
        close();
    }
    /**
     * 根據(jù)行鍵rowkey查找數(shù)據(jù)
     * @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))+" ");
        }
    }

}
'''

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末碌燕,一起剝皮案震驚了整個濱河市误证,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌修壕,老刑警劉巖愈捅,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異叠殷,居然都是意外死亡改鲫,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門林束,熙熙樓的掌柜王于貴愁眉苦臉地迎上來像棘,“玉大人,你說我怎么就攤上這事壶冒÷铺猓” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵胖腾,是天一觀的道長烟零。 經(jīng)常有香客問我,道長咸作,這世上最難降的妖魔是什么锨阿? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮记罚,結(jié)果婚禮上墅诡,老公的妹妹穿的比我還像新娘。我一直安慰自己桐智,他們只是感情好末早,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著说庭,像睡著了一般然磷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上刊驴,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天姿搜,我揣著相機與錄音,去河邊找鬼捆憎。 笑死舅柜,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的攻礼。 我是一名探鬼主播业踢,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼栗柒,長吁一口氣:“原來是場噩夢啊……” “哼礁扮!你這毒婦竟也來了知举?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤太伊,失蹤者是張志新(化名)和其女友劉穎雇锡,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體僚焦,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡锰提,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了芳悲。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片立肘。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖名扛,靈堂內(nèi)的尸體忽然破棺而出谅年,到底是詐尸還是另有隱情,我是刑警寧澤肮韧,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布融蹂,位于F島的核電站,受9級特大地震影響弄企,放射性物質(zhì)發(fā)生泄漏超燃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一拘领、第九天 我趴在偏房一處隱蔽的房頂上張望意乓。 院中可真熱鬧,春花似錦院究、人聲如沸洽瞬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽伙窃。三九已至,卻和暖如春样漆,著一層夾襖步出監(jiān)牢的瞬間为障,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工放祟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鳍怨,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓跪妥,卻偏偏與公主長得像鞋喇,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子眉撵,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容