Run SQLFlow Command-line Tool

In addition to the Jupyter Notebook magic command, we provide a command-line client sqlflow to connect and operate the SQLFlow gRPC server. Similar to other command-line client tools like mysql, sqflow implements several interaction modes.

  1. sqlflow -e "SELECT ... TO TRAIN ..." runs the SQLFlow program provided in the command line.
  2. sqlflow -f a.sql runs the SQLFlow program in a file.
  3. sqflow starts the REPL mode for user interaction.

In the REPL mode, sqlflow supports automatic code completion and other features.

Run with command-line tool - 图1

Installation

You can click below links to download the sqlflow binary according to your system type:

For Linux and macOS, after downloading the sqlflow binary, you need to change the file’s mode to allow execution. e.g.

  1. wget http://cdn.sqlflow.tech/latest/linux/sqlflow
  2. chmod +x sqlflow

Quick Start

We can run an example session by typing the following command on macOS. We assume you have installed our playground on your computer.

  • follow this guide to start your playground
  • connect to SQLFlow server according to the prompt message output by the playground, just do copy & paste, like:

    1. ./sqlflow --data-source="mysql://root:root@tcp(172.17.0.8)/?maxAllowedPacket=0"

You should be able to see the following:

  1. Welcome to SQLFlow. Commands end with ;
  2. sqlflow>

Suppose that we are going to train a DNNClassifier model.

Let’s go over some training data from the Iris database:

  1. sqlflow> SELECT * from iris.train limit 2;
  2. +--------------+-------------+--------------+-------------+-------+
  3. | SEPAL LENGTH | SEPAL WIDTH | PETAL LENGTH | PETAL WIDTH | CLASS |
  4. +--------------+-------------+--------------+-------------+-------+
  5. | 6.4 | 2.8 | 5.6 | 2.2 | 2 |
  6. | 5 | 2.3 | 3.3 | 1 | 1 |
  7. +--------------+-------------+--------------+-------------+-------+

Then we can train a TensorFlow DNNClassifier model using the following statement.

  1. SELECT * FROM iris.train
  2. TO TRAIN DNNClassifier
  3. WITH model.n_classes=3, model.hidden_units=[128,64],
  4. validation.select="SELECT * FROM iris.test"
  5. LABEL class
  6. INTO sqlflow_models.my_dnn_model;

To predict using the trained model, we can type the following statement.

  1. sqlflow> SELECT *
  2. FROM iris.test
  3. TO PREDICT iris.predict.class
  4. USING sqlflow_models.my_dnn_model;
  5. ...
  6. Done predicting. Predict table : iris.predict

We can then check the prediction result.

  1. sqlflow> SELECT * from iris.predict limit 3;
  2. ...
  3. +--------------+-------------+--------------+-------------+-------+
  4. | SEPAL LENGTH | SEPAL WIDTH | PETAL LENGTH | PETAL WIDTH | CLASS |
  5. +--------------+-------------+--------------+-------------+-------+
  6. | 6.3 | 2.7 | 4.9 | 1.8 | 2 |
  7. | 5.7 | 2.8 | 4.1 | 1.3 | 1 |
  8. | 5 | 3 | 1.6 | 0.2 | 0 |
  9. +--------------+-------------+--------------+-------------+-------+

Congratulations! Now you have successfully completed a session using SQLFlow syntax to train model using DNNClassifier and make a quick prediction.

Command-line Options

OptionEnvironment VariableDescription
-sqlflow_server <quoted-query-string>SQLFLOW_SERVERSpecify sqlflow server address, in host:port form, e.g. -sqlflow_server “localhost:50051”
-e <quoted-query-string> Execute from command line without entering interactive mode. e.g.
-e “SELECT * FROM iris.train TRAIN DNNClassifier…”
does the same thing as the training example above.
-f <filename> Execute from file without entering interactive mode. e.g.
-f ./my_sqlflow.sql
does the same thing as
< ./my_sqlflow.sql and cat ./my_sqlflow.sql | sqlflow. The special file - means read from standard input.
-datasource <database-connection-url>SQLFLOW_DATASOURCEConnect to the specified database. e.g. -datasource “mysql://root:root@tcp(localhost:3306)/“
-A No auto completion for sqlflow_models. This gives a quicker start.

Environment Variable Config File Setup

You can specify some of the options in a config file named .sqlflow_env under your home directory. This process is optional but can be convenient if you use the same config intensively. The content are exported as environment variables at run time. Be aware the config file is just a default setting, you can overwrite them via corresponding command-line options. Currently supported variables are listed in Environment Variable column in above table. You can setup the file using the following bash code.

  1. cat <<EOF >~/.sqlflow_env
  2. SQLFLOW_SERVER=localhost:50051
  3. SQLFLOW_DATASOURCE=mysql://root:root@tcp(localhost:3306)/?maxAllowedPacket=0
  4. EOF

Keyboard Shortcuts

Moving the cursor

Keyboard ShortcutAction
Ctrl + aGo to the beginning of the line (Home)
Ctrl + eGo to the end of the line (End)
Meta + bGo back one word
Meta + fGo forward one word
Ctrl + bGo back one character (Left arrow)
Ctrl + fGo forward one character (Right arrow)

Editing

Keyboard ShortcutAction
Ctrl + lClear the screen
Meta + DelCut the word before the cursor to the clipboard
Meta + dCut the word after the cursor to the clipboard
Ctrl + wCut the word before the cursor to the clipboard
Ctrl + dDelete the character under the cursor
Ctrl + hDelete the character before the cursor (Backspace)
Ctrl + kCut the line after the cursor to the clipboard
Ctrl + uCut the line before the cursor to the clipboard
Ctrl + yPaste the last thing to be cut (yank)
TABAuto completion for model/attributes names, navigate the pop-up menu

History

Keyboard ShortcutAction
Ctrl + rPop a menu of history commands including the specified character(s), the menu updates as you type
Ctrl/Meta + pShow the previous statement in history, or navigate the pop-up history menu (Up arrow)
Ctrl/Meta + nShow the Next statement in history, or navigate the pop-up history menu (Down arrow)
Meta + WSimilar to Ctrl + r but use wildcard to search history commands

Control

Keyboard ShortcutAction
Ctrl + LClear the screen
Ctrl + DExit (when no inputs)