aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/parallelai/spyglass/jdbc/JDBCTapCollector.java
blob: e8b7deb43de1847e977b77c9698766ac2e8a7607 (plain)
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
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package parallelai.spyglass.jdbc;

import cascading.flow.FlowProcess;
import cascading.flow.hadoop.HadoopFlowProcess;
import cascading.tap.Tap;
import cascading.tap.TapException;
import cascading.tuple.TupleEntrySchemeCollector;
import org.apache.hadoop.mapred.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Class JDBCTapCollector is a kind of {@link cascading.tuple.TupleEntrySchemeCollector} that writes tuples to the resource managed by
 * a particular {@link JDBCTap} instance.
 */
public class JDBCTapCollector extends TupleEntrySchemeCollector implements OutputCollector
{
    /** Field LOG */
    private static final Logger LOG = LoggerFactory.getLogger( JDBCTapCollector.class );

    /** Field conf */
    private final JobConf conf;
    /** Field writer */
    private RecordWriter writer;
    /** Field flowProcess */
    private final FlowProcess<JobConf> hadoopFlowProcess;
    /** Field tap */
    private final Tap<JobConf, RecordReader, OutputCollector> tap;
    /** Field reporter */
    private final Reporter reporter = Reporter.NULL;

    /**
     * Constructor TapCollector creates a new TapCollector instance.
     *
     * @param flowProcess
     * @param tap               of type Tap
     * @throws IOException when fails to initialize
     */
    public JDBCTapCollector( FlowProcess<JobConf> flowProcess, Tap<JobConf, RecordReader, OutputCollector> tap ) throws IOException {
        super( flowProcess, tap.getScheme() );
        this.hadoopFlowProcess = flowProcess;

        this.tap = tap;
        this.conf = new JobConf( flowProcess.getConfigCopy() );

        this.setOutput( this );
    }

    @Override
    public void prepare() {
        try {
            initialize();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        super.prepare();
    }

    private void initialize() throws IOException {
        tap.sinkConfInit( hadoopFlowProcess, conf );

        OutputFormat outputFormat = conf.getOutputFormat();

        LOG.info("Output format class is: " + outputFormat.getClass().toString());

        writer = outputFormat.getRecordWriter( null, conf, tap.getIdentifier(), Reporter.NULL );

        sinkCall.setOutput( this );
    }

    @Override
    public void close() {
        try {
            LOG.info( "closing tap collector for: {}", tap );
            writer.close( reporter );
        } catch( IOException exception ) {
            LOG.warn( "exception closing: {}", exception );
            throw new TapException( "exception closing JDBCTapCollector", exception );
        } finally {
            super.close();
        }
    }

    /**
     * Method collect writes the given values to the {@link Tap} this instance encapsulates.
     *
     * @param writableComparable of type WritableComparable
     * @param writable           of type Writable
     * @throws IOException when
     */
    public void collect( Object writableComparable, Object writable ) throws IOException {
        if (hadoopFlowProcess instanceof HadoopFlowProcess)
            ((HadoopFlowProcess) hadoopFlowProcess).getReporter().progress();

        writer.write( writableComparable, writable );
    }
}