An example of hadoop-hbase-Observer

Source: Internet
Author: User
Hbase (main): 021: 0> describe 'users'
Description enabled
{Name => 'users', coprocessor $1 => 'file: // home/u/myjar/usersobserver. Jar | test. hbase. Inaction. example5_2.users true
Observer | 1001 | ', families => [{name => 'info', bloomfilter => 'none', replication_scope => '0', compression => 'n'
One ', versions => '3', TTL => '000000', min_versions => '0', blocksize => '000000', in_memory => 'false', Block
Cache => 'true'}]}
1 row (s) in 0.0610 seconds

Disable the table first, and then alert. That 1001 is the priority. Don't forget enable.

Disable 'users'
Alter 'users', method => 'table _ ATT ', 'coprocessor' => 'file: // home/u/mylib/usersobserver. jar | test. hbase. inaction. example5_2.usersobserver | 1001 |'
Alter 'users', method => 'table _ att_unset', name => 'coprocessor $ 2' Delete
Enable 'users'

Hbase (main): 022: 0> scan 'users'
Row column + cell
Id01 column = info: email, timestamp = 1413963413002, [email protected]
Id01 column = info: name, timestamp = 1413963413002, value = wyj
Id01 column = info: Password, timestamp = 1413963413002, value = 000000
Id01 column = info: User, timestamp = 1413963413002, value = id01
Id09 column = info: email, timestamp = 1414566775616, [email protected]
Id09 column = info: name, timestamp = 1414566775616, value = test9
Id09 column = info: Password, timestamp = 1414566775616, value = 9
Id09 column = info: User, timestamp = 1414566775616, value = id09
Id99 column = info: email, timestamp = 1414565339530, [email protected]
Id99 column = info: name, timestamp = 1414565339530, value = test99
Id99 column = info: Password, timestamp = 1414565339530, value ====== 01 ====
Id99 column = info: User, timestamp = 1414565339530, value = id99
Invalid column = info: invalid_pass, timestamp = 1414566775657, value = invalid_pass: 9
11 row (s) in 0.1290 seconds

The START () and stop () methods are also executed in the Enable and disable tables.

It is redundant to obtain the connection and release the connection between start () and stop () in the connection pool.

The observer is similar to a trigger. When operating a table, pay attention to recursive calls. After a put operation is intercepted, the table will be recursive again.

This is an example. It doesn't actually make any sense. It's just an example.

Note that hbase locks are row-level locks. If you want to put the same row, it will not succeed.

Observer is not easy to debug. Only a bunch of logs can be created ......

Package test. hbase. inaction. example5_2; import Java. io. ioexception; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import Org. apache. hadoop. hbase. coprocessorenvironment; 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. baseregion Observer; 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; import test. hbase. inaction. example2_4.user; import test. hbase. inaction. example2_4.usersdao;/* <span style = "color: # ff0000;"> first disable the table and then alert. The 1001 is the priority. Do not forget enable. </Span> disable 'users' alter 'users', method => 'table _ ATT ', 'coprocessor' => 'file: /// home/u/mylib/followsobserver. jar | test. hbase. inaction. example5_2.followsobserver | 1001 | 'alter 'users', method => 'table _ att_unset', name => 'coprocessor $ 2' <span style = "white-space: pre "> </span> enable 'users' describe 'users' */public class usersobserver extends baseregionobserver {private final log = logfactory. Getlog (this. getclass (); Private htablepool pool = NULL; Public void start (coprocessorenvironment env) throws ioexception {log.info ("wyj restart start () begin"); pool = new htablepool (Env. getconfiguration (), integer. max_value); log.info ("pool =" + pool); log.info ("wyj -------------------------- start () End");} public void stop (coprocessorenvironment env) throws ioexception {log. in Fo ("wyj ---------------------------- stop () begin"); pool. close (); log.info ("wyj finished stop () End");} public void postput (final observercontext <regioncoprocessorenvironment> E, final put, final waledit edit, final Boolean writetowal) {log.info ("wyj: ========================================================== ========= postput () begin "); try {byte [] Table = E. getenvironment (). getregion (). getregion Info (). gettablename (); log.info ("table =" + bytes. tostring (table); If (! Bytes. equals (table, usersdao. table_name) {return;} string pass = bytes. tostring (put. get (usersdao. info_fam, usersdao. pass_col ). get (0 ). getvalue (); log.info ("Pass =" + pass); byte [] user = put. get (usersdao. info_fam, usersdao. user_col ). get (0 ). getvalue (); log.info ("user =" + bytes. tostring (User); If (pass = NULL | pass. length () <6) {log.info ("password. length = "+ pass. length (); usersdao Dao = new usersdao (pool); log.info ("pool =" + pool); Dao. adduser ("id99", "test99", "[email protected]", "==== 01 ===="); log.info ("Dao. adduser (\ "id99 \", \ "test99 \", \ "[email protected] \", \ "99999999 \"); Final ");}} catch (exception ex) {log. error (ex, ex);} log.info ("wyj: ========================================================== ========= postput () end ");}}

An example of hadoop-hbase-Observer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.