2021-05-28 13:48:19 -04:00
---
layout: default
2022-09-26 12:28:00 -04:00
title: SQL and PPL CLI
parent: SQL and PPL
nav_order: 3
2023-05-26 17:48:31 -04:00
redirect_from:
- /search-plugins/sql/cli/
2021-05-28 13:48:19 -04:00
---
2022-09-26 12:28:00 -04:00
# SQL and PPL CLI
2021-05-28 13:48:19 -04:00
2022-09-26 12:28:00 -04:00
The SQL and PPL command line interface (CLI) is a standalone Python application that you can launch with the `opensearchsql` command.
2021-05-28 13:48:19 -04:00
2022-09-26 12:28:00 -04:00
To use the SQL and PPL CLI, install the SQL plugin on your OpenSearch instance, run the CLI using MacOS or Linux, and connect to any valid OpenSearch endpoint.
2021-05-28 13:48:19 -04:00
2021-06-09 22:15:41 -04:00
![SQL CLI ]({{site.url}}{{site.baseurl}}/images/cli.gif )
2021-05-28 13:48:19 -04:00
## Features
2022-09-26 12:28:00 -04:00
The SQL and PPL CLI has the following features:
2021-05-28 13:48:19 -04:00
- Multi-line input
2022-09-26 12:28:00 -04:00
- PPL support
2021-05-28 13:48:19 -04:00
- Autocomplete for SQL syntax and index names
- Syntax highlighting
- Formatted output:
- Tabular format
- Field names with color
- Enabled horizontal display (by default) and vertical display when output is too wide for your terminal, for better visualization
- Pagination for large output
- Works with or without security enabled
- Supports loading configuration files
- Supports all SQL plugin queries
## Install
Launch your local OpenSearch instance and make sure you have the SQL plugin installed.
2022-09-26 12:28:00 -04:00
1. Install the CLI:
```console
2022-01-25 17:54:58 -05:00
pip3 install opensearchsql
2021-05-28 13:48:19 -04:00
```
The SQL CLI only works with Python 3.
{: .note }
2022-09-26 12:28:00 -04:00
2. To launch the CLI, run:
```console
2021-05-28 13:48:19 -04:00
opensearchsql https://localhost:9200 --username admin --password admin
```
By default, the `opensearchsql` command connects to http://localhost:9200.
## Configure
When you first launch the SQL CLI, a configuration file is automatically created at `~/.config/opensearchsql-cli/config` (for MacOS and Linux), the configuration is auto-loaded thereafter.
You can configure the following connection properties:
2022-01-25 17:54:58 -05:00
- `endpoint` : You do not need to specify an option. Anything that follows the launch command `opensearchsql` is considered as the endpoint. If you do not provide an endpoint, by default, the SQL CLI connects to http://localhost:9200.
2023-05-04 11:11:54 -04:00
- `-u/-w` : Supports username and password for HTTP basic authentication, such as with the Security plugin or fine-grained access control for Amazon OpenSearch Service.
2021-05-28 13:48:19 -04:00
- `--aws-auth` : Turns on AWS sigV4 authentication to connect to an Amazon OpenSearch endpoint. Use with the AWS CLI (`aws configure`) to retrieve the local AWS configuration to authenticate and connect.
2023-04-04 15:49:27 -04:00
For a list of all available configurations, see [clirc ](https://github.com/opensearch-project/sql/blob/1.x/sql-cli/src/opensearch_sql_cli/conf/clirc ).
2021-05-28 13:48:19 -04:00
## Using the CLI
2022-09-26 12:28:00 -04:00
1. Run the CLI tool. If your cluster runs with the default security settings, use the following command:
```console
opensearchsql --username admin --password admin https://localhost:9200
2021-05-28 13:48:19 -04:00
```
2022-09-26 12:28:00 -04:00
If your cluster runs without security, run:
```console
opensearchsql
2021-05-28 13:48:19 -04:00
```
2022-09-26 12:28:00 -04:00
2. Run a sample SQL command:
```sql
2021-05-28 13:48:19 -04:00
SELECT * FROM accounts;
```
By default, you see a maximum output of 200 rows. To show more results, add a `LIMIT` clause with the desired value.
2022-09-26 12:28:00 -04:00
To exit the CLI tool, select **Ctrl+D** .
{: .tip }
## Using the CLI with PPL
1. Run the CLI by specifying the query language:
```console
opensearchsql -l ppl < params >
```
2. Execute a PPL query:
```sql
source=accounts | fields firstname, lastname
```
2021-05-28 13:48:19 -04:00
## Query options
2022-09-26 12:28:00 -04:00
Run a single query with the following command line options:
2021-05-28 13:48:19 -04:00
- `-q` : Follow by a single query
- `-f` : Specify JDBC or raw format output
- `-v` : Display data vertically
- `-e` : Translate SQL to DSL
## CLI options
2022-09-26 12:28:00 -04:00
- `--help` : Help page for options
2022-01-25 17:54:58 -05:00
- `-l` : Query language option. Available options are `sql` and `ppl` . Default is `sql`
2021-05-28 13:48:19 -04:00
- `-p` : Always use pager to display output
- `--clirc` : Provide path for the configuration file