SQLShell: A Cross-Database SQL Tool With NoSQL Potential
Join the DZone community and get the full member experience.Join For Free
in this blog post i will introduce sqlshell and demonstrate, step-by-step, how to install it and start using it with mysql. i will also reflect on the possibilites of using this with nosql technologies, such as hbase, mongodb, hive, couchdb, redis and google bigquery.
sqlshell is a cross-platform, cross-database command-line tool for sql, much like psql for postgresql or the mysql command-line tool for mysql.
why use it?
if you already use only one command-line tool, such as postgresql’s psql or mysql’s mysql tool and are happy then you may not need to. if you find yourself jumping between several of these tools and would like common functionality, or you are a fan of the many nosql technologies out there, then this is worth keeping an eye on.
jdbc driver support
sqlshell is built in scala . scala is a scalable programming language that compiles to java byte code, can run on the jvm and can be used alongside java code. therefore, sqlshell can interface to any database for which there is a jdbc (java database connectivity) driver. encase you do not know, there are many.
jdbc drivers available
there are several jdbc drivers for many nosql technologies, but none of these implement jdbc fully enough to sqlshell, yet .
something to note about google’s recent bigquery (part of google storage ) is that, while they do not have a jdbc driver, yet, they do internally make use of sqlcmd with bigquery. sqlcmd is also created by brian m. clapper at clapper.org , but he has discontinued development of it in favor of the new sqlshell. therefore, i think there is a good chance that google may soon develop a jdbc driver compatible with sqlshell. that said, google does like python and sqlcmd is written in python.
brian m. clapper, author of sqlshell, writes about the move from python to scala in the sqlshell user-guide .
sqlshell is a sql command line tool, similar in concept to tools like oracle’s sql plus, the postgresql psql command, and the mysql mysql tool.
sqlshell is a scala rewrite of my python sqlcmd tool (rewritten because, as it turns out, i think jdbc is more consistent and portable than python’s db api).
you can download a pre-compiled installation jar file for sqlshell or compile the binary yourself . downloading the jar is recommended and i have download version 0.7.1
curl -o http://cloud.github.com/downloads/bmc/sqlshell/sqlshell-0.7.1-install.jar
running this installer can be done with the java command, which will launch a graphical installer. the graphical installer uses izpack , which is a cross-platform installation framework. therefore, even though i am using mac os x, you should not have any problems installing on windows or linux.
java -jar sqlshell-0.7.1-install.jar
first thing you see if all goes well is the language prompt. i am speak the english.
following this you will see a welcome screen, then a intro and info page, and then the license page. be sure to read the license carefully – especially the part about “your first born child”.
here is what you will see for the intro screen.
when you get to the end, you will see the following message…
you’ve successfully installed sqlshell.
for your convenience, a command-line wrapper script has been provided in
the “bin” directory under “/applications/clapper.org/sqlshell”.
that tells you where sqlshell is installed. under this directory is the bin directory, which contains sqlshell . it’s a command-line tool, and since we will be using it often, i’ll add the path of that bin directory to my path.
echo 'export path=$path:/applications/clapper.org/sqlshell/bin' >> ~/.profile source ~/.profile which sqlshell || echo "not found in path" # this tests that it's found in your path
we can see what parameters it expects, by running sqlshell with the –help argument.
sqlshell --help sqlshell, version 0.7.1 (2010/11/10 17:27:55) usage: sqlshell [options] db [@file] options -? -h --help show this usage message. -v --version show version and exit. -c config_file --config config_file specify configuration file. defaults to: /users/phil/.sqlshell/config -n --no-ansi --noansi disable the use of ansi terminal sequences. this option just sets the initial value for this setting. the value can be changed later from within sqlshell itself. -r lib_name --readline lib_name specify readline libraries to use. legal values: editline, getline, gnu, jline, simple. (may be specified multiple times.) -s --stack show all exception stack traces. -v --verbose enable various verbose messages. this option just sets the initial verbosity value. the value can be changed later from within sqlshell itself. parameters db name of database to which to connect, or an on-the-fly database specification, of the form: driver,url,[user[,password]] if the name of a database is specified, sqlshellwill look in the configuration file for the corresponding connection parameters. if a database specification is used, the specification must one argument. the driver can be a full driver class name, or a driver alias from the configuration file. the user and password are optional, since some databases (like sqlite) don't require them at all. @file path of file of commands to run
sqlshell with mysql
install mysql’s jdbc driver
download the driver from mysql’s website .
curl -o <a href="http://www.mysql.com/downloads/connector/j/">http://mysql.mirror.iweb.ca/downloads/connector-j/mysql-connector-java-5.1.14.tar.gz</a> tar xvzf mysql-connector-java-5.1.14.tar.gz cd mysql-connector-java-5.1.14 cp mysql-connector-java-5.1.14-bin.jar /applications/clapper.org/sqlshell/lib/
above, we copied the mysql-connector-java-5.1.14-bin.jar file to sqlshell’s lib . sqlshell will load all the jar files found in that directory at start-up, and will then reference them by package and class name. we will use the class name of the mysql driver to configure a “mysql” alias in the sqlshell configuration.
to configure the “mysql” alias we will edit the default configuration file ~/.sqlshell/config , which currently does not exist.
mkdir ~/.sqlshell vim ~/.sqlshell/config # i use vim to edit files
add the following configuration…
[drivers] mysql = com.mysql.jdbc.driver
connect to mysql using sqlshell
the format of your connection string for the mysql jdbc driver should be
i’m just going to connect to the local mysql database and look at the pre-installed mysql database called “test”. you should have the same database, unless you deleted it.
sqlshell, version 0.7.1 (2010/11/10 17:27:55) copyright (c) 2009-2010 brian m. clapper using jline type "help" for help. type ".about" for more information. sqlshell>
ok, we are connected to mysql and can start running some queries.
we can see the commands that we can run using the help command.
sqlshell> help help is available for the following commands: ------------------------------------------------------------------------------- .about .capture .desc .echo .run .set .show alter begin commit create delete drop exit help history insert r rollback select update
let’s see what databases we have…
sqlshell> show databases; execution time: 0.26 seconds retrieval time: 0.21 seconds 2 rows returned. schema_name ------------------ information_schema test sqlshell>
we can create an new database.
sqlshell> create database sqlshell_test; 1 row affected. execution time: 0.5 seconds
sqlshell passes sql statements in full to the database, so you can use any commands that your database understands
the initial purpose of this blog post was to demonstrate sqlshell with a nosql database, but unfortunately i failed to find a jdbc driver that implemented all the features that sqlshell requires. i’m hoping that development of such drivers will continue, as it will be good to have standard inferface to all these nosql technologies directly from the command-line. i look forward to seeing how sqlshell and these jdbc develop over time.
Opinions expressed by DZone contributors are their own.
How To Become a 10x Dev: An Essential Guide
Merge GraphQL Schemas Using Apollo Server and Koa
The Internet of Java: Eclipse's Open IoT Stack for Java
Creating Scalable OpenAI GPT Applications in Java