@Override public void addResults(QueryResults results) { if (types.get() == null && results.getColumns() != null) { types.set(getTypes(results.getColumns())); } if (results.getData() != null) { checkState(types.get() != null, "Data without types received!"); List<Column> columns = results.getColumns(); for (List<Object> fields : results.getData()) { ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); for (int i = 0; i < fields.size(); i++) { Type type = types.get().get(i); Object value = convertValue(fields.get(i), type); if (value != null) { builder.put(columns.get(i).getName(), value); } } producer.send(new KeyedMessage<>(topicName, count.getAndIncrement(), builder.build())); } } }
@POST @Produces(MediaType.APPLICATION_JSON) public Response createQuery(String query, @Context HttpServletRequest servletRequest) { assertRequest(!isNullOrEmpty(query), "SQL query is empty"); Session session = createSessionForRequest(servletRequest, accessControl, sessionPropertyManager, queryIdGenerator.createNextQueryId()); ClientSession clientSession = session.toClientSession(serverUri(), false, new Duration(2, MINUTES)); StatementClient client = new StatementClient(httpClient, queryResultsCodec, clientSession, query); List<Column> columns = getColumns(client); Iterator<List<Object>> iterator = flatten(new ResultsPageIterator(client)); SimpleQueryResults results = new SimpleQueryResults(columns, iterator); return Response.ok(results, MediaType.APPLICATION_JSON_TYPE).build(); }
private static List<Column> createColumnsList(QueryInfo queryInfo) { requireNonNull(queryInfo, "queryInfo is null"); StageInfo outputStage = queryInfo.getOutputStage(); requireNonNull(outputStage, "outputStage is null"); List<String> names = queryInfo.getFieldNames(); List<Type> types = outputStage.getTypes(); checkArgument(names.size() == types.size(), "names and types size mismatch"); ImmutableList.Builder<Column> list = ImmutableList.builder(); for (int i = 0; i < names.size(); i++) { String name = names.get(i); TypeSignature typeSignature = types.get(i).getTypeSignature(); String type = typeSignature.toString(); list.add(new Column(name, type, new ClientTypeSignature(typeSignature))); } return list.build(); }
private static List<Column> getColumns(StatementClient client) throws SQLException { while (client.isValid()) { List<Column> columns = client.current().getColumns(); if (columns != null) { return columns; } client.advance(); } QueryResults results = client.finalResults(); if (!client.isFailed()) { throw new SQLException(format("Query has no columns (#%s)", results.getId())); } throw resultsException(results); }
@JsonCreator public Job(@JsonProperty("user") final String user, @JsonProperty("query") final String query, @JsonProperty("uuid") final UUID uuid, @JsonProperty("output") final PersistentJobOutput output, @JsonProperty("queryStats") final QueryStats queryStats, @JsonProperty("state") final JobState state, @JsonProperty("columns") final List<Column> columns, @JsonProperty("tablesUsed") final Set<Table> tablesUsed, @JsonProperty("queryStarted") final DateTime queryStarted, @JsonProperty("error") final QueryError error, @JsonProperty("queryFinished") final DateTime queryFinished) { this.user = user; this.query = query; this.uuid = uuid; this.output = output; this.queryStats = queryStats; this.state = state; this.columns = columns; this.tablesUsed = tablesUsed; this.queryStarted = queryStarted; this.error = error; this.queryFinished = queryFinished; }
public Job(final String user, final String query, final UUID uuid, final PersistentJobOutput output, final QueryStats stats, final JobState state, final List<Column> columns, final QueryError error, final DateTime queryFinished) { this(user, query, uuid, output, stats, state, columns, Sets.<Table>newConcurrentHashSet(), new DateTime(), error, queryFinished ); }
@Override public TableRow map(int index, ResultSet r, StatementContext ctx) throws SQLException { try { return new TableRow( r.getLong("id"), r.getString("connector_id"), r.getString("schema_"), r.getString("table_"), objectMapper.<List<Column>>readValue(r.getString("columns"), columnTypeReference)); } catch (IOException e) { log.error("Caught exception mapping TableRow", e); return null; } }
protected Function<Column, Type> columnTypeGetter() { return column -> { String typeName = column.getType(); Type type = prestoServer.getMetadata().getType(parseTypeSignature(typeName)); if (type == null) { throw new AssertionError("Unhandled type: " + typeName); } return type; }; }
private static List<Column> getColumns(StatementClient client) { while (client.isValid()) { List<Column> columns = client.current().getColumns(); if (columns != null) { return columns; } client.advance(); } if (!client.isFailed()) { throw internalServerError("No columns"); } throw internalServerError(failureMessage(client.finalResults())); }
PrestoResultSet(StatementClient client) throws SQLException { this.client = requireNonNull(client, "client is null"); this.sessionTimeZone = DateTimeZone.forID(client.getTimeZoneId()); this.queryId = client.current().getId(); List<Column> columns = getColumns(client); this.fieldMap = getFieldMap(columns); this.columnInfoList = getColumnInfo(columns); this.resultSetMetaData = new PrestoResultSetMetaData(columnInfoList); this.results = flatten(new ResultsPageIterator(client)); }
private static Map<String, Integer> getFieldMap(List<Column> columns) { Map<String, Integer> map = new HashMap<>(); for (int i = 0; i < columns.size(); i++) { String name = columns.get(i).getName().toLowerCase(ENGLISH); if (!map.containsKey(name)) { map.put(name, i + 1); } } return ImmutableMap.copyOf(map); }
private void doRenderResults(PrintStream out, OutputFormat format, boolean interactive, List<Column> columns) throws IOException { List<String> fieldNames = Lists.transform(columns, Column::getName); if (interactive) { pageOutput(format, fieldNames); } else { sendOutput(out, format, fieldNames); } }
@Override public void addColumns(List<Column> columns) throws FileTooLargeException { if (!headerWritten && includeHeader) { List<String> columnNames = Lists.transform(columns, Column::getName); writeCsvRow(columnNames.toArray(new String[columnNames.size()])); headerWritten = true; } }
private List<HiveColumn> queryColumns(String query) { final ImmutableList.Builder<HiveColumn> cache = ImmutableList.builder(); QueryRunner queryRunner = queryRunnerFactory.create(); QueryClient queryClient = new QueryClient(queryRunner, io.dropwizard.util.Duration.seconds(60), query); try { queryClient.executeWith(new Function<StatementClient, Void>() { @Nullable @Override public Void apply(StatementClient client) { QueryResults results = client.current(); if (results.getData() != null) { for (List<Object> row : results.getData()) { Column column = new Column((String) row.get(0), (String) row.get(1), new ClientTypeSignature(TypeSignature.parseTypeSignature((String)row.get(1)))); boolean isNullable = (Boolean) row.get(2); boolean isPartition = (Boolean) row.get(3); cache.add(HiveColumn.fromColumn(column, isNullable, isPartition)); } } return null; } }); } catch (QueryClient.QueryTimeOutException e) { log.error("Caught timeout loading columns", e); } return cache.build(); }
/** * Rate Limited updateJobInfo */ protected void rlUpdateJobInfo( Set<Table> usedTables, List<Column> columns, QueryStats queryStats, JobState state, QueryError error, List<List<Object>> outputPreview) { if (updateLimiter.tryAcquire(1)) { updateJobInfo(usedTables, columns, queryStats, state, error, outputPreview, true); } else { updateJobInfo(usedTables, columns, queryStats, state, error, outputPreview, false); } }
private static List<Column> createColumnsList(QueryInfo queryInfo) { checkNotNull(queryInfo, "queryInfo is null"); StageInfo outputStage = queryInfo.getOutputStage(); if (outputStage == null) { checkNotNull(outputStage, "outputStage is null"); } List<String> names = queryInfo.getFieldNames(); ArrayList<Type> types = new ArrayList<>(); for (TupleInfo tupleInfo : outputStage.getTupleInfos()) { types.addAll(tupleInfo.getTypes()); } checkArgument(names.size() == types.size(), "names and types size mismatch"); ImmutableList.Builder<Column> list = ImmutableList.builder(); for (int i = 0; i < names.size(); i++) { String name = names.get(i); Type type = types.get(i); switch (type) { case BOOLEAN: list.add(new Column(name, "boolean")); break; case FIXED_INT_64: list.add(new Column(name, "bigint")); break; case DOUBLE: list.add(new Column(name, "double")); break; case VARIABLE_BINARY: list.add(new Column(name, "varchar")); break; default: throw new IllegalArgumentException("unhandled type: " + type); } } return list.build(); }
@Override public void addResults(QueryResults results) { if (types.get() == null && results.getColumns() != null) { types.set(getTypes(results.getColumns())); } if (results.getData() != null) { checkState(types.get() != null, "Data without types received!"); List<Column> columns = results.getColumns(); for (List<Object> fields : results.getData()) { String redisKey = tableName + ":" + count.getAndIncrement(); try (Jedis jedis = jedisPool.getResource()) { switch (dataFormat) { case "string": ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); for (int i = 0; i < fields.size(); i++) { Type type = types.get().get(i); Object value = convertValue(fields.get(i), type); if (value != null) { builder.put(columns.get(i).getName(), value); } } jedis.set(redisKey, jsonEncoder.toString(builder.build())); break; case "hash": // add keys to zset String redisZset = "keyset:" + tableName; jedis.zadd(redisZset, count.get(), redisKey); // add values to Hash for (int i = 0; i < fields.size(); i++) { jedis.hset(redisKey, columns.get(i).getName(), fields.get(i).toString()); } break; default: throw new AssertionError("unhandled value type: " + dataFormat); } } } } }
protected List<Type> getTypes(List<Column> columns) { return ImmutableList.copyOf(transform(columns, columnTypeGetter())); }
public SimpleQueryResults(List<Column> columns, Iterator<List<Object>> data) { this.columns = requireNonNull(columns, "columns is null"); this.data = requireNonNull(data, "data is null"); }
@JsonProperty public List<Column> getColumns() { return columns; }
@GET @Path("queries") public Response getUserQueries( @Auth AirpalUser user, @PathParam("id") String userId, @QueryParam("results") int numResults, @QueryParam("table") List<PartitionedTable> tables) { Iterable<Job> recentlyRun; int results = Optional.of(numResults).or(0); if (results <= 0) { results = 100; } if (tables.size() < 1) { recentlyRun = jobHistoryStore.getRecentlyRunForUser(userId, results); } else { recentlyRun = jobHistoryStore.getRecentlyRunForUser( userId, results, Iterables.transform(tables, new PartitionedTableToTable())); } ImmutableList.Builder<Job> filtered = ImmutableList.builder(); for (Job job : recentlyRun) { if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) { filtered.add(job); continue; } for (Table table : job.getTablesUsed()) { if (AuthorizationUtil.isAuthorizedRead(user, table)) { filtered.add(new Job( job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime())); } } } List<Job> sortedResult = Ordering .natural() .nullsLast() .onResultOf(JOB_ORDERING) .reverse() .immutableSortedCopy(filtered.build()); return Response.ok(sortedResult).build(); }
@GET public Response getQueries( @Auth AirpalUser user, @QueryParam("results") int numResults, @QueryParam("table") List<PartitionedTable> tables) { Iterable<Job> recentlyRun; int results = Optional.of(numResults).or(200); if (tables.size() < 1) { recentlyRun = jobHistoryStore.getRecentlyRun(results); } else { recentlyRun = jobHistoryStore.getRecentlyRun( results, Iterables.transform(tables, new PartitionedTable.PartitionedTableToTable())); } ImmutableList.Builder<Job> filtered = ImmutableList.builder(); for (Job job : recentlyRun) { if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) { filtered.add(job); continue; } for (Table table : job.getTablesUsed()) { if (AuthorizationUtil.isAuthorizedRead(user, table)) { filtered.add(new Job( job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime())); } } } List<Job> sortedResult = Ordering .natural() .nullsLast() .onResultOf(JOB_ORDERING) .reverse() .immutableSortedCopy(filtered.build()); return Response.ok(sortedResult).build(); }
@GET @Path("history") @Produces(MediaType.APPLICATION_JSON) public Response getHistory( @Auth AirpalUser user, @QueryParam("table") List<Table> tables) { Iterable<Job> recentlyRun; if (tables.size() < 1) { recentlyRun = jobHistoryStore.getRecentlyRun(200); } else { Table[] tablesArray = tables.toArray(new Table[tables.size()]); Table[] restTables = Arrays.copyOfRange(tablesArray, 1, tablesArray.length); recentlyRun = jobHistoryStore.getRecentlyRun(200, tablesArray[0], restTables); } ImmutableList.Builder<Job> filtered = ImmutableList.builder(); for (Job job : recentlyRun) { if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) { filtered.add(job); continue; } for (Table table : job.getTablesUsed()) { if (AuthorizationUtil.isAuthorizedRead(user, table)) { filtered.add(new Job( job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime())); } } } List<Job> sortedResult = Ordering .natural() .nullsLast() .onResultOf(JOB_ORDERING) .reverse() .immutableSortedCopy(filtered.build()); return Response.ok(sortedResult).build(); }
public void addColumns(List<Column> columns) throws FileTooLargeException;
@Override public void addColumns(List<Column> columns) throws FileTooLargeException {}
public static HivePartition fromColumn(Column column, List<Object> values) { return new HivePartition(column.getName(), column.getType(), values); }
public static HiveColumn fromColumn(Column column, boolean isNullable, boolean isPartition) { return new HiveColumn(column.getName(), column.getType(),isPartition, isNullable); }
public TableRowMapper(ObjectMapper objectMapper) { this.objectMapper = objectMapper; this.columnTypeReference = new TypeReference<List<Column>>() {}; }
protected void updateJobInfo( Set<Table> usedTables, List<Column> columns, QueryStats queryStats, JobState state, QueryError error, List<List<Object>> outputPreview, boolean postUpdate) { if ((usedTables != null) && (usedTables.size() > 0)) { job.getTablesUsed().addAll(usedTables); } if ((columns != null) && (columns.size() > 0)) { job.setColumns(columns); } if (queryStats != null) { job.setQueryStats(queryStats); } if ((state != null) && (job.getState() != JobState.FINISHED) && (job.getState() != JobState.FAILED)) { job.setState(state); } if (error != null) { FailureInfo failureInfo = new FailureInfo( error.getFailureInfo().getType(), error.getFailureInfo().getMessage(), null, Collections.<FailureInfo>emptyList(), Collections.<String>emptyList(), error.getFailureInfo().getErrorLocation()); QueryError queryError = new QueryError( error.getMessage(), error.getSqlState(), error.getErrorCode(), error.getErrorName(), error.getErrorType(), error.getErrorLocation(), failureInfo); job.setError(queryError); } if (postUpdate) { eventBus.post(new JobUpdateEvent(job, outputPreview)); } }