Java Code Examples for org.springframework.jdbc.core.ResultSetExtractor

The following examples show how to use org.springframework.jdbc.core.ResultSetExtractor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: compass   Source File: ShardJdbcTemplate.java    License: Apache License 2.0 7 votes vote down vote up
@SuppressWarnings("rawtypes")
private ShardOperationProcessor<List> getNonAggregationProcessor(final RowMapper rowMapper) {
       final List<QueryCallable<List>> callableList = new ArrayList<ShardJdbcTemplate.QueryCallable<List>>();

       ShardOperationProcessor<List> processor = new ShardOperationProcessor<List>() {
           @SuppressWarnings("unchecked")
		@Override
           public void addOperation(Shard shard, String sql, Object[] args) {
               ResultSetExtractor extractor = new RowMapperResultSetExtractor(rowMapper);
               QueryCallable<List> callable = new QueryCallable<List>(shard, sql, args, extractor);

               callableList.add(callable);
           }

		@Override
           public List processOperations() {
               List<List> rawResultList = ShardJdbcTemplate.this.executeQuery(callableList);
               return AggregationUtil.aggregateObjectList(rawResultList);
           }
       };

       return processor;
   }
 
Example 2
Source Project: dts   Source File: TransactionLogStorage.java    License: Apache License 2.0 6 votes vote down vote up
public GlobalLog getGlobalLog(long transId) {
    return jdbcTemplate.query("select * from dts_global_record where trans_id = ?", new Object[] {transId},
        new ResultSetExtractor<GlobalLog>() {
            @Override
            public GlobalLog extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (!rs.next()) {
                    return null;
                }
                GlobalLog log = new GlobalLog();
                log.setTransId(rs.getLong("trans_id"));
                log.setState(rs.getInt("state"));
                log.setGmtCreated(rs.getTimestamp("gmt_created"));
                log.setGmtModified(rs.getTimestamp("gmt_modified"));
                log.setClientInfo(rs.getString("client_info"));
                log.setClientIp(rs.getString("client_ip"));
                return log;
            }
        });
}
 
Example 3
private <T> T selectRecipients(@VelocityCheck int companyID, ResultSetExtractor<T> extractor, String sqlStatement, Object... sqlParameters) throws SQLException {
	// TODO: IGNORE_BOUNCELOAD_COMPANY_ID is a bad hack for CONRAD-371
	final boolean useUnsharpRecipientQuery = configService.useUnsharpRecipientQuery(companyID);

	try (Connection connection = getDataSource().getConnection()) {
		try {
			// TODO: IGNORE_BOUNCELOAD_COMPANY_ID is a bad hack for CONRAD-371!!!
			if (useUnsharpRecipientQuery) {
				setRuleOptimizerMode(connection, true);
			}

			final SingleConnectionDataSource scds = new SingleConnectionDataSource(connection, true);
			final JdbcTemplate template = new JdbcTemplate(scds);

			return template.query(sqlStatement, extractor, sqlParameters);
		} finally {
			// TODO: IGNORE_BOUNCELOAD_COMPANY_ID is a bad hack for CONRAD-371!!!
			if (useUnsharpRecipientQuery) {
				setRuleOptimizerMode(connection, false);
			}
		}
	}
}
 
Example 4
@Override
public Set<String> findUserIdsConnectedTo(String providerId, Set<String> providerUserIds) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("providerId", providerId);
    parameters.addValue("providerUserIds", providerUserIds);
    final Set<String> localUserIds = new HashSet<String>();
    return new NamedParameterJdbcTemplate(jdbcTemplate).query("select userId from " + tablePrefix + "UserConnection where providerId = :providerId and providerUserId in (:providerUserIds)", parameters,
            new ResultSetExtractor<Set<String>>() {
                @Override
                public Set<String> extractData(ResultSet rs) throws SQLException, DataAccessException {
                    while (rs.next()) {
                        localUserIds.add(rs.getString("userId"));
                    }
                    return localUserIds;
                }
            });
}
 
Example 5
Source Project: poli   Source File: JdbcQueryService.java    License: MIT License 6 votes vote down vote up
private QueryResult executeQuery(NamedParameterJdbcTemplate npjt,
                                 String sql,
                                 Map<String, Object> namedParameters,
                                 int resultLimit) {
    // Determine max query result
    final int maxQueryResult = JdbcQueryServiceHelper.calculateMaxQueryResultLimit(appProperties.getMaximumQueryRecords(), resultLimit);

    QueryResult result = npjt.query(sql, namedParameters, new ResultSetExtractor<QueryResult>() {
        @Nullable
        @Override
        public QueryResult extractData(ResultSet rs) {
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                String[] columnNames = getColumnNames(metadata);
                List<Column> columns = getColumnList(metadata);
                String data = resultSetToJsonString(rs, metadata, maxQueryResult);
                return QueryResult.ofData(data, columns);
            } catch (Exception e) {
                String error = CommonUtils.getSimpleError(e);
                return QueryResult.ofError(error);
            }
        }
    });

    return result;
}
 
Example 6
Source Project: metacat   Source File: DirectSqlGetPartition.java    License: Apache License 2.0 6 votes vote down vote up
private List<FieldSchema> getPartitionKeys(final String databaseName,
                                           final String tableName,
                                           final boolean forceDisableAudit) {
    final List<FieldSchema> result = Lists.newArrayList();
    final ResultSetExtractor<List<FieldSchema>> handler = rs -> {
        while (rs.next()) {
            final String name = rs.getString("pkey_name");
            final String type = rs.getString("pkey_type");
            result.add(new FieldSchema(name, type, null));
        }
        return result;
    };
    final Optional<QualifiedName> sourceTable = getSourceTableName(databaseName, tableName, forceDisableAudit);

    return sourceTable.map(qualifiedName -> jdbcTemplate
        .query(SQL.SQL_GET_AUDIT_TABLE_PARTITION_KEYS,
            new Object[]{databaseName, tableName, qualifiedName.getDatabaseName(), qualifiedName.getTableName()},
            new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR},
            handler))
        .orElseGet(() -> jdbcTemplate
            .query(SQL.SQL_GET_PARTITION_KEYS,
                new Object[]{databaseName, tableName},
                new int[]{Types.VARCHAR, Types.VARCHAR},
                handler));
}
 
Example 7
Source Project: metacat   Source File: DirectSqlGetPartition.java    License: Apache License 2.0 6 votes vote down vote up
private Map<Long, Map<String, String>> getParameters(final List<Long> ids, final String sql, final String idName) {
    // Create the sql
    final StringBuilder queryBuilder = new StringBuilder(sql);
    if (!ids.isEmpty()) {
        queryBuilder.append(" and ").append(idName)
            .append(" in ('").append(Joiner.on("','").skipNulls().join(ids)).append("')");
    }
    final ResultSetExtractor<Map<Long, Map<String, String>>> handler = rs -> {
        final Map<Long, Map<String, String>> result = Maps.newHashMap();
        while (rs.next()) {
            final Long id = rs.getLong(idName);
            final String key = rs.getString("param_key");
            final String value = rs.getString("param_value");
            final Map<String, String> parameters = result.computeIfAbsent(id, k -> Maps.newHashMap());
            parameters.put(key, value);
        }
        return result;
    };
    return jdbcTemplate.query(queryBuilder.toString(), handler);
}
 
Example 8
public boolean translator(DataSource sourceDs, Record record) {
    ColumnValue idColum = record.getColumnByName("id");
    if (idColum != null) {
        // 需要根据test.id字段,和另一张表做join,提取到关联id记录的name字段,合并输出到一个目标表
        JdbcTemplate jdbcTemplate = new JdbcTemplate(sourceDs);
        String name_value = (String) jdbcTemplate.query("select NAME FROM JOIN_NAME WHERE JOIN_ID = "
                                                        + idColum.getValue().toString(), new ResultSetExtractor() {

            public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (rs.next()) {
                    return rs.getString("NAME");
                }

                return null;
            }
        });

        ColumnMeta nameMeta = new ColumnMeta("name", Types.VARCHAR);
        ColumnValue nameColumn = new ColumnValue(nameMeta, name_value);
        record.addColumn(nameColumn);
    }
    return super.translator(record);
}
 
Example 9
Source Project: spring-content   Source File: AbstractBlobResource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean exists() {
	String sql = "SELECT COUNT(id) FROM BLOBS WHERE id='" + this.id + "'";
	return this.template.query(sql, new ResultSetExtractor<Boolean>() {
		@Override
		public Boolean extractData(ResultSet rs)
				throws SQLException, DataAccessException {
			if (rs.next()) {
				int count = rs.getInt(1);
				return count == 1;
			}
			else {
				return false;
			}
		}
	});
}
 
Example 10
@Override
public Set<Long> getTaskExecutionIdsByTaskName(String taskName) {
	final MapSqlParameterSource queryParameters = new MapSqlParameterSource()
			.addValue("taskName", taskName, Types.VARCHAR);

	try {
		return this.jdbcTemplate.query(getQuery(FIND_TASK_EXECUTION_IDS_BY_TASK_NAME),
				queryParameters, new ResultSetExtractor<Set<Long>>() {
					@Override
					public Set<Long> extractData(ResultSet resultSet)
							throws SQLException, DataAccessException {
						Set<Long> taskExecutionIds = new TreeSet<>();

						while (resultSet.next()) {
							taskExecutionIds
									.add(resultSet.getLong("TASK_EXECUTION_ID"));
						}
						return taskExecutionIds;
					}
				});
	}
	catch (DataAccessException e) {
		return Collections.emptySet();
	}
}
 
Example 11
Source Project: herd   Source File: JdbcDaoImpl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Delegates to {@link JdbcOperations#query(JdbcTemplate, String, ResultSetExtractor)} where the {@link ResultSetExtractor} converts the {@link ResultSet}
 * into {@link JdbcStatementResultSet}.
 */
@Override
public JdbcStatementResultSet query(JdbcTemplate jdbcTemplate, String sql, final Integer maxResult)
{
    return jdbcOperations.query(jdbcTemplate, sql, new ResultSetExtractor<JdbcStatementResultSet>()
    {
        @Override
        public JdbcStatementResultSet extractData(ResultSet resultSet) throws SQLException, DataAccessException
        {
            JdbcStatementResultSet jdbcStatementResultSet = new JdbcStatementResultSet();

            List<String> columnNames = getColumnNames(resultSet.getMetaData());
            jdbcStatementResultSet.setColumnNames(columnNames);

            List<JdbcStatementResultSetRow> rows = getRows(resultSet, maxResult);
            jdbcStatementResultSet.setRows(rows);

            return jdbcStatementResultSet;
        }
    });
}
 
Example 12
Source Project: spring-analysis-note   Source File: NamedParameterJdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse)
		throws DataAccessException {

	return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rse);
}
 
Example 13
Source Project: spring-analysis-note   Source File: NamedParameterJdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse)
		throws DataAccessException {

	return query(sql, new MapSqlParameterSource(paramMap), rse);
}
 
Example 14
Source Project: dts   Source File: TransactionLogStorage.java    License: Apache License 2.0 5 votes vote down vote up
public BranchLog getBranchLog(long branchId) {
    return jdbcTemplate.query("select * from dts_branch_record where branch_id = ?", new Object[] {branchId},
        new ResultSetExtractor<BranchLog>() {
            @Override
            public BranchLog extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (!rs.next()) {
                    return null;
                }
                return rowToObject(rs);
            }
        });
}
 
Example 15
Source Project: java-technology-stack   Source File: NamedParameterJdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse)
		throws DataAccessException {

	return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rse);
}
 
Example 16
Source Project: java-technology-stack   Source File: NamedParameterJdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse)
		throws DataAccessException {

	return query(sql, new MapSqlParameterSource(paramMap), rse);
}
 
Example 17
Source Project: qconfig   Source File: PermissionDaoImpl.java    License: MIT License 5 votes vote down vote up
@Override
public Integer selectPermission(String group, String rtxId) {
    return jdbcTemplate.query(SELECT_PERMISSION_SQL, new ResultSetExtractor<Integer>() {
        @Override
        public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
            if (rs.next()) {
                return rs.getInt("permission");
            } else {
                return null;
            }
        }
    }, group, rtxId);
}
 
Example 18
Source Project: qconfig   Source File: CandidateSnapshotDaoImpl.java    License: MIT License 5 votes vote down vote up
@Override
public Long findEditVersion(ConfigMeta configMeta, long editVersion) {
    return jdbcTemplate.query(FIND_EDIT_VERSION_SQL,
            new Object[]{configMeta.getGroup(), configMeta.getProfile(), configMeta.getDataId(), editVersion},
            new ResultSetExtractor<Long>() {
                @Override
                public Long extractData(ResultSet rs) throws SQLException, DataAccessException {
                    if (rs.next()) {
                        return rs.getLong(rs.getInt("edit_version"));
                    }
                    return -1L;
                }
            });
}
 
Example 19
Source Project: qconfig   Source File: CandidateSnapshotDaoImpl.java    License: MIT License 5 votes vote down vote up
@Override
public long findLatestRecordId() {
    return jdbcTemplate.query("SELECT id FROM config_candidate_snapshot ORDER BY id DESC LIMIT 1", new ResultSetExtractor<Long>() {
        @Override
        public Long extractData(ResultSet rs) throws SQLException, DataAccessException {
            if (rs.next()) {
                return rs.getLong("id");
            }
            return null;
        }
    });
}
 
Example 20
Source Project: qconfig   Source File: JdbcTemplateDelegated.java    License: MIT License 5 votes vote down vote up
public <T> T query(String sql, ResultSetExtractor<T> extractor, Object...args) throws DataAccessException {
    T result = null;
    for (JdbcTemplate jdbcTemplate : jdbcTemplates.values()) {
        result = jdbcTemplate.query(sql, extractor, args);
        if (result != null) {
            break;
        }
    }
    return result;
}
 
Example 21
Source Project: pacbot   Source File: NotificationServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> unsubscribeDigestMail(final String mailId) {
	String sql = "SELECT subscriptionValue AS subscriptionValue FROM PacmanSubscriptions WHERE emailId = ?";
	String updateOrInsertQuery ;
	Object[] parameters;
	Date date = new Date();
	Boolean isUnsubscriptedAlready =  jdbcTemplate.query(sql, new Object[]{mailId}, new ResultSetExtractor<Boolean>() {
	    @Override
	    public Boolean extractData(ResultSet rs) {
	        try {
				return rs.next() ? rs.getBoolean("subscriptionValue") : null;
			} catch (SQLException e) {
				Log.error(e.getMessage());
				return null;
			}
	    }
	});
	
	if(isUnsubscriptedAlready != null) {
		updateOrInsertQuery="UPDATE PacmanSubscriptions SET emailId=?, subscriptionValue=?, modifiedDate=? WHERE emailId=?";
		parameters = new Object[] {mailId, false, date, mailId};
	} else {
		updateOrInsertQuery="INSERT INTO PacmanSubscriptions (emailId, subscriptionValue, createdDate, modifiedDate) VALUES (?,?,?,?)";
		
		parameters = new Object[] {mailId, false, date, date};
	}
	int status = jdbcTemplate.update(updateOrInsertQuery, parameters);
	Map<String, Object> response = Maps.newHashMap();
	if(status==1) {
		response.put(STATUS, true);
		response.put(MESSAGE_KEY, "You have successfully unsubscribed");
	} else {
		response.put(STATUS, false);
		response.put(MESSAGE_KEY, "Unsubscribing Failed!!!");
	}
	return response;
}
 
Example 22
Source Project: pacbot   Source File: NotificationServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> subscribeDigestMail(String mailId) {
	String sql = "SELECT subscriptionValue AS subscriptionValue FROM PacmanSubscriptions WHERE emailId = ?";
	String updateOrInsertQuery ;
	Object[] parameters;
	Date date = new Date();
	Boolean isUnsubscriptedAlready =  jdbcTemplate.query(sql, new Object[]{mailId}, new ResultSetExtractor<Boolean>() {
	    @Override
	    public Boolean extractData(ResultSet rs) {
	        try {
				return rs.next() ? rs.getBoolean("subscriptionValue") : null;
			} catch (SQLException e) {
				Log.error(e.getMessage());
				return null;
			}
	    }
	});
	
	if(isUnsubscriptedAlready != null) {
		updateOrInsertQuery="UPDATE PacmanSubscriptions SET emailId=?, subscriptionValue=?, modifiedDate=? WHERE emailId=?";
		parameters = new Object[] {mailId, true, date, mailId};
	} else {
		updateOrInsertQuery="INSERT INTO PacmanSubscriptions (emailId, subscriptionValue, createdDate, modifiedDate) VALUES (?,?,?,?)";
		
		parameters = new Object[] {mailId, true, date, date};
	}
	int status = jdbcTemplate.update(updateOrInsertQuery, parameters);
	Map<String, Object> response = Maps.newHashMap();
	if(status==1) {
		response.put(STATUS, true);
		response.put(MESSAGE_KEY, "You have successfully subscribed");
	} else {
		response.put(STATUS, false);
		response.put(MESSAGE_KEY, "Subscription Failed!!!");
	}
	return response;
}
 
Example 23
Source Project: poli   Source File: JdbcQueryService.java    License: MIT License 5 votes vote down vote up
public QueryResult executeQuery(DataSource dataSource, String sql, String contentType) {
    JdbcTemplate jt = new JdbcTemplate(dataSource);
    final int maxQueryResult = JdbcQueryServiceHelper.calculateMaxQueryResultLimit(appProperties.getMaximumQueryRecords(), Constants.QUERY_RESULT_NOLIMIT);

    QueryResult result = jt.query(sql, new Object[] {}, new ResultSetExtractor<QueryResult>() {
        @Nullable
        @Override
        public QueryResult extractData(ResultSet rs) {
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                String[] columnNames = getColumnNames(metadata);
                List<Column> columns = getColumnList(metadata);
                String data;
                if (Constants.CONTENT_TYPE_CSV.equals(contentType)) {
                    data = resultSetToCsvString(rs, columnNames, maxQueryResult);
                } else {
                    data = resultSetToJsonString(rs, metadata, maxQueryResult);
                }
                return QueryResult.ofData(data, columns);
            } catch (Exception e) {
                String error = CommonUtils.getSimpleError(e);
                return QueryResult.ofError(error);
            }
        }
    });

    return result;
}
 
Example 24
Source Project: springboot-shiro-cas-mybatis   Source File: DataSourceMonitor.java    License: MIT License 5 votes vote down vote up
@Override
protected StatusCode checkPool() throws Exception {
    return this.jdbcTemplate.query(this.validationQuery, new ResultSetExtractor<StatusCode>() {
        public StatusCode extractData(final ResultSet rs) throws SQLException {
            if (rs.next()) {
                return StatusCode.OK;
            }
            return StatusCode.WARN;
        }
    });
}
 
Example 25
Source Project: cas4.0.x-server-wechat   Source File: DataSourceMonitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected StatusCode checkPool() throws Exception {
    return this.jdbcTemplate.query(this.validationQuery, new ResultSetExtractor<StatusCode>() {
        public StatusCode extractData(final ResultSet rs) throws SQLException {
            if (rs.next()) {
                return StatusCode.OK;
            }
            return StatusCode.WARN;
        }
    });
}
 
Example 26
Source Project: diamond   Source File: PaginationHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 取分页
 * 
 * @param jt
 *            jdbcTemplate
 * @param sqlCountRows
 *            查询总数的SQL
 * @param sqlFetchRows
 *            查询数据的sql
 * @param args
 *            查询参数
 * @param pageNo
 *            页数
 * @param pageSize
 *            每页大小
 * @param rowMapper
 * @return
 */
public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows,
        final Object args[], final int pageNo, final int pageSize, final ParameterizedRowMapper<E> rowMapper) {
    if (pageSize == 0) {
        return null;
    }

    // 查询当前记录总数
    final int rowCount = jt.queryForInt(sqlCountRows, args);

    // 计算页数
    int pageCount = rowCount / pageSize;
    if (rowCount > pageSize * pageCount) {
        pageCount++;
    }

    // 创建Page对象
    final Page<E> page = new Page<E>();
    page.setPageNumber(pageNo);
    page.setPagesAvailable(pageCount);
    page.setTotalCount(rowCount);

    if (pageNo > pageCount)
        return null;
    // 取单页数据,计算起始位置
    final int startRow = (pageNo - 1) * pageSize;
    // TODO 在数据量很大时, limit效率很低
    final String selectSQL = sqlFetchRows + " limit " + startRow + "," + pageSize;
    jt.query(selectSQL, args, new ResultSetExtractor() {
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            final List<E> pageItems = page.getPageItems();
            int currentRow = 0;
            while (rs.next()) {
                pageItems.add(rowMapper.mapRow(rs, currentRow++));
            }
            return page;
        }
    });
    return page;
}
 
Example 27
Source Project: metacat   Source File: MysqlUserMetadataService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Transactional(readOnly = true)
public List<QualifiedName> searchByOwners(final Set<String> owners) {
    final List<QualifiedName> result = Lists.newArrayList();
    final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES);
    final List<SqlParameterValue> paramList = Lists.newArrayList();
    query.append(" where 1=0");
    owners.forEach(s -> {
        query.append(" or data like ?");
        paramList.add(new SqlParameterValue(Types.VARCHAR, "%\"userId\":\"" + s.trim() + "\"%"));
    });
    final SqlParameterValue[] params = new SqlParameterValue[paramList.size()];
    try {
        // Handler for reading the result set
        final ResultSetExtractor<Void> handler = rs -> {
            while (rs.next()) {
                final String definitionName = rs.getString("name");
                result.add(QualifiedName.fromString(definitionName, false));
            }
            return null;
        };
        jdbcTemplate.query(query.toString(), paramList.toArray(params), handler);
    } catch (Exception e) {
        log.error("Failed to search by owners", e);
        throw new UserMetadataServiceException("Failed to search by owners", e);
    }
    return result;

}
 
Example 28
Source Project: metacat   Source File: DirectSqlGetPartition.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the partition uris based on a filter expression for the specified table.
 *
 * @param requestContext    The Metacat request context
 * @param tableName         table handle to get partition for
 * @param partitionsRequest The metadata for what kind of partitions to get from the table
 * @return filtered list of partition names
 */
@Transactional(readOnly = true)
public List<String> getPartitionUris(final ConnectorRequestContext requestContext,
                                     final QualifiedName tableName,
                                     final PartitionListRequest partitionsRequest) {
    final long start = registry.clock().wallTime();
    final List<String> result;
    final List<String> partitionNames = partitionsRequest.getPartitionNames();
    final Sort sort = partitionsRequest.getSort();
    final Pageable pageable = partitionsRequest.getPageable();

    final String filterExpression = partitionsRequest.getFilter();

    if (filterExpression != null) {
        return filterPartitionsColumn(
            tableName.getDatabaseName(),
            tableName.getTableName(),
            partitionNames,
            PARTITION_URI,
            filterExpression,
            sort,
            pageable,
            partitionsRequest.getIncludeAuditOnly());
    } else {
        final ResultSetExtractor<List<String>> handler = rs -> {
            final List<String> uris = Lists.newArrayList();
            while (rs.next()) {
                uris.add(rs.getString(PARTITION_URI));
            }
            return uris;
        };
        result = getHandlerResults(tableName.getDatabaseName(), tableName.getTableName(),
            null, partitionNames, SQL.SQL_GET_PARTITIONS_URI, handler, sort, pageable,
            partitionsRequest.getIncludeAuditOnly());
    }
    this.fastServiceMetric.recordTimer(
        HiveMetrics.TagGetPartitionKeys.getMetricName(), registry.clock().wallTime() - start);
    return result;
}
 
Example 29
Source Project: metacat   Source File: DirectSqlGetPartition.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * query partitions using filters from name or uri column.
 */
private List<String> filterPartitionsColumn(
    final String databaseName,
    final String tableName,
    final List<String> partitionNames,
    final String columnName,
    final String filterExpression,
    final Sort sort,
    final Pageable pageable,
    final boolean forceDisableAudit) {
    final FilterPartition filter = new FilterPartition();
    // batch exists
    final boolean isBatched =
        !Strings.isNullOrEmpty(filterExpression) && filterExpression.contains(FIELD_BATCHID);
    final boolean hasDateCreated =
        !Strings.isNullOrEmpty(filterExpression) && filterExpression.contains(FIELD_DATE_CREATED);

    ResultSetExtractor<List<String>> handler = rs -> {
        final List<String> columns = Lists.newArrayList();
        while (rs.next()) {
            final String name = rs.getString(PARTITION_NAME);
            final String uri = rs.getString(PARTITION_URI);
            final long createdDate = rs.getLong(FIELD_DATE_CREATED);
            Map<String, String> values = null;
            if (hasDateCreated) {
                values = Maps.newHashMap();
                values.put(FIELD_DATE_CREATED, createdDate + "");
            }
            if (Strings.isNullOrEmpty(filterExpression)
                || filter.evaluatePartitionExpression(filterExpression, name, uri, isBatched, values)) {
                columns.add(rs.getString(columnName));
            }
        }
        return columns;
    };
    return getHandlerResults(databaseName,
        tableName, filterExpression, partitionNames,
        SQL.SQL_GET_PARTITIONS_WITH_KEY_URI, handler, sort, pageable, forceDisableAudit);
}
 
Example 30
Source Project: metacat   Source File: DirectSqlGetPartition.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the partition names/keys based on a filter expression for the specified table.
 *
 * @param requestContext    The Metacat request context
 * @param tableName         table handle to get partition for
 * @param partitionsRequest The metadata for what kind of partitions to get from the table
 * @return filtered list of partition names
 */
@Transactional(readOnly = true)
public List<String> getPartitionKeys(final ConnectorRequestContext requestContext,
                                     final QualifiedName tableName,
                                     final PartitionListRequest partitionsRequest) {
    final long start = registry.clock().wallTime();
    final List<String> result;
    final List<String> partitionNames = partitionsRequest.getPartitionNames();
    final Sort sort = partitionsRequest.getSort();
    final Pageable pageable = partitionsRequest.getPageable();

    final String filterExpression = partitionsRequest.getFilter();
    if (filterExpression != null) {
        return filterPartitionsColumn(
            tableName.getDatabaseName(),
            tableName.getTableName(),
            partitionNames,
            PARTITION_NAME,
            filterExpression,
            sort,
            pageable,
            partitionsRequest.getIncludeAuditOnly());
    } else {
        final ResultSetExtractor<List<String>> handler = rs -> {
            final List<String> names = Lists.newArrayList();
            while (rs.next()) {
                names.add(rs.getString("name"));
            }
            return names;
        };
        result = getHandlerResults(tableName.getDatabaseName(), tableName.getTableName(),
            null, partitionNames, SQL.SQL_GET_PARTITIONS_WITH_KEY,
            handler, sort, pageable, partitionsRequest.getIncludeAuditOnly());
    }
    this.fastServiceMetric.recordTimer(
        HiveMetrics.TagGetPartitionKeys.getMetricName(), registry.clock().wallTime() - start);
    return result;
}