-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_superset2sparql.py
174 lines (119 loc) · 4.7 KB
/
flask_superset2sparql.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# flask_superset4sparql
# elasticsearch+http://192.168.3.133:5000
# source venv/bin/activate
# export FLASK_APP=prog_lang_app.py
# flask run --host=192.168.3.133 --debug
# http://192.168.3.189:8088/superset/welcome/
import requests
from flask import Flask
from flask import request
def sparql2es_results(response):
# create results table
results = response['results']['bindings']
rows = []
for row in results:
#print(row)
columns = []
for key, value in row.items():
#print(key, value['type'], value['value'])
val = value['value']
if value['type'] == 'literal':
if value['datatype'] == 'http://www.w3.org/2001/XMLSchema#int':
val = int(val)
if value['datatype'] == 'http://www.w3.org/2001/XMLSchema#decimal':
val = float(val)
#print(key, val, type(val))
columns.append(val)
rows.append(columns)
# create column name list with type
first_row = response['results']['bindings'][0]
column_names = []
for key, value in first_row.items():
#print(key, value['type'], value['value'])
if value['type'] == 'literal':
if value['datatype'] == 'http://www.w3.org/2001/XMLSchema#int':
type_name = "integer"
if value['datatype'] == 'http://www.w3.org/2001/XMLSchema#decimal':
type_name = "float"
else:
type_name = "text"
column = {
"name": key,
"type": type_name
}
column_names.append(column)
column_names
results = {
"columns": column_names,
"rows": rows
}
return results
app = Flask(__name__)
@app.post('/_sql')
def sql_query():
return {"columns":[{"name":"1","type":"integer"}],"rows":[[1]]}
@app.post('/_sql2/')
def sql_query2es():
content = request.get_json(silent=True)
print(content)
url = "http://es.kibio.science/_sql?format=json"
response = requests.post(url, json=content)
response_json = response.json()
#print(response_json)
return response_json
@app.post('/_sql/')
def sql_query2sparql():
content = request.get_json(silent=True)
query = content['query'].replace('\n',' ')
print('QUERY_IN', query)
query = query.replace('SELECT t AS t ','SELECT ?t ')
query = query.replace('SELECT p AS p ','SELECT ?p ')
query = query.replace(' ASC ',' ')
query = query.replace(' DESC ',' ')
#query = query.replace('SELECT c AS c, p AS p, t AS t ','SELECT ?c ?p ?t ')
query = query.replace('FROM (select','WHERE {select').replace(') AS virtual_table','}')
query = query.replace(' c AS c',' c').replace(' p AS p',' p').replace(' t AS t',' t')
query = query.replace(' c, ',' ?c ').replace(' p, ',' ?p ').replace(' t, ',' ?t ')
query = query.replace(' c ',' ?c ').replace(' p ',' ?p ').replace(' t ',' ?t ')
query = query.replace('sum(c) AS "SUM(c)"',' (sum(?c) AS ?zSUM_c) ')
query = query.replace('"SUM(c)"','?zSUM_c')
if query.find('IN (') != -1:
query = query.replace("WHERE ?p IN ('http://xmlns.com/foaf/0.1/name')",'FILTER(?p = <http://xmlns.com/foaf/0.1/name>)}')
query = query.replace("WHERE ?t IN ('http://bio2rdf.org/bibtex#Article')",'FILTER(?t = <http://bio2rdf.org/bibtex#Article>)}')
query = query.replace("WHERE {select",'WHERE {{select')
content['query'] = query
print('QUERY_OUT', content['query'])
if content['query'][0:4] == 'SHOW':
url = 'http://es.kibio.science/_sql?format=json'
response = requests.post(url, json=content)
response_json = response.json()
#print(response_json)
else:
url = "http://192.168.3.190:7006/"
response = requests.get(url, params=content)
#print(response.json())
response_json = sparql2es_results(response.json())
#print(response_json)
return response_json
@app.get('/_cat/indices')
def cat_indices():
url = "http://es.kibio.science/_cat/indices?format=json"
response = requests.get(url)
response_json = response.json()
return response_json
@app.get('/')
def home():
url = "http://es.kibio.science/"
# A GET request to the API
response = requests.get(url)
# Print the response
response_json = response.json()
#print(response_json)
return response_json
@app.route('/<string:index_name>/_search', methods=['POST'])
def post_home(index_name):
url = "http://es.kibio.science/"+index_name+"/_search?size=1"
response = requests.post(url)
response_json = response.json()
#print(response_json)
return response_json