cassandra命令行操作
查看cqlsh命令帮助
root@sitewhere-cassandra-0:/# cqlsh --help Usage: cqlsh.py [options] [host [port]] CQL Shell for Apache Cassandra Options: --version show program's version number and exit -h, --help show this help message and exit -C, --color Always use color output --no-color Never use color output --browser=BROWSER The browser to use to display CQL help, where BROWSER can be: - one of the supported browsers in https://docs.python.org/2/library/webbrowser.html. - browser path followed by %s, example: /usr/bin /google-chrome-stable %s --ssl Use SSL --no_compact No Compact -u USERNAME, --username=USERNAME Authenticate as user. -p PASSWORD, --password=PASSWORD Authenticate using password. -k KEYSPACE, --keyspace=KEYSPACE Authenticate to the given keyspace. -f FILE, --file=FILE Execute commands from FILE, then exit --debug Show additional debugging information --encoding=ENCODING Specify a non-default encoding for output. (Default: utf-8) --cqlshrc=CQLSHRC Specify an alternative cqlshrc file location. --cqlversion=CQLVERSION Specify a particular CQL version, by default the highest version supported by the server will be used. Examples: "3.0.3", "3.1.0" --protocol-version=PROTOCOL_VERSION Specify a specific protcol version otherwise the client will default and downgrade as necessary -e EXECUTE, --execute=EXECUTE Execute the statement and quit. --connect-timeout=CONNECT_TIMEOUT Specify the connection timeout in seconds (default: 5 seconds). --request-timeout=REQUEST_TIMEOUT Specify the default request timeout in seconds (default: 10 seconds). -t, --tty Force tty mode (command prompt). Connects to 127.0.0.1:9042 by default. These defaults can be changed by setting $CQLSH_HOST and/or $CQLSH_PORT. When a host (and optional port number) are given on the command line, they take precedence over any defaults.
连接数据库
root@sitewhere-cassandra-0:/# cqlsh Connected to cassandra at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.11.3 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh>
查看帮助
cqlsh> help Documented shell commands: =========================== CAPTURE CLS COPY DESCRIBE EXPAND LOGIN SERIAL SOURCE UNICODE CLEAR CONSISTENCY DESC EXIT HELP PAGING SHOW TRACING CQL help topics: ================ AGGREGATES CREATE_KEYSPACE DROP_TRIGGER TEXT ALTER_KEYSPACE CREATE_MATERIALIZED_VIEW DROP_TYPE TIME ALTER_MATERIALIZED_VIEW CREATE_ROLE DROP_USER TIMESTAMP ALTER_TABLE CREATE_TABLE FUNCTIONS TRUNCATE ALTER_TYPE CREATE_TRIGGER GRANT TYPES ALTER_USER CREATE_TYPE INSERT UPDATE APPLY CREATE_USER INSERT_JSON USE ASCII DATE INT UUID BATCH DELETE JSON BEGIN DROP_AGGREGATE KEYWORDS BLOB DROP_COLUMNFAMILY LIST_PERMISSIONS BOOLEAN DROP_FUNCTION LIST_ROLES COUNTER DROP_INDEX LIST_USERS CREATE_AGGREGATE DROP_KEYSPACE PERMISSIONS CREATE_COLUMNFAMILY DROP_MATERIALIZED_VIEW REVOKE CREATE_FUNCTION DROP_ROLE SELECT CREATE_INDEX DROP_TABLE SELECT_JSON
连续两个Tab键
cqlsh>
? BEGIN CLEAR CONSISTENCY CREATE DELETE DESCRIBE EXPAND HELP LIST PAGING SELECT SHOW TRACING UPDATE exit
ALTER CAPTURE CLS COPY DEBUG DESC DROP GRANT INSERT LOGIN REVOKE SERIAL SOURCE TRUNCATE USE quit
查询有哪些keyspace
cqlsh> DESC KEYSPACES
system_traces system_schema system_auth system system_distributed
选择keyspace
cqlsh> use system_schema ;
cqlsh:system_schema>
查看tables
cqlsh:system_schema> desc tables;
tables triggers views keyspaces dropped_columns
functions aggregates indexes types columns
cqlsh:system_schema>
查询数据
cqlsh:system_schema> select * from keyspaces; keyspace_name | durable_writes | replication --------------------+----------------+------------------------------------------------------------------------------------- system_auth | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '1'} system_schema | True | {'class': 'org.apache.cassandra.locator.LocalStrategy'} system_distributed | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '3'} system | True | {'class': 'org.apache.cassandra.locator.LocalStrategy'} system_traces | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '2'} (5 rows) cqlsh:system_schema> select * from keyspaces where keyspace_name='system_auth';//注意不能用双引号,不然语法错误 keyspace_name | durable_writes | replication ---------------+----------------+------------------------------------------------------------------------------------- system_auth | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '1'} (1 rows)
增删改查和mysql的语句差不多