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:
- High performance, extremely high writing efficiency;
- Support big data;
- High reliability;
- …
However, relatively, his shortcomings are:
- Does not support sql language;
- 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 connection
is a database link instance. Below we give some related operation functions:
open()
- Open the HBase database instance
close()
- Shut down the HBase database instance
tables()
- View all table names in the HBase database instance
table(name, use_prefix=True)
- Instantiate a table in the HBase database and return a table instance;
create_table(name, families)
- Create a new table in the HBase database;
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_key
and 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.
- data query:
row(row, columns=None, timestamp=None, include_timestamp=False)
- If you know the
row_key
information of the data , you can directlyrow()
query the data through the method;
- If you know the
rows(rows, columns=None, timestamp=None, include_timestamp=False)
rows()
The method is therow()
method of batch calling;
cells(row, column, versions=None, timestamp=None, include_timestamp=False)
- Take out a certain element characteristic of the data directly from the table;
- Data traversal
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_key
information, you canscan()
traverse the entire table through methods;
- If you don’t know the
- Data insertion
put(row, data, timestamp=None, wal=True)
put()
Data can be inserted through methods;
- Data deletion
delete(row, columns=None, timestamp=None, wal=True)
- The
delete()
method can be used to delete the data;
- The
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
- https://happybase.readthedocs.io/en/latest/
- https://www.zhihu.com/question/39859266
- https://blog.csdn.net/L13763338360/article/details/106916440
- https://sq.163yun.com/blog/article/174620451741294592
- https://blog.csdn.net/y472360651/article/details/79059457
- https://blog.csdn.net/xc_zhou/article/details/80868332