Search in sources :

Example 1 with Folder3

use of org.skife.jdbi.v2.Folder3 in project druid by druid-io.

the class SQLMetadataSegmentManager method enableDatasource.

@Override
public boolean enableDatasource(final String ds) {
    try {
        final IDBI dbi = connector.getDBI();
        VersionedIntervalTimeline<String, DataSegment> segmentTimeline = connector.inReadOnlyTransaction(new TransactionCallback<VersionedIntervalTimeline<String, DataSegment>>() {

            @Override
            public VersionedIntervalTimeline<String, DataSegment> inTransaction(Handle handle, TransactionStatus status) throws Exception {
                return handle.createQuery(String.format("SELECT payload FROM %s WHERE dataSource = :dataSource", getSegmentsTable())).setFetchSize(connector.getStreamingFetchSize()).bind("dataSource", ds).map(ByteArrayMapper.FIRST).fold(new VersionedIntervalTimeline<String, DataSegment>(Ordering.natural()), new Folder3<VersionedIntervalTimeline<String, DataSegment>, byte[]>() {

                    @Override
                    public VersionedIntervalTimeline<String, DataSegment> fold(VersionedIntervalTimeline<String, DataSegment> timeline, byte[] payload, FoldController foldController, StatementContext statementContext) throws SQLException {
                        try {
                            final DataSegment segment = DATA_SEGMENT_INTERNER.intern(jsonMapper.readValue(payload, DataSegment.class));
                            timeline.add(segment.getInterval(), segment.getVersion(), segment.getShardSpec().createChunk(segment));
                            return timeline;
                        } catch (Exception e) {
                            throw new SQLException(e.toString());
                        }
                    }
                });
            }
        });
        final List<DataSegment> segments = Lists.newArrayList();
        for (TimelineObjectHolder<String, DataSegment> objectHolder : segmentTimeline.lookup(new Interval("0000-01-01/3000-01-01"))) {
            for (PartitionChunk<DataSegment> partitionChunk : objectHolder.getObject()) {
                segments.add(partitionChunk.getObject());
            }
        }
        if (segments.isEmpty()) {
            log.warn("No segments found in the database!");
            return false;
        }
        dbi.withHandle(new HandleCallback<Void>() {

            @Override
            public Void withHandle(Handle handle) throws Exception {
                Batch batch = handle.createBatch();
                for (DataSegment segment : segments) {
                    batch.add(String.format("UPDATE %s SET used=true WHERE id = '%s'", getSegmentsTable(), segment.getIdentifier()));
                }
                batch.execute();
                return null;
            }
        });
    } catch (Exception e) {
        log.error(e, "Exception enabling datasource %s", ds);
        return false;
    }
    return true;
}
Also used : IDBI(org.skife.jdbi.v2.IDBI) SQLException(java.sql.SQLException) TransactionStatus(org.skife.jdbi.v2.TransactionStatus) DataSegment(io.druid.timeline.DataSegment) SQLException(java.sql.SQLException) IOException(java.io.IOException) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) FoldController(org.skife.jdbi.v2.FoldController) Batch(org.skife.jdbi.v2.Batch) VersionedIntervalTimeline(io.druid.timeline.VersionedIntervalTimeline) Folder3(org.skife.jdbi.v2.Folder3) Interval(org.joda.time.Interval)

Example 2 with Folder3

use of org.skife.jdbi.v2.Folder3 in project hive by apache.

the class DruidStorageHandlerUtils method getDataSegmentList.

/**
   * @param connector                   SQL connector to metadata
   * @param metadataStorageTablesConfig Tables configuration
   * @param dataSource                  Name of data source
   *
   * @return List of all data segments part of the given data source
   */
public static List<DataSegment> getDataSegmentList(final SQLMetadataConnector connector, final MetadataStorageTablesConfig metadataStorageTablesConfig, final String dataSource) {
    List<DataSegment> segmentList = connector.retryTransaction(new TransactionCallback<List<DataSegment>>() {

        @Override
        public List<DataSegment> inTransaction(Handle handle, TransactionStatus status) throws Exception {
            return handle.createQuery(String.format("SELECT payload FROM %s WHERE dataSource = :dataSource", metadataStorageTablesConfig.getSegmentsTable())).setFetchSize(getStreamingFetchSize(connector)).bind("dataSource", dataSource).map(ByteArrayMapper.FIRST).fold(new ArrayList<DataSegment>(), new Folder3<List<DataSegment>, byte[]>() {

                @Override
                public List<DataSegment> fold(List<DataSegment> accumulator, byte[] payload, FoldController control, StatementContext ctx) throws SQLException {
                    try {
                        final DataSegment segment = DATA_SEGMENT_INTERNER.intern(JSON_MAPPER.readValue(payload, DataSegment.class));
                        accumulator.add(segment);
                        return accumulator;
                    } catch (Exception e) {
                        throw new SQLException(e.toString());
                    }
                }
            });
        }
    }, 3, SQLMetadataConnector.DEFAULT_MAX_TRIES);
    return segmentList;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) TransactionStatus(org.skife.jdbi.v2.TransactionStatus) DataSegment(io.druid.timeline.DataSegment) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) FoldController(org.skife.jdbi.v2.FoldController) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Folder3(org.skife.jdbi.v2.Folder3)

Aggregations

DataSegment (io.druid.timeline.DataSegment)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 FoldController (org.skife.jdbi.v2.FoldController)2 Folder3 (org.skife.jdbi.v2.Folder3)2 Handle (org.skife.jdbi.v2.Handle)2 StatementContext (org.skife.jdbi.v2.StatementContext)2 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)2 ImmutableList (com.google.common.collect.ImmutableList)1 VersionedIntervalTimeline (io.druid.timeline.VersionedIntervalTimeline)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Interval (org.joda.time.Interval)1 Batch (org.skife.jdbi.v2.Batch)1 IDBI (org.skife.jdbi.v2.IDBI)1