0
我有以下代碼來列出Apache Cassandra數據庫(Rest Api)的密鑰空間。問題是使用2.0完成的Apache Cassandra數據庫的代碼版本。修復Cassandra Python RestApi
從schema_keyspaces在3.0版中不工作,選擇keyspace_name如果我替換查詢:SELECT * FROM,在3.0版本的作品system_schema.keyspaces我有以下錯誤:
from math import ceil
class Pagination(object):
def __init__(self, page, per_page, total_count):
self.page = page
self.per_page = per_page
self.total_count = total_count
@property
def pages(self):
return int(ceil(self.total_count/float(self.per_page)))
@property
def has_prev(self):
return self.page > 1
@property
def has_next(self):
return self.page < self.pages
def iter_pages(self, left_edge=2, left_current=2,
right_current=5, right_edge=2):
last = 0
for num in xrange(1, self.pages + 1):
if num <= left_edge or \
(num > self.page - left_current - 1 and \
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num
@app.route('/keyspaces/', defaults={'page': 1}, methods=['GET'])
@app.route('/keyspaces/page/<int:page>', methods=['GET'])
def getKeyspaces(page):
auth_provider = PlainTextAuthProvider(username='admin',
password='root')
cluster = Cluster(['hostname'],
auth_provider=auth_provider)
session = cluster.connect()
rows = session.execute('select keyspace_name from schema_keyspaces')
keyspaces = []
for row in range(len(rows)):
keyspaces.append(rows[row][0])
pages = keyspaces[(page - 1) * PER_PAGE:PER_PAGE * page]
if not pages and page != 1:
abort(404)
pagination = Pagination(page, PER_PAGE, len(rows))
return render_template('listkeyspace.html',
pagination=pagination, pages=pages,
section='getKeyspaces')
當我按照你說的去做時,我又犯了一個錯誤: RuntimeError:迭代結果時不能使用索引運算符。 On keyspaces.append(rows [row] [0]) –
Right。如上所述,您還需要在整個循環體中將'rows'的所有實例更改爲'row_list'。所以'keyspaces.append(rows [row] [0])''應該成爲'keyspaces.append(row_list [row] [0])' –
好,它是行得通的,謝謝我的朋友;) –