私はPython-cassandraドライバを使ってApache Casandraに接続しようとしているPythonプログラムをいくつか実行しています。私が戻ってきた結果セットはリストか辞書です。しかし、私はこのようなPythonの印刷NQL形式のCQL出力
(列と行として)出力はネイティブCQLの出力形式に戻って、ユーザに印刷することにしたい、
は、私はこれを達成するために使用できるモジュールはありますか?私は、これはPythonのドライバ自体内の機能であるとは考えていない2.7
私はPython-cassandraドライバを使ってApache Casandraに接続しようとしているPythonプログラムをいくつか実行しています。私が戻ってきた結果セットはリストか辞書です。しかし、私はこのようなPythonの印刷NQL形式のCQL出力
(列と行として)出力はネイティブCQLの出力形式に戻って、ユーザに印刷することにしたい、
は、私はこれを達成するために使用できるモジュールはありますか?私は、これはPythonのドライバ自体内の機能であるとは考えていない2.7
:私は
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import dict_factory
def fireCql(cqlStatement):
auth_provider = PlainTextAuthProvider(
username='myusername', password='myPassWord')
cluster = Cluster(['cassandra.myhostname.com'],auth_provider=auth_provider)
session = cluster.connect('my_keyspace')
session.row_factory = dict_factory
rows = session.execute(cqlStatement)
return rows
のPythonをテストするには、次のコードを使用します。多くの人がこれを望むと思うなら、pythonドライバグループに要求することができます。
ただし、Cassandraがその書式を生成するために使用するコードは次のとおりです。これをベースとして使用して、必要に応じて結果とフォーマットを解析するロジックを実装することができます。 https://github.com/apache/cassandra/blob/b02dec3da539d181544cfb0288102d6073f36264/bin/cqlsh.py
def print_formatted_result(self, formatted_names, formatted_values):
# determine column widths
widths = [n.displaywidth for n in formatted_names]
if formatted_values is not None:
for fmtrow in formatted_values:
for num, col in enumerate(fmtrow):
widths[num] = max(widths[num], col.displaywidth)
# print header
header = ' | '.join(hdr.ljust(w, color=self.color) for (hdr, w) in zip(formatted_names, widths))
self.writeresult(' ' + header.rstrip())
self.writeresult('-%s-' % '-+-'.join('-' * w for w in widths))
# stop if there are no rows
if formatted_values is None:
self.writeresult("")
return
# print row data
for row in formatted_values:
line = ' | '.join(col.rjust(w, color=self.color) for (col, w) in zip(row, widths))
self.writeresult(' ' + line)
self.writeresult("")
から