Python notes: Introduction to the happybase library

1. Introduction to happybase library

happybase is a python interface library for interacting with Apache HBase database.

Its official documents are detailed at: https://happybase.readthedocs.io/en/latest/

Therefore, in order to better understand the happybase tool, let’s briefly introduce the HBase database, but since I’m not a database, here is some generalization based on the information found on the Internet. If there is a need for details to understand the necessary readers , Still need to study this thing carefully.

HBase is a distributed storage system based on HDFS (Hadoop Distributed File System). It is a distributed storage system based on rowkey. Its advantages include:

  1. High performance, extremely high writing efficiency;
  2. Support big data;
  3. High reliability;

However, relatively, his shortcomings are:

  1. Does not support sql language;
  2. The query only supports rowkey-based search, and does not support multi-condition query;

Therefore, the main application scenarios of HBase are: write-intensive scenarios that do not require a lot of read operations . Among them, a typical example is the server log.

2. Installation of happybase library

Let's first look at the installation of the happybase library.

This is actually very simple under the Linux system, as long as it is simple pip install.

The specific installation commands are as follows:

pip install happybase

3. HBase database instance

Obviously, before using the happybase library to query and modify the HBase database, we must first associate it with a specific table in the HBase library.

Give the happybase library to connect to the database command as follows:

# HOST: HBase数据库ip
# PORT: 端口号
connection = happybase.Connection(HOST, PORT, timeout=3000000)

Through the above commands, we can connect to the HBase database, which connectionis a database link instance. Below we give some related operation functions:

  1. open()
    • Open the HBase database instance
  2. close()
    • Shut down the HBase database instance
  3. tables()
    • View all table names in the HBase database instance
  4. table(name, use_prefix=True)
    • Instantiate a table in the HBase database and return a table instance;
  5. create_table(name, families)
    • Create a new table in the HBase database;
  6. delete_table(name, disable=False)
    • Delete a table from the HBase database

4. HBase table example

From the above, we know connection.Table(table_name)that a table in HBase can be instantiated through commands.

The table data format in the HBase database is a two-element tuple, and its content is the data row_keyand its actual data content.

The specific format is given as follows:

(row_key, row_dict)

Next, let's take a look at how to operate the tables in the HBase database.

  1. data query:
    1. row(row, columns=None, timestamp=None, include_timestamp=False)
      • If you know the row_keyinformation of the data , you can directly row()query the data through the method;
    2. rows(rows, columns=None, timestamp=None, include_timestamp=False)
      • rows()The method is the row()method of batch calling;
    3. cells(row, column, versions=None, timestamp=None, include_timestamp=False)
      • Take out a certain element characteristic of the data directly from the table;
  2. Data traversal
    1. scan(row_start=None, row_stop=None, row_prefix=None, columns=None, filter=None, timestamp=None, include_timestamp=False, batch_size=1000, scan_batching=None, limit=None, sorted_columns=False, reverse=False)
      • If you don’t know the row_keyinformation, you can scan()traverse the entire table through methods;
  3. Data insertion
    1. put(row, data, timestamp=None, wal=True)
      • put()Data can be inserted through methods;
  4. Data deletion
    1. delete(row, columns=None, timestamp=None, wal=True)
      • The delete()method can be used to delete the data;

5. Summary

The above is a brief introduction to the happybase library.

Generally speaking, the purpose of using the happybase library is to perform operations such as traversing, querying, inserting or deleting a data table in the HBase database. The above section 3 briefly introduces how to use happybase to associate the database. A certain table of, and section 4 briefly introduces several main APIs of happybase for data manipulation in the table.

According to our personal experience, the above content should be enough to meet most of the usage needs, but if you still encounter problems that cannot be handled, you can check the happybase interface api document, the link is as follows:

6. Reference link

  1. https://happybase.readthedocs.io/en/latest/
  2. https://www.zhihu.com/question/39859266
  3. https://blog.csdn.net/L13763338360/article/details/106916440
  4. https://sq.163yun.com/blog/article/174620451741294592
  5. https://blog.csdn.net/y472360651/article/details/79059457
  6. https://blog.csdn.net/xc_zhou/article/details/80868332

Guess you like

Origin blog.csdn.net/codename_cys/article/details/109749631