【问题标题】:Why does cqlsh right-align strings?为什么 cqlsh 右对齐字符串?
【发布时间】:2015-05-05 01:53:34
【问题描述】:

我发现使用 cqlsh 显示的字符串值是右对齐的。是否有一个原因?有没有办法让字符串左对齐?

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)

【问题讨论】:

  • 是的,当您有一些非常长的值和一些非常短的值时,这很烦人。连续列中的数据之间存在巨大差距。

标签: cassandra cql cqlsh


【解决方案1】:

我认为这主要是出于审美原因,但是您可以更改它!

cqlsh 只是一个使用 python-driver 的 python 文件。您可以在cqlsh的print_formatted_result方法中简单地更改以下代码:

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

您可以将 col.rjust 更改为 ljust、center 等,或者您可以简单地将其更改为 'col' 以按原样打印数据。

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

【讨论】:

  • 要查找要修改的文件,请使用whereis cqlsh。通常是/usr/bin/cqlsh.py。进入文件后,搜索def print_formatted_result
【解决方案2】:

尝试使用 shell 的 column 程序来对齐列:

$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. 使用此处的文档将输入发送到cqlsh
  2. 使用您最喜欢的正则表达式工具删除多余的空格(但注意不要在您的数据中删除它们)
  3. 使用column 程序以| 作为分隔符/定界符对齐字段
  4. (可选)使用tee 将输出复制到txt 文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2012-01-04
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多