hbase創建索引


1.代碼

package Test05;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;

public class TestCoprocessor extends BaseRegionObserver {
    static Configuration config = HBaseConfiguration.create();
    static HTable table = null;

    static {
        config.set("hbase.zookeeper.quorum",
                "192.168.1.113.12:2181,192.168.1.113.13:2181,192.168.1.113.14:2181");
        try {
            table = new HTable(config, "guanzhu");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void prePut(ObserverContext<RegionCoprocessorEnvironment> e,
                       Put put, WALEdit edit, Durability durability) throws IOException {
// super.prePut(e, put, edit, durability);
        byte[] row = put.getRow();
        Cell cell = put.get("f1".getBytes(), "from".getBytes()).get(0);
        Put putIndex = new
                Put(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
        putIndex.addColumn("f1".getBytes(), "from".getBytes(), row);
        table.put(putIndex);
        table.close();
    }
}

上面這個代碼有缺陷,只能索引一次結果。建議使用下面的

package Test05;


import java.io.IOException;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CoprocessorEnvironment;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTableInterface;

import org.apache.hadoop.hbase.client.HTablePool;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;

import org.apache.hadoop.hbase.coprocessor.ObserverContext;

import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;

import org.apache.hadoop.hbase.regionserver.wal.WALEdit;

import org.apache.hadoop.hbase.util.Bytes;

public class TestCoprocessor2 extends BaseRegionObserver {

    private HTablePool pool = null;
    @Override
 public void start(CoprocessorEnvironment env) throws IOException {

        pool = new HTablePool(env.getConfiguration(), 10);
    }
    @Override

    public void prePut(ObserverContext<RegionCoprocessorEnvironment> e,

                       Put put, WALEdit edit, Durability durability) throws IOException {

        HTableInterface table1 = pool.getTable(Bytes.toBytes("guanzhu"));


        byte[] rowkey = put.getRow();

        Cell cell = put.get("f1".getBytes(), "from".getBytes()).get(0);

        Put indexput = new Put(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());

        indexput.add(Bytes.toBytes("f1"), Bytes.toBytes("from"), rowkey);


        table1.put(indexput);

        table1.close();

    }
    @Override

    public void stop(CoprocessorEnvironment env) throws IOException {
        pool.close();
    }

}

2.在hadoop創建文件

//普通集群
hdfs dfs -mkdir /hbasecp
//cdh
sudo -u hdfs hdfs dfs -mkdir /hbasecp

3.將文件上傳hadoop-1

  • 使用shell命令上傳到hdfs
hadoop fs -put hbase-observer-elasticsearch-1.0-SNAPSHOT-zcestestrecord.jar /hbase_es 
hadoop fs -chmod -R 777 /hbase_es 

下面特殊

  對CDH用戶:假設系統有一個文件夾/user/directory,注意,這個文件夾的擁有者不是所謂的“root”。實際上,這個文件夾為“hdfs”所有(755權限,這里將hdfs理解為一個屬於supergroup的用戶)。
  所以,只有hdfs可以對文件夾進行寫操作。與Unix/Linux不同,hdfs是超級使用者(superuser),而不是root。
  所以,當你想要進行創建文件夾或者上傳文件等操作的時候,可以這么做:

sudo -u hdfs hdfs dfs -mkdir /user/intern/test67
///

sudo -u hdfs hdfs dfs -put myfile.txt /user/intern/test67 顯然,上面的方法麻煩在於:每當進行一次操作的時候,都需要切換成hdfs用戶。這在spark-shell開發程序倒還好,但是對提交作業的時候,顯然是很不方便的。 這里,我們可以創建一個文件夾,更改其權限為root(或你的用戶名)。所以,你可以向這個文件夾隨意的存、改文件了。 sudo -u hdfs hadoop fs -chown root /user/intern/test67

之后是

4.以下是解綁的命令(這個是錯誤時刪除使用)

//hbase shell命令
disable 'test_record' alter 'test_record', METHOD => 'table_att_unset',NAME => 'coprocessor$1' enable 'test_record' desc 'test_record'

 5.測試

hbase(main):017:0> put 'fensi','c','f1:from','b'

對fensi表添加數據,查看數據是guanzhu表里也有數據

注意細節:

1.上面圖片路徑錯誤。正確的是:   alter 'fensi', METHOD => 'table_att', 'coprocessor' => 'hdfs:///hbasecp/cppp.jar|Test05.TestCoprocessor|1001|'

2.cdh坑略多自己慢慢試驗最終的結果是下面這樣的,有很多權限

3.cdh將jar添加到hdfs上

 

 

 

hbase建立索引參考:https://www.cnblogs.com/liuwei6/p/6837674.html

 cdh修改權限參考:https://blog.csdn.net/g11d111/article/details/72902112

   https://blog.csdn.net/m0_37739193/article/details/78544276

----------------------------------------------------------------------------------------------------------------------------------------------------------------

上面的方案角色分配:fensi是數據表,導數據(當滿足jar的屬性時數據會插到guanzhu表里,guanzhu表里是fensi表的rowkey索引)。guanzhu是索引表。

 二級索引的缺點

 hbase創建索引不當時將會導致HRegionServer都掛掉,重啟hbase也會掛掉。解決辦法

修改hbase所有配置文件
vi hbase-site.xml 
添加如下,關閉hbase的二級索引

<property>
<name>hbase.coprocessor.abortonerror</name>
<value>false</value>
</property>

重啟hbase。然后刪除帶有索引的表,之后把hbase-site.xml上面這個剛剛添加的刪除。即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM