问题
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)
回答1:
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
回答2:
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
- Use a here document to send input to
cqlsh
- Removing excess spaces with your favorite regex tool (but be careful not to remove them in your data)
- Align fields based on
|
as the separator / delimiter using thecolumn
program - (Optional) Copy the output to a txt file using
tee
来源:https://stackoverflow.com/questions/28848315/why-does-cqlsh-right-align-strings