Considerations for Htable and Htablepool use in hbase databases

Source: Internet
Author: User
Tags thread zookeeper

Both Htable and Htablepool are part of the HBase client API that you can use to perform CRUD operations on hbase tables. The following combined with the application in the project, the use of the two processes of attention to do a summary summary.

Htable

Htable is a Java API object that HBase clients communicate with the HBase server, and the client can perform CRUD operations on Htable object and server (add-check). It's very simple to create:

Configuration conf = hbaseconfiguration.create ();
htable table = new htable (conf, "tablename");
TODO CRUD Operation ...

Some things to note when using htable:

1. Circumvent the creation cost of Htable objects

Because the client creates a Htable object, a series of actions are required: check. META. The table confirms that the HBase table with the specified name exists, that the table is valid, and so on, that the overall time overhead is heavier and can take several seconds, so it is a good idea to create the Htable object that is needed at the start of the program, if you use the Java API, which is typically created Direct reuse after program startup.

2. The Htable object is not thread safe

The Htable object is not thread-safe for the client to read and write data, so when multithreading, create a separate htable object for each thread, and do not share htable objects among the different objects, especially when client Auto Flash is set to false. Data inconsistency may be caused by the presence of local write buffer.

3. Sharing between Htable objects configuration

The benefit of Htable objects sharing configuration objects is that:

Shared Zookeeper connection: Each client needs to establish a connection with the zookeeper, query the user's table regions location, which can be cached and shared after the connection is established;

Shared public resources: clients need to find-root-and. META. Tables through zookeeper, which requires network transport overhead, and the client caches these public resources to reduce subsequent network transport overhead and speed up the lookup process.

Therefore, this approach is compared to the following:

htable table1 = new Htable ("Table1");

htable table2 = new Htable ("Table2");

The following approach is more effective:

Configuration conf = hbaseconfiguration.create ();

htable table1 = new htable (conf, "Table1");

htable table2 = new htable (conf, "table2");

Note: Even high-load multithreaded programs do not find performance problems due to shared configuration, and if not, you can try not to share configuration.

Htablepool

Htablepool can resolve thread-unsafe problems with htable, while maintaining a fixed number of htable objects to reuse these htable resource objects during program operation.

Configuration conf = hbaseconfiguration.create ();

Htablepool pool = new Htablepool (conf, 10);

1. Htablepool can automatically create Htable objects, and is completely transparent to the client, to avoid the problem of concurrency modification between multithreading.

2. The Htable object in Htablepool is a public configuration connection that can reduce network overhead.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/extra/

The use of Htablepool is simple: each time before the operation, through the Htablepool GetTable method to obtain a Htable object, and then put/get/scan/delete and other operations, Finally, the Htable object is put back into Htablepool through the Htablepool puttable method.

Here is a simple example of using Htablepool:

public void CreateUser (string username, string firstName, String lastName, string email, string password, string roles) th  Rows IOException {
htable table = rm.gettable (usertable.name);
Put on = new put (Bytes.tobytes (username));
Put.add (usertable.data_family, Usertable.firstname,
bytes.tobytes (FIRSTNAME));
Put.add (usertable.data_family, Usertable.lastname,
bytes.tobytes (LASTNAME));
Put.add (usertable.data_family, Usertable.email, Bytes.tobytes (EMAIL));
Put.add (usertable.data_family, Usertable.credentials,
bytes.tobytes (password));
Put.add (usertable.data_family, Usertable.roles, Bytes.tobytes (ROLES));
Table.put (put);
Table.flushcommits ();
Rm.puttable (table);

As for the authenticity of multithreading using htablepool, it needs to be done through actual testing.

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.