Explore data in the console
Work with your project in a terminal session built for you, with readable output and fast queries
anfra's core commands answer in YAML, built for the coding agents that run them most. When you run them yourself, use anfra console: answers come back as readable tables, in colour, in milliseconds.
Early release
anfra console is new in anfra 0.18, and it needs a real terminal. Completion of commands, flags and dataset names isn't there yet. Run anfra update to get the latest version.
Why use it
- Results you can read. A query returns a table, not YAML. Answers and errors are in colour, and JSON is indented for reading.
- Fast answers. The console starts
anfra servein the background and keeps it warm, so queries don't pay the start-up cost each time. When you leave, it stops the server it started. - Less typing. You type
validate, notanfra validate. You set the dataset once with\-d ecommerce, and every query in the session uses it. - AQL on several lines. In query mode you type AQL across as many lines as you like and run it when it's done.
- History that stays. Press Up to reach entries from earlier sessions, as in a shell or psql.
- Safe beside your agents. The console refuses to start without a terminal, so a script or an agent piping commands can't open one. Plain
anfracommands keep answering in YAML.
An example session
The query from the video, as text:
$ anfra console query
anfra console 0.20.0 in ~/acme
Type AQL, ending it with ;; to run it. \? helps, \c goes back.
query> \-d ecommerce
query> orders
... | group(orders.status)
... | select(orders.status, order_count, revenue);;
Result:
┌────────────────┬─────────────┬───────────┐
│ orders->status │ order_count │ revenue │
├────────────────┼─────────────┼───────────┤
│ cancelled │ 306 │ 245873.46 │
│ shipped │ 301 │ 259979.76 │
│ completed │ 893 │ 777199.73 │
└────────────────┴─────────────┴───────────┘
Has more: false
Timings:
compiling_query 41 ms
connecting_to_dw 15 ms
open_connection 15 ms
executing_query 3 ms
fetching_query_result 2 ms
total 61 msStart a session
Run it from your project folder:
anfra console # or the short form: anfra cThe session opens in command mode, with a > prompt. To start straight in a mode, name it: anfra c query opens query mode, anfra c query --lang sql opens SQL mode, and anfra c search opens search mode.
Under the prompt, a status line shows where you are: the project folder, the flags you have set, and ● server once the server is up. Notices, such as a new anfra release, appear on the right.
Run commands
In command mode, type any anfra command without anfra:
> validate
> show ecommerce
> query -d ecommerce "orders | group(orders.status) | select(orders.status, order_count)"
> statusEach answer comes back in the pretty format, errors included. A line that isn't a command, such as AQL typed by mistake, fails as it would in a shell, and the console tells you to enter query mode.
Query with AQL
Query mode takes AQL directly, so you don't wrap it in quotes or escape it.
- Enter query mode with
\c query. The prompt changes toquery>. - Set the dataset with
\-d ecommerce. Until you do, the status line warns you:! --dataset is missing. Set it using \-d <value>. - Type your AQL. Enter adds a new line, so you can write the query across several lines.
- End the query with
;;and press Enter to run it. Or press Alt+Enter to run it as it is.
A query ends with ;; because a single ; can be part of the query itself, as in metric m = count(orders.id); orders | group(orders.status) | select(orders.status, m).
Other ways to write a query
You don't need a full pipeline. For a single number, type the measure on its own:
query> sum(products.price * order_items.quantity);;
Result:
┌──────────────────────┐
│ sum_24288e4fdcbb81f8 │
├──────────────────────┤
│ 1283052.95 │
└──────────────────────┘A measure typed on its own gets a generated column name. To give it a readable one, or to write the whole query as code with its dimensions, measures and filters, use an explore block. It can run across several lines:
query> explore {
... dimensions { orders.status }
... measures { revenue: sum(products.price * order_items.quantity) }
... filters { orders.status in ['shipped', 'completed'] }
... };;
Result:
┌────────────────┬───────────┐
│ orders->status │ revenue │
├────────────────┼───────────┤
│ shipped │ 259979.76 │
│ completed │ 777199.73 │
└────────────────┴───────────┘To run a command while you're in query mode, start it with a backslash: \validate. To go back to command mode, type \c.
Query with SQL
SQL mode runs SQL straight on a connection, without going through a dataset. Use it to check the raw tables behind your models, with the same readable output and without leaving the session.
- Enter SQL mode with
\c query --lang sql. The prompt changes toquery sql>. - Set the connection with
\-s ecommerce_duckdb, using a name from.anfra/data_sources.yml. Until you do, the status line warns you:! --data-source is missing. Set it using \-s <value>. - Type your SQL. Enter adds a new line, as in AQL.
- End the query with a single
;and press Enter to run it. A;inside a string, as in'a;b', doesn't end it.
query sql> select status, count(*) as orders
... from public.orders
... group by 1;
Result:
┌───────────┬────────┐
│ status │ orders │
├───────────┼────────┤
│ shipped │ 301 │
│ completed │ 893 │
│ cancelled │ 306 │
└───────────┴────────┘
Has more: false
Timings:
connecting_to_dw 60 ms
open_connection 59 ms
executing_query 4 ms
fetching_query_result 8 ms
total 72 msA dataset set with \-d doesn't apply to SQL. If one is set, the status line says ! A SQL query runs against a data source, not a dataset. and tells you to unset it with \--dataset=.
Search the catalog
Search mode runs anfra search on each line you type, with no quotes needed:
> \c search
Type search's text, and press Enter to run it. \? helps, \c goes back.
search> revenueSearch reads the local catalog. Run anfra ingest first if you have changed your models since the last ingest.
Set flags for the session
A line made only of flags sets them for every command after it, so you don't repeat them:
| Type | What it does |
|---|---|
\-d ecommerce | Run every AQL query against the ecommerce dataset. |
\-s ecommerce_duckdb | Run every SQL query against the ecommerce_duckdb connection. |
\--json | Answer in JSON, indented. \--yaml answers in YAML instead. |
\--json= | Go back to the console's own layout. An empty value unsets any flag. |
\--input.timezone UTC | Pass --input.timezone UTC to every command that takes it, such as query. |
\--dataset= | Unset the dataset. |
\-- | List the flags set in this session. |
A flag you type on a single command still wins over the session's flag for that command.
Keys and commands
The keys and backslash commands you can use in a session, such as \? for help, \c to switch modes and \q to leave, are listed in the CLI reference.
Troubleshooting
anfra console needs a terminal. The console only runs in an interactive terminal, so it refuses when its input is piped or a script runs it. In a script, run each command as anfra <command>.
Alt+Enter adds a line instead of running the query. Your terminal may not send Alt+Enter to the console. End the query with ;; and press Enter instead.
Search finds nothing. Search reads the local catalog. Run anfra ingest to build it.
Related
- CLI reference: the anfra commands you can type in the console.
- Search: the syntax for search mode.
- Build your first app: test each query in the console before you put it in an app.