You can use the CLI to issue queries and output results. The following instructions show you how to format the query output results using the CLI.
To output the result of a single query to a MySQL server add the --result option to the td query command. After the job is finished, the results are written into your database:
$ td query --result 'mysql://user:password@host/database/table' \
-w -d testdb \
"SELECT code, COUNT(1) FROM www_access GROUP BY code"To create a scheduled query whose output is systematically written to MySQL add the --result option when creating the schedule through td sched:create command. Every time the job runs, the results are written into mytbl.
$ td result:create mydb mysql://user:password@host/database
$ td sched:create hourly_count_example "0 * * * *" -d testdb "select count(*) from www_access" --result mydb:mytblThe result output target is represented by URL with the following format:
mysql://username:password@hostname:port/database/tablewhere:
- mysql is identified for result output to MySQL;
- username and password are the credentials to the MySQL server;
- hostname is the host name of theMySQL server;
- port is the port number through which theMySQL server is accessible. “:” is optional and assumed to be 3306 by default;
- database is the name of the destination database;
- table is the name of a table within the above mentioned database. It may not exist at the moment the query output is execute, in which case a table with the specified name will be created for the user.
Writing Job Results to the MySQL server supports the following options.
You can add or delete data in four ways.
mysql://user:password@host/database/table # append
mysql://user:password@host/database/table?mode=append # append
mysql://user:password@host/database/table?mode=replace # replace
mysql://user:password@host/database/table?mode=truncate # truncate
mysql://user:password@host/database/table?mode=update # updateAll four modes are atomic.
In the “host” section, you can specify the port as well. Here is an example:
mysql://superadmin:superstrongpassword@data.science.com:27013/transaction/user_infoThis is the default mode. The query results are appended to a table. If the table does not exist, a new table will be created.
If the table already exists, the rows of the existing table are replaced with the query results. If the table does not exist, a new table will be created.
We achieve atomicity (so that a consumer of the table always has consistent data) by performing the following three steps in a single transaction.
- Create a temporary table.
- Write to the temporary table.
- Replace the existing table with the temporary table using ALTER TABLE RENAME.
Using replace mode may change the schema of the MySQL table. To avoid schema changes to the target table, select 'Preserve schema?' option.
The system first truncates the existing table, then inserts the query results. If the table does not exist, a new table will be created.
Unlike REPLACE, TRUNCATE retains the indexes of your table.
This mode inserts new data and update existing data by performing the following steps.
- Create a temporary table.
- Write to the temporary table.
- Update data from temporary table to the destination table.
- Insert the rest of the data from temporary table to the destination table.
To specify a unique key, you can set unique parameter, which is comma-separated list of keys. If the values for all keys match all existing record, that record will be replaced. Here is an example:
mysql://superadmin:superstrongpassword@data.science.com:27013/transaction/user_infomode=update&unique=column1%2Ccolumn2 On CLI, Comma in the paramter must be URL encoded. ("," should be "%2C")
Writing Job Results to the MySQL server supports following additional options that can be specified as optional URL parameters.
ssl option determines whether to use SSL or not for connecting to the MySQL server.
Use SSL from Treasure Data to the MySQL server connection. The MySQL server must be configured to accept SSL connection.
mysql://user:password@host/database/table?ssl=trueDo not use SSL from Treasure Data to the MySQL server.
mysql://user:password@host/database/table?ssl=falseTreasure Data always uses UTF-8 as character encoding. You also need to setup your database to use UTF-8 by default. If you came across any encoding related issues, check the following configurations.
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+Job Results into MySQL doesn't support INT UNSIGNED type of MySQL. Its value will be converted to an unexpected value.