4

I find that string-values displayed using cqlsh are right-aligned. Is there a reason for this? And is there a way to left-align strings?

cqlsh:test> create table test (id int, a ascii, t text, primary key(id));
cqlsh:test> insert into test (id, a, t) values (1, 'ascii', 'text');
cqlsh:test> insert into test (id, a, t) values (2, 'a', 't');       
cqlsh:test> select * from test;                              

 id | a     | t
----+-------+------
  1 | ascii | text
  2 |     a |    t

(2 rows)
tbsalling
  • 4,477
  • 4
  • 30
  • 51
  • Yeah this is annoying when you have some values that are really long and some that are really short. You have a massive gap between the data in consecutive columns. – Sridhar Sarnobat Jan 06 '17 at 21:57

2 Answers2

5

I think this is mostly done for aesthetic reasons, however you can change it!

cqlsh is simply a python file that uses the python-driver. You can simply change the following code in the print_formatted_result method of cqlsh:

    for row in formatted_values:                                                
        line = ' | '.join(col.rjust(w, color=self.color) for (col, w) in zip(row, widths))
        self.writeresult(' ' + line)

You can change col.rjust to ljust, center, etc. or you can simply change it to 'col' to print the data as is.

Example using ljust:

cqlsh:friends> select * from group_friends;

 groupid                              | friendid | time
--------------------------------------+----------+--------
 13814000-1dd2-11b2-8080-808080808080 | 1        | 123456
 13814000-1dd2-11b2-8080-808080808080 | 2        | 123456
 13814000-1dd2-11b2-8080-808080808080 | 4        | 123456
 13814000-1dd2-11b2-8080-808080808080 | 8        | 123456
 13814000-1dd2-11b2-8080-808080808080 | 22       | 123456
 13814000-1dd2-11b2-8080-808080808080 | 1002     | 123456
Andy Tolbert
  • 11,418
  • 1
  • 30
  • 45
  • 1
    To find the file to modify, use `whereis cqlsh`. It's usually `/usr/bin/cqlsh.py`. Once inside the file, search for `def print_formatted_result`. – Nato Boram Jul 16 '18 at 19:40
0

Try using the shell's column program to align columns:

$CASSANDRA_HOME/bin/cqlsh <<EOF | grep -v '+--'  | perl -pe 's{[ ]{4,}}{|}g' | column -t -s '|' | tee out.txt
    select mycol1,mycol2 from mykeyspace.mytable;
EOF
  1. Use a here document to send input to cqlsh
  2. Removing excess spaces with your favorite regex tool (but be careful not to remove them in your data)
  3. Align fields based on | as the separator / delimiter using the column program
  4. (Optional) Copy the output to a txt file using tee
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106