Can CockroachDB save the results of a query to a CSV file? - cockroachdb

Is there a way to export the results of a running reporting query, or just to get the contents of a given table, to a CSV file (e.g. that could then be imported by Excel or similar)?

You can export to CSV by running a SELECT statement from the command line with the --format=csv flag, e.g.:
cockroach sql --format=csv -e 'SELECT * FROM bank.customers;' > csvtest.csv

Related

How to create BiqQuery view from SQL source in a file (Windows command line)

I created a number of BigQuery views and all works well. I need to move the SQL source for the queries into my source control and manage changes from there. Is there a way to create/update a view from the command line using the source from a file? The bq mk command seems to only allow the SQL code to be inline on the command line --view keyword. Some of my views are quite lengthy and I'm sure there are characters that would need to be escaped - which I obviously don't want to get into. I'm running on Windows. Thanks
Simply use the flagfile parameter:
bq mk --help:
--flagfile: Insert flag definitions from the given file into the command line.
bq mk --view --flagfile=<path_to_to_your_file> dataset.newview
Let us assume that file MyQuery.sql contains view definition.
Create a Script file script.sh with below contents
query=`cat MyQuery.sql`
bq mk --use_legacy_sql=false --view "$query" dataset.myview
Run using command sh script.sh
This worked for me in Shell..!! You can make necessary changes for Windows..!!

How can I run a series of queries/commands at once in Vertica?

Currently I am using Vertica on Ubuntu from the terminal as dbadmin. I am using admintools to connect a database and then executing queries like Create Table, Select, Insert in the terminal.
Is there any way I can write the commands in any external text file and execute all the query at once? Like for Oracle, We can create a SQL file in Notepad++ and then run all the queries in the database.
Not only can you use scripts, but it's a good practice for any operation that you might need to repeat.
From the vsql prompt, use the \i command to run a script:
vsql> \i create_tables.sql
From outside the vsql prompt, you can invoke vsql with -f filename.
File paths, if not absolute, are relative to the current working directory.

Saving hive queries

I need to know how we can store a query I have written in a command line just like we do in sql(we use ctrl+S in sql server).
I heared hive QL queries use .q or .hql extension.Is there any possibility I save my query to get the same by saving list of commands I am executing.
sure whatever ide you use you can just save your file as myfile.q and then run it from the command line as
hive -f myfile.q
You can also do
hive -f myfile.q > myfileResults.log
if you want to pipe your results into a log file.
Create a new file using "cat" command(You can even use editor).Write all the queries you want to perform inside the file
$cat > MyQueries.hql
query1
query2
.
.
Ctrl+D
Note: .hql or .q is not necessary. It is just for our reference to identify that it is a hive query(file).
You can execute all the queries inside the file at a time using
$hive -f MyQueries.hql
You can use hue or web interface to access hive instead of terminal. It will provide you UI from where you can write and execute queries. Solves copy problem too.
http://gethue.com/
https://cwiki.apache.org/confluence/display/Hive/HiveWebInterface

Strange issue running hiveql using -e option from .sh file

I have checked Stackoverflow but could not find any help and that is the reason i m posting a new question.
Issue is related executing hiveql using -e option from .sh file.
If i run hive as $ bin/hive everything works fine and properly all databases and tables are displayed.
If i run hive as $ ./hive OR $ hive (as set in path variable) OR $HIVE_HOME/bin/hive only default database is displayed that too without any table information.
I m learning hive and trying to execute hive command using $HIVE_HOME/bin/hive -e from .sh file but it always give database not found.
So i understand that it is something related to reading of metadata but i m not able to understand why this kind of behavior.
However hadoop commands work fine from anywhere.
Below is one command i m trying to execute from .sh file
$HIVE_HOME/bin/hive -e 'LOAD DATA INPATH hdfs://myhost:8040/user/hduser/sample_table INTO TABLE rajen.sample_table'
Information:
I m using hive-0.13.0, hadoop-1.2.1
Can anybody pl explain me how to solve this or how to overcome this issue?
can you correct the query first, hive expect load statement path should be followed by quotes.
try this first from shell- HIVE_HOME/bin/hive -e "LOAD DATA INPATH '/user/hduser/sample_table' INTO TABLE rajen.sample_table"
or put your command in test.hql file and test $hive -f test.hql
--test.hql
LOAD DATA INPATH '/user/hduser/sample_table' INTO TABLE rajen.sample_table
I finally was able to fix the issue.
Issue was that i have kept the default derby setup of hive metadatastore_db , so from where ever i used to trigger hive -e command, it used to create a new copy of metadata_db copy.
So i created metadata store in mysql which became global and so now from where ever i trigger hive -e command, same metadata store db was being used.

Specifying path of CSV file in an SQL script (H2 database)

I have an H2 database file which is created by importing tables from CSV files, using the CSVREAD command provided by H2 (described here: http://www.h2database.com/html/tutorial.html#csv). The CSV files are just tables in text format, and are generated by Perl scripts. Every time I generate new versions of the CSV files, I have to delete the database tables and import the CSV files again to the database, using the CSVREAD command. This database is to be used by a webserver. But, instead of uploading the database file (which gets too big), I prefer to upload the CSV files to the server, and then execute a script (with the RUNSCRIPT command or from the command line, using the H2 RunScript tool), so that the database tables are generated inside the server.
My problem is that, to use the CSVREAD command inside a script, I have to specify the absolute path of the CSV files. Instead of writing the absolute path inside the script file, it would be convenient to be able to pass the absolute path as an argument to the RUNSCRIPT command, so that I don't have to hardcode it inside the script. Then, in the script file, I would just write a placeholder that would get replaced by the argument value. How can I do this?

Resources