Common shell operations in Phoenix
- 1. Client login
- 2. Client exits
- 3. Query the list of all tables
- Note: The above commands all start with "`!`", and the following commands all need to end with "`;`".
- 4. Create a table
- 5. Delete table
- 6. Modify the table structure
- 7. View the table structure (see 6 for details)
- 8. Create an index for a column of the table
- 9. Delete the index
- 10. Insert a record
- 11. Delete the data in the table
- 12. Modify the data in the table
- 13. Query the data in the table
- 14. Associate existing tables in Hbase
- 15. help manual
- 16. Query metadata information
1. Client login
[root@dn3 ~]# cd /opt/module/phoenix && /usr/bin/python2 bin/sqlline.py dn3,dn4,dn5:2181
Setting property: [incremental, false]
Setting property: [isolation, TRANSACTION_READ_COMMITTED]
issuing: !connect jdbc:phoenix:dn3,dn4,dn5:2181 none none org.apache.phoenix.jdbc.PhoenixDriver
Connecting to jdbc:phoenix:dn3,dn4,dn5:2181
22/07/25 18:40:48 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Connected to: Phoenix (version 5.0)
Driver: PhoenixEmbeddedDriver (version 5.0)
Autocommit status: true
Transaction isolation: TRANSACTION_READ_COMMITTED
Building list of tables and columns for tab-completion (set fastconnect to true to skip)...
225/225 (100%) Done
Done
sqlline version 1.2.0
0: jdbc:phoenix:dn3,dn4,dn5:2181>
2. Client exits
The following 2 commands can be used:
- !quit
- !exit
0: jdbc:phoenix:dn3,dn4,dn5:2181> !quit
Closing: org.apache.phoenix.jdbc.PhoenixConnection
[root@dn3 ~]#
0: jdbc:phoenix:dn3,dn4,dn5:2181> !exit
Closing: org.apache.phoenix.jdbc.PhoenixConnection
[root@dn3 ~]#
3. Query the list of all tables
The following 2 commands can be used:
- !tables
- !table
0: jdbc:phoenix:dn3,dn4,dn5:2181> !tables
+------------+-----------------+-------------------------+-+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | |
+------------+-----------------+-------------------------+-+
| | SYSTEM | CATALOG | |
| | SYSTEM | FUNCTION | |
| | SYSTEM | LOG | |
| | SYSTEM | SEQUENCE | |
| | SYSTEM | STATS | |
| | | hbase_test | |
| | GMALL_REALTIME | DIM_ACTIVITY_INFO | |
| | GMALL_REALTIME | DIM_ACTIVITY_RULE | |
| | GMALL_REALTIME | DIM_ACTIVITY_SKU | |
| | GMALL_REALTIME | DIM_BASE_CATEGORY1 | |
| | GMALL_REALTIME | DIM_BASE_CATEGORY2 | |
| | GMALL_REALTIME | DIM_BASE_CATEGORY3 | |
| | GMALL_REALTIME | DIM_BASE_PROVINCE | |
| | GMALL_REALTIME | DIM_BASE_REGION | |
| | GMALL_REALTIME | DIM_BASE_TRADEMARK | |
| | GMALL_REALTIME | DIM_COUPON_INFO | |
| | GMALL_REALTIME | DIM_COUPON_RANGE | |
| | GMALL_REALTIME | DIM_FINANCIAL_SKU_COST | |
| | GMALL_REALTIME | DIM_SKU_INFO | |
| | GMALL_REALTIME | DIM_SPU_INFO | |
| | GMALL_REALTIME | DIM_USER_INFO | |
+------------+-----------------+-------------------------+-+
Note: The table information in Phoenix is in the SYSTEM.CATALOG table, and you can also view the system table information through the following sql:
select * from SYSTEM.CATALOG;
Note: The above commands all
!
start with " ", and the following commands all need to;
end with " ".describe also needs to start with !.
4. Create a table
(1) Table names, column family names, and table names must be enclosed in double quotes if lowercase is required.
create table "person" ("id" integer not null primary key, "cf"."name" varchar, "cf"."age" integer);
(2) The column family names and column names created in this way are all capitalized.
create table "person" (id integer not null primary key, cf.name varchar, cf.age integer);
Note: Table names and column family names are case-sensitive . If you add double quotation marks, you must use lowercase table names to check, otherwise you will not be able to find them. Column names are not case-sensitive, and are displayed in uppercase, but can still be searched in lowercase.
Example:
0: jdbc:phoenix:dn3,dn4,dn5:2181> create table if not exists "person" ("id" integer not null primary key, "cf"."name" varchar, "cf"."age" integer);
No rows affected (2.389 seconds)
0: jdbc:phoenix:dn3,dn4,dn5:2181> !tables
+------------+-----------------+-------------------------+-+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | |
+------------+-----------------+-------------------------+-+
| | SYSTEM | CATALOG | |
| | SYSTEM | FUNCTION | |
| | SYSTEM | LOG | |
| | SYSTEM | SEQUENCE | |
| | SYSTEM | STATS | |
| | | hbase_test | |
| | | person | |
+------------+-----------------+-------------------------+-+
0: jdbc:phoenix:dn3,dn4,dn5:2181> !describe "person";
+------------+--------------+-------------+--------------+-+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | COLUMN_NAME | |
+------------+--------------+-------------+--------------+-+
| | | person | id | |
| | | person | name | |
| | | person | age | |
+------------+--------------+-------------+--------------+-+
5. Delete table
0: jdbc:phoenix:dn3,dn4,dn5:2181> drop table "person";
No rows affected (1.25 seconds)
6. Modify the table structure
Note the fields added as follows:
- address is capitalized by default because there are no quotation marks;
- birthday is lowercase because of the quotation marks;
0: jdbc:phoenix:dn3,dn4,dn5:2181> alter table "person" add address varchar;
No rows affected (6.629 seconds)
0: jdbc:phoenix:dn3,dn4,dn5:2181> !describe "person";
+------------+--------------+-------------+--------------+------------+------------+--------------+--------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | COLUMN_NAME | DATA_TYPE | TYPE_NAME | COLUMN_SIZE | BUFFER |
+------------+--------------+-------------+--------------+------------+------------+--------------+--------+
| | | person | id | 4 | INTEGER | null | null |
| | | person | name | 12 | VARCHAR | null | null |
| | | person | age | 4 | INTEGER | null | null |
| | | person | ADDRESS | 12 | VARCHAR | null | null |
+------------+--------------+-------------+--------------+------------+------------+--------------+--------
0: jdbc:phoenix:dn3,dn4,dn5:2181> alter table "person" add "birthday" timestamp;
No rows affected (6.786 seconds)
0: jdbc:phoenix:dn3,dn4,dn5:2181> !describe "person";
+------------+--------------+-------------+--------------+------------+------------+--------------+--------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | COLUMN_NAME | DATA_TYPE | TYPE_NAME | COLUMN_SIZE | BUFFER |
+------------+--------------+-------------+--------------+------------+------------+--------------+--------+
| | | person | id | 4 | INTEGER | null | null |
| | | person | name | 12 | VARCHAR | null | null |
| | | person | age | 4 | INTEGER | null | null |
| | | person | ADDRESS | 12 | VARCHAR | null | null |
| | | person | birthday | 93 | TIMESTAMP | null | null |
+------------+--------------+-------------+--------------+------------+------------+--------------+--------
7. View the table structure (see 6 for details)
[root@dn3 ~]# !describe "person";
8. Create an index for a column of the table
Note that before building an index, you need to add the following configuration code to the hbase-site.xml file on the RegionServer:
Otherwise an error will be reported:
Error: ERROR 1029 (42Y88): Mutable secondary indexes must have the hbase.regionserver.wal.codec property set to org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec in the hbase-sites.xml of every region server. tableName=index_device_data_test08 (state=42Y88,code=1029)
Two double quotes, the first is the name of the index, and the second is the name of the original table The
index field here is: deviceID
Include The brackets contain the columns to be returned
0: jdbc:phoenix:dn3,dn4,dn5:2181> create index "person_index" on "person"("cf"."name") include ("cf"."name","cf"."age");
9. Delete the index
0: jdbc:phoenix:dn3,dn4,dn5:2181> drop index "person_index" on "person";
10. Insert a record
注意:数据值需要用引号时只能用单引号,双引号会报错
0: jdbc:phoenix:dn3,dn4,dn5:2181> upsert into "person" values(1,'david', 20, 'ShangHai', '2002-01-20 00:00:00');
1 row affected (0.071 seconds)
0: jdbc:phoenix:dn3,dn4,dn5:2181> select * from "person";
+-----+-----------+------+-----------+--------------------------+
| id | name | age | ADDRESS | birthday |
+-----+-----------+------+-----------+--------------------------+
| 1 | david | 20 | ShangHai | 2002-01-20 00:00:00.000 |
+-----+-----------+------+-----------+--------------------------+
1 row selected (0.037 seconds)
11. Delete the data in the table
注意:数据值需要用引号时只能用单引号,双引号会报错
0: jdbc:phoenix:dn3,dn4,dn5:2181> delete from "person" where "cf"."name"='david';
1 row affected (0.031 seconds)
0: jdbc:phoenix:dn3,dn4,dn5:2181> select * from "person";
+-----+-------+------+----------+-----------+
| id | name | age | ADDRESS | birthday |
+-----+-------+------+----------+-----------+
+-----+-------+------+----------+-----------+
No rows selected (0.036 seconds)
12. Modify the data in the table
注意:修改时必须带上id,否则会报错
注意:数据值需要用引号时只能用单引号,双引号会报错
0: jdbc:phoenix:dn3,dn4,dn5:2181> upsert into "person"("id",ADDRESS) values(1,'BeiJing');
1 row affected (0.013 seconds)
0: jdbc:phoenix:dn3,dn4,dn5:2181> select * from "person";
+-----+--------+------+----------+--------------------------+
| id | name | age | ADDRESS | birthday |
+-----+--------+------+----------+--------------------------+
| 1 | david | 20 | BeiJing | 2002-01-20 00:00:00.000 |
+-----+--------+------+----------+--------------------------+
1 row selected (0.026 seconds)
13. Query the data in the table
注意:数据值需要用引号时只能用单引号,双引号会报错
①.Full table query
0: jdbc:phoenix:dn3,dn4,dn5:2181> select * from "person";
+-----+---------+------+------------+--------------------------+
| id | name | age | ADDRESS | birthday |
+-----+---------+------+------------+--------------------------+
| 1 | david | 20 | BeiJing | 2002-01-20 00:00:00.000 |
| 2 | amanda | 18 | ShangHai | 2004-10-13 00:00:00.000 |
| 3 | andy | 2 | GuangZhou | 2020-07-29 00:00:00.000 |
+-----+---------+------+------------+--------------------------+
②.Condition query
0: jdbc:phoenix:dn3,dn4,dn5:2181> select * from "person" where "name"='andy';
+-----+-------+------+------------+--------------------------+
| id | name | age | ADDRESS | birthday |
+-----+-------+------+------------+--------------------------+
| 3 | andy | 2 | GuangZhou | 2020-07-29 00:00:00.000 |
+-----+-------+------+------------+--------------------------+
1 row selected (0.025 seconds)
③. Group query (group by)
0: jdbc:phoenix:dn3,dn4,dn5:2181> select ADDRESS, count(ADDRESS) from "person" where "age" > 16 group by ADDRESS;
+-----------+-----------------+
| ADDRESS | COUNT(ADDRESS) |
+-----------+-----------------+
| BeiJing | 1 |
| ShangHai | 1 |
+-----------+-----------------+
2 rows selected (0.023 seconds)
④. case when transform query
0: jdbc:phoenix:dn3,dn4,dn5:2181> select "name", (case "name" when 'david' then 'haha' when 'amanda' then 'xixi' else "name" end) as nicky_name from "person";
+---------+-------------+
| name | NICKY_NAME |
+---------+-------------+
| david | haha |
| amanda | xixi |
| andy | andy |
+---------+-------------+
3 rows selected (0.015 seconds)
14. Associate existing tables in Hbase
grammar:
create view if not exists "device_data"(
"pk" varchar not null primary key,
"data"."deviceID" varchar,
"data"."deviceTime" varchar,
"data"."modelID" varchar,
"data"."processState" varchar,
"data"."subDevice" varchar
);
hbase data preparation:
#创建Hbase 表
create 'device_data', 'data'
#向Hbase表里插入测试数据
put 'device_data','rowKey_001', 'data:deviceID', 'D1001'
put 'device_data','rowKey_001', 'data:deviceTime', '2022-07-13 11:11:11'
put 'device_data','rowKey_001', 'data:modelID', 'M1001'
put 'device_data','rowKey_001', 'data:processState', 'Fail'
put 'device_data','rowKey_001', 'data:subDevice', 'D1001_S0001'
put 'device_data','rowKey_002', 'data:deviceID', 'D1002'
put 'device_data','rowKey_002', 'data:deviceTime', '2022-07-14 12:12:12'
put 'device_data','rowKey_002', 'data:modelID', 'M1002'
put 'device_data','rowKey_002', 'data:processState', 'Succ'
put 'device_data','rowKey_002', 'data:subDevice', 'D1002_S0002'
[root@dn3 ~]# hbase shell
2022-07-25 19:45:15,606 WARN [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
Version 2.0.5, r76458dd074df17520ad451ded198cd832138e929, Mon Mar 18 00:41:49 UTC 2019
Took 0.0030 seconds
hbase(main):001:0>
hbase(main):020:0> scan 'device_data'
ROW COLUMN+CELL
rowKey_001 column=data:deviceID, timestamp=1658750104769, value=D1001
rowKey_001 column=data:deviceTime, timestamp=1658750124167, value=2022-07-13 11:11:11
rowKey_001 column=data:modelID, timestamp=1658750124195, value=M1001
rowKey_001 column=data:processState, timestamp=1658750124224, value=Fail
rowKey_001 column=data:subDevice, timestamp=1658750124255, value=D1001_S0001
rowKey_002 column=data:deviceID, timestamp=1658750124281, value=D1002
rowKey_002 column=data:deviceTime, timestamp=1658750124303, value=2022-07-14 12:12:12
rowKey_002 column=data:modelID, timestamp=1658750124330, value=M1002
rowKey_002 column=data:processState, timestamp=1658750124355, value=Succ
rowKey_002 column=data:subDevice, timestamp=1658750124929, value=D1002_S0002
2 row(s)
Took 0.0410 seconds
Create Hbase mapping table in Phonenix:
0: jdbc:phoenix:dn3,dn4,dn5:2181> create view if not exists "device_data"(
. . . . . . . . . . . . . . . . > "pk" varchar not null primary key,
. . . . . . . . . . . . . . . . > "data"."deviceID" varchar,
. . . . . . . . . . . . . . . . > "data"."deviceTime" varchar,
. . . . . . . . . . . . . . . . > "data"."modelID" varchar,
. . . . . . . . . . . . . . . . > "data"."processState" varchar,
. . . . . . . . . . . . . . . . > "data"."subDevice" varchar
. . . . . . . . . . . . . . . . > );
Note:
(1) If no column family is added, an error will be reported as follows:
Error: ERROR 505 (42000): Table is read only. (state=42000,code=505)
(2) If double quotes are not added, hbase will not be matched The field in the table, the result is that although it is associated with the database, it has no value! ! !
(3) When connecting, it is best to create tables of Phoenix varchar type, which is not easy to make mistakes
(4) It is best to create a view instead of a table. Because deleting the table on the Phoenix side will also delete the hbase table, but not if it is a view.
Query the Phonenix table (note the double quotes):
0: jdbc:phoenix:dn3,dn4,dn5:2181> select * from "device_data";
+-------------+-----------+----------------------+----------+---------------+--------------+
| pk | deviceID | deviceTime | modelID | processState | subDevice |
+-------------+-----------+----------------------+----------+---------------+--------------+
| rowKey_001 | D1001 | 2022-07-13 11:11:11 | M1001 | Fail | D1001_S0001 |
| rowKey_002 | D1002 | 2022-07-14 12:12:12 | M1002 | Succ | D1002_S0002 |
+-------------+-----------+----------------------+----------+---------------+--------------+
2 rows selected (0.071 seconds)
15. help manual
0: jdbc:phoenix:dn3,dn4,dn5:2181> !help
!all Execute the specified SQL against all the current
connections
!autocommit Set autocommit mode on or off
!batch Start or execute a batch of statements
!brief Set verbose mode off
!call Execute a callable statement
!close Close the current connection to the database
!closeall Close all current open connections
!columns List all the columns for the specified table
!commit Commit the current transaction (if autocommit is off)
!connect Open a new connection to the database.
!dbinfo Give metadata information about the database
!describe Describe a table
!dropall Drop all tables in the current database
!exportedkeys List all the exported keys for the specified table
!go Select the current connection
!help Print a summary of command usage
!history Display the command history
!importedkeys List all the imported keys for the specified table
!indexes List all the indexes for the specified table
!isolation Set the transaction isolation for this connection
!list List the current connections
!manual Display the SQLLine manual
!metadata Obtain metadata information
!nativesql Show the native SQL for the specified statement
!outputformat Set the output format for displaying results
(table,vertical,csv,tsv,xmlattrs,xmlelements)
!primarykeys List all the primary keys for the specified table
!procedures List all the procedures
!properties Connect to the database specified in the properties file(s)
!quit Exits the program
!reconnect Reconnect to the database
!record Record all output to the specified file
!rehash Fetch table and column names for command completion
!rollback Roll back the current transaction (if autocommit is off)
!run Run a script from the specified file
!save Save the current variabes and aliases
!scan Scan for installed JDBC drivers
!script Start saving a script to a file
!set Set a sqlline variable
Variable Value Description
=============== ========== ================================
autoCommit true/false Enable/disable automatic
transaction commit
autoSave true/false Automatically save preferences
color true/false Control whether color is used
for display
fastConnect true/false Skip building table/column list
for tab-completion
force true/false Continue running script even
after errors
headerInterval integer The interval between which
headers are displayed
historyFile path File in which to save command
history. Default is
$HOME/.sqlline/history (UNIX,
Linux, Mac OS),
$HOME/sqlline/history (Windows)
incremental true/false Do not receive all rows from
server before printing the first
row. Uses fewer resources,
especially for long-running
queries, but column widths may
be incorrect.
isolation LEVEL Set transaction isolation level
maxColumnWidth integer The maximum width to use when
displaying columns
maxHeight integer The maximum height of the
terminal
maxWidth integer The maximum width of the
terminal
numberFormat pattern Format numbers using
DecimalFormat pattern
outputFormat table/vertical/csv/tsv Format mode for
result display
propertiesFile path File from which SqlLine reads
properties on startup; default is
$HOME/.sqlline/sqlline.properties
(UNIX, Linux, Mac OS),
$HOME/sqlline/sqlline.properties
(Windows)
rowLimit integer Maximum number of rows returned
from a query; zero means no
limit
showElapsedTime true/false Display execution time when
verbose
showHeader true/false Show column names in query
results
showNestedErrs true/false Display nested errors
showWarnings true/false Display connection warnings
silent true/false Be more silent
timeout integer Query timeout in seconds; less
than zero means no timeout
trimScripts true/false Remove trailing spaces from
lines read from script files
verbose true/false Show verbose error messages and
debug info
!sql Execute a SQL command
!tables List all the tables in the database
!typeinfo Display the type map for the current connection
!verbose Set verbose mode on
Comments, bug reports, and patches go to ???
0: jdbc:phoenix:dn3,dn4,dn5:2181>
16. Query metadata information
0: jdbc:phoenix:dn3,dn4,dn5:2181> !dbinfo
allProceduresAreCallable false
allTablesAreSelectable true
dataDefinitionCausesTransactionCommit false
dataDefinitionIgnoredInTransactions false
doesMaxRowSizeIncludeBlobs false
getCatalogSeparator .
getCatalogTerm Tenant
getDatabaseProductName Phoenix
getDatabaseProductVersion 5.0
getDefaultTransactionIsolation 2
getDriverMajorVersion 5
getDriverMinorVersion 0
getDriverName PhoenixEmbeddedDriver
getDriverVersion 5.0
getExtraNameCharacters
getIdentifierQuoteString "
getMaxBinaryLiteralLength 0
getMaxCatalogNameLength 0
getMaxCharLiteralLength 4000
getMaxColumnNameLength 200
getMaxColumnsInGroupBy 1
getMaxColumnsInIndex 0
getMaxColumnsInOrderBy 0
getMaxColumnsInSelect 0
getMaxColumnsInTable 0
getMaxConnections 0
getMaxCursorNameLength 0
getMaxIndexLength 0
getMaxProcedureNameLength 0
getMaxRowSize 0
getMaxSchemaNameLength 0
getMaxStatementLength 0
getMaxStatements 0
getMaxTableNameLength 0
getMaxTablesInSelect 1
getMaxUserNameLength 0
getNumericFunctions
getProcedureTerm procedure
getSchemaTerm schema
getSearchStringEscape \
getSQLKeywords
getStringFunctions
getSystemFunctions
getTimeDateFunctions
getURL jdbc:phoenix:dn3,dn4,dn5:2181
getUserName
isCatalogAtStart false
isReadOnly false
nullPlusNonNullIsNull true
nullsAreSortedAtEnd false
nullsAreSortedAtStart true
nullsAreSortedHigh false
nullsAreSortedLow true
storesLowerCaseIdentifiers false
storesLowerCaseQuotedIdentifiers false
storesMixedCaseIdentifiers false
storesMixedCaseQuotedIdentifiers true
storesUpperCaseIdentifiers true
storesUpperCaseQuotedIdentifiers true
supportsAlterTableWithAddColumn true
supportsAlterTableWithDropColumn true
supportsANSI92EntryLevelSQL false
supportsANSI92FullSQL false
supportsANSI92IntermediateSQL false
supportsBatchUpdates true
supportsCatalogsInDataManipulation false
supportsCatalogsInIndexDefinitions false
supportsCatalogsInPrivilegeDefinitions false
supportsCatalogsInProcedureCalls false
supportsCatalogsInTableDefinitions false
supportsColumnAliasing true
supportsConvert true
supportsCoreSQLGrammar false
supportsCorrelatedSubqueries false
supportsDataDefinitionAndDataManipulationTransactionstrue
supportsDataManipulationTransactionsOnly false
supportsDifferentTableCorrelationNames false
supportsExpressionsInOrderBy true
supportsExtendedSQLGrammar false
supportsFullOuterJoins false
supportsGroupBy true
supportsGroupByBeyondSelect false
supportsGroupByUnrelated false
supportsIntegrityEnhancementFacility false
supportsLikeEscapeClause true
supportsLimitedOuterJoins false
supportsMinimumSQLGrammar false
supportsMixedCaseIdentifiers false
supportsMixedCaseQuotedIdentifiers true
supportsMultipleResultSets true
supportsMultipleTransactions true
supportsNonNullableColumns true
supportsOpenCursorsAcrossCommit false
supportsOpenCursorsAcrossRollback false
supportsOpenStatementsAcrossCommit false
supportsOpenStatementsAcrossRollback false
supportsOrderByUnrelated false
supportsOuterJoins true
supportsPositionedDelete false
supportsPositionedUpdate false
supportsSchemasInDataManipulation true
supportsSchemasInIndexDefinitions false
supportsSchemasInPrivilegeDefinitions false
supportsSchemasInProcedureCalls false
supportsSchemasInTableDefinitions false
supportsSelectForUpdate false
supportsStoredProcedures false
supportsSubqueriesInComparisons false
supportsSubqueriesInExists false
supportsSubqueriesInIns false
supportsSubqueriesInQuantifieds false
supportsTableCorrelationNames false
supportsTransactions true
supportsUnion false
supportsUnionAll false
usesLocalFilePerTable false
usesLocalFiles false
0: jdbc:phoenix:dn3,dn4,dn5:2181>