-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassandraToExcel.py
109 lines (81 loc) · 3.23 KB
/
cassandraToExcel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Program that will write cassandra table in excel workbook
"""
from openpyxl import Workbook
from openpyxl import load_workbook
import os
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import SimpleStatement
dir_excel='C:\\quartExcel\\cassandraThesis.xlsx'
current_dir = os.getcwd()
def main():
dexist=False
dexist=os.path.exists('C:\\quartExcel')
if dexist==False:
os.mkdir('C:\\quartExcel')
wb = Workbook()
ws = wb.active
ws.title = "Thesis"
#Get cassandra columns
query="select column_name from system_schema.columns WHERE keyspace_name = 'thesis' AND table_name = 'tbthesis';"
columns_list=''
columns_list=cassandraBDProcess(True,query)
coln=1
for col in columns_list:
#Write(row,column)
#Headers (h1,...)
h1 = ws.cell(row = 1, column = coln)
h1.value = col[0]
coln=coln+1
wb.save(dir_excel)
#Starts the reading of periods
for i in range(10,11):
flag=os.path.isfile(dir_excel)
if flag:
#Expedient xls already exists
print('Printing period number:',str(i))
resultSet=''
query="select book_number,dt_publication_date,heading,id_thesis,instance,jurisprudence_type,lst_precedents,multiple_subjects,page,period,period_number,publication,publication_date,source,subject,subject_1,subject_2,subject_3,text_content,thesis_number,type_of_thesis from thesis.tbthesis where period_number="+str(i)+""
resultSet=cassandraBDProcess(False,query)
print('The excel is ready!')
def cassandraBDProcess(isShortQuery,query):
global current_dir
#Connect to Cassandra
objCC=CassandraConnection()
cloud_config= {
'secure_connect_bundle':current_dir+'\\secure-connect-dbquart.zip'
}
auth_provider = PlainTextAuthProvider(objCC.cc_user,objCC.cc_pwd)
res=''
if isShortQuery:
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()
session.default_timeout=70
#Check wheter or not the record exists
future = session.execute_async(query)
res=future.result()
cluster.shutdown()
else:
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()
session.default_timeout=70
statement = SimpleStatement(query, fetch_size=1000)
wb = load_workbook(dir_excel)
ws = wb['Thesis']
for row in session.execute(statement):
ls=[]
for r in row:
ls.append(str(r))
ws.append(ls)
wb.save(dir_excel)
cluster.shutdown()
res=''
return res
class CassandraConnection():
cc_user='quartadmin'
cc_keyspace='thesis'
cc_pwd='P@ssw0rd33'
cc_databaseID='9de16523-0e36-4ff0-b388-44e8d0b1581f'
if __name__=='__main__':
main()