Execute each query in an Impala query file individually
(file_name)
| 175 | |
| 176 | # KERBEROS TODO: fails when kerberized and impalad principal isn't "impala" |
| 177 | def exec_impala_query_from_file(file_name): |
| 178 | """Execute each query in an Impala query file individually""" |
| 179 | if not os.path.exists(file_name): |
| 180 | LOG.info("Error: File {0} not found".format(file_name)) |
| 181 | return False |
| 182 | |
| 183 | LOG.info("Beginning execution of impala SQL on {0}: {1}".format( |
| 184 | options.impalad, file_name)) |
| 185 | is_success = True |
| 186 | impala_client = ImpylaHS2Connection(HS2_HOST_PORT, |
| 187 | use_kerberos=options.use_kerberos) |
| 188 | output_file = file_name + ".log" |
| 189 | query = None |
| 190 | with open(output_file, 'w') as out_file: |
| 191 | try: |
| 192 | impala_client.connect() |
| 193 | with open(file_name, 'r+') as query_file: |
| 194 | queries = sqlparse.split(query_file.read()) |
| 195 | for query in queries: |
| 196 | query = sqlparse.format(query.rstrip(';'), strip_comments=True) |
| 197 | if query.strip() != "": |
| 198 | result = impala_client.execute(query) |
| 199 | out_file.write("{0}\n{1}\n".format(query, result)) |
| 200 | except Exception as e: |
| 201 | if query: |
| 202 | out_file.write("ERROR: {0}\n".format(query)) |
| 203 | else: |
| 204 | out_file.write("Encounter errors before parsing any queries.\n") |
| 205 | traceback.print_exc(file=out_file) |
| 206 | is_success = False |
| 207 | |
| 208 | if is_success: |
| 209 | LOG.info("Finished execution of impala SQL: {0}".format(file_name)) |
| 210 | else: |
| 211 | LOG.info("Error executing impala SQL: {0} See: {1}".format(file_name, \ |
| 212 | output_file)) |
| 213 | |
| 214 | return is_success |
| 215 | |
| 216 | def run_dataset_preload(dataset): |
| 217 | """Execute a preload script if present in dataset directory. E.g. to generate data |