Why does cqlsh right-align strings?

a 夏天 提交于 2019-12-14 03:53:54

问题


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
  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


来源:https://stackoverflow.com/questions/28848315/why-does-cqlsh-right-align-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!