Search in sources :

Example 11 with HCatRecord

use of org.apache.hive.hcatalog.data.HCatRecord in project hive by apache.

the class HCatInputFormatReader method read.

@Override
public Iterator<HCatRecord> read() throws HCatException {
    HCatInputFormat inpFmt = new HCatInputFormat();
    RecordReader<WritableComparable, HCatRecord> rr;
    try {
        TaskAttemptContext cntxt = ShimLoader.getHadoopShims().getHCatShim().createTaskAttemptContext(conf, new TaskAttemptID());
        rr = inpFmt.createRecordReader(split, cntxt);
        rr.initialize(split, cntxt);
    } catch (IOException e) {
        throw new HCatException(ErrorType.ERROR_NOT_INITIALIZED, e);
    } catch (InterruptedException e) {
        throw new HCatException(ErrorType.ERROR_NOT_INITIALIZED, e);
    }
    return new HCatRecordItr(rr);
}
Also used : WritableComparable(org.apache.hadoop.io.WritableComparable) TaskAttemptID(org.apache.hadoop.mapreduce.TaskAttemptID) HCatException(org.apache.hive.hcatalog.common.HCatException) TaskAttemptContext(org.apache.hadoop.mapreduce.TaskAttemptContext) IOException(java.io.IOException) HCatInputFormat(org.apache.hive.hcatalog.mapreduce.HCatInputFormat) HCatRecord(org.apache.hive.hcatalog.data.HCatRecord)

Example 12 with HCatRecord

use of org.apache.hive.hcatalog.data.HCatRecord in project hive by apache.

the class HCatOutputFormatWriter method write.

@Override
public void write(Iterator<HCatRecord> recordItr) throws HCatException {
    int id = sp.getId();
    setVarsInConf(id);
    HCatOutputFormat outFormat = new HCatOutputFormat();
    TaskAttemptContext cntxt = ShimLoader.getHadoopShims().getHCatShim().createTaskAttemptContext(conf, new TaskAttemptID(ShimLoader.getHadoopShims().getHCatShim().createTaskID(), id));
    OutputCommitter committer = null;
    RecordWriter<WritableComparable<?>, HCatRecord> writer;
    try {
        committer = outFormat.getOutputCommitter(cntxt);
        committer.setupTask(cntxt);
        writer = outFormat.getRecordWriter(cntxt);
        while (recordItr.hasNext()) {
            HCatRecord rec = recordItr.next();
            writer.write(null, rec);
        }
        writer.close(cntxt);
        if (committer.needsTaskCommit(cntxt)) {
            committer.commitTask(cntxt);
        }
    } catch (IOException e) {
        if (null != committer) {
            try {
                committer.abortTask(cntxt);
            } catch (IOException e1) {
                throw new HCatException(ErrorType.ERROR_INTERNAL_EXCEPTION, e1);
            }
        }
        throw new HCatException("Failed while writing", e);
    } catch (InterruptedException e) {
        if (null != committer) {
            try {
                committer.abortTask(cntxt);
            } catch (IOException e1) {
                throw new HCatException(ErrorType.ERROR_INTERNAL_EXCEPTION, e1);
            }
        }
        throw new HCatException("Failed while writing", e);
    }
}
Also used : OutputCommitter(org.apache.hadoop.mapreduce.OutputCommitter) WritableComparable(org.apache.hadoop.io.WritableComparable) TaskAttemptID(org.apache.hadoop.mapreduce.TaskAttemptID) HCatException(org.apache.hive.hcatalog.common.HCatException) HCatOutputFormat(org.apache.hive.hcatalog.mapreduce.HCatOutputFormat) TaskAttemptContext(org.apache.hadoop.mapreduce.TaskAttemptContext) IOException(java.io.IOException) HCatRecord(org.apache.hive.hcatalog.data.HCatRecord)

Example 13 with HCatRecord

use of org.apache.hive.hcatalog.data.HCatRecord in project beam by apache.

the class HCatalogIOTest method testReadFromSource.

/**
 * Test of Read using SourceTestUtils.readFromSource(..).
 */
@Test
@NeedsTestData
public void testReadFromSource() throws Exception {
    ReaderContext context = getReaderContext(getConfigPropertiesAsMap(service.getHiveConf()));
    HCatalogIO.Read spec = HCatalogIO.read().withConfigProperties(getConfigPropertiesAsMap(service.getHiveConf())).withContext(context).withTable(TEST_TABLE);
    List<String> records = new ArrayList<>();
    for (int i = 0; i < context.numSplits(); i++) {
        BoundedHCatalogSource source = new BoundedHCatalogSource(spec.withSplitId(i));
        for (HCatRecord record : SourceTestUtils.readFromSource(source, OPTIONS)) {
            records.add(record.get(0).toString());
        }
    }
    assertThat(records, containsInAnyOrder(getExpectedRecords(TEST_RECORDS_COUNT).toArray()));
}
Also used : HCatalogIOTestUtils.getReaderContext(org.apache.beam.sdk.io.hcatalog.test.HCatalogIOTestUtils.getReaderContext) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) BoundedHCatalogSource(org.apache.beam.sdk.io.hcatalog.HCatalogIO.BoundedHCatalogSource) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) DefaultHCatRecord(org.apache.hive.hcatalog.data.DefaultHCatRecord) HCatRecord(org.apache.hive.hcatalog.data.HCatRecord) Test(org.junit.Test)

Example 14 with HCatRecord

use of org.apache.hive.hcatalog.data.HCatRecord in project flink by apache.

the class HCatInputFormatBase method nextRecord.

@Override
public T nextRecord(T record) throws IOException {
    if (!this.fetched) {
        // first record
        fetchNext();
    }
    if (!this.hasNext) {
        return null;
    }
    try {
        // get next HCatRecord
        HCatRecord v = this.recordReader.getCurrentValue();
        this.fetched = false;
        if (this.fieldNames.length > 0) {
            // return as Flink tuple
            return this.buildFlinkTuple(record, v);
        } else {
            // return as HCatRecord
            return (T) v;
        }
    } catch (InterruptedException e) {
        throw new IOException("Could not get next record.", e);
    }
}
Also used : IOException(java.io.IOException) DefaultHCatRecord(org.apache.hive.hcatalog.data.DefaultHCatRecord) HCatRecord(org.apache.hive.hcatalog.data.HCatRecord)

Aggregations

HCatRecord (org.apache.hive.hcatalog.data.HCatRecord)14 IOException (java.io.IOException)6 DefaultHCatRecord (org.apache.hive.hcatalog.data.DefaultHCatRecord)6 ArrayList (java.util.ArrayList)4 WritableComparable (org.apache.hadoop.io.WritableComparable)4 HCatException (org.apache.hive.hcatalog.common.HCatException)4 HashMap (java.util.HashMap)3 TaskAttemptContext (org.apache.hadoop.mapreduce.TaskAttemptContext)3 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)3 ReaderContext (org.apache.hive.hcatalog.data.transfer.ReaderContext)3 Path (org.apache.hadoop.fs.Path)2 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 Job (org.apache.hadoop.mapreduce.Job)2 OutputCommitter (org.apache.hadoop.mapreduce.OutputCommitter)2 TaskAttemptID (org.apache.hadoop.mapreduce.TaskAttemptID)2 HCatFieldSchema (org.apache.hive.hcatalog.data.schema.HCatFieldSchema)2 HCatReader (org.apache.hive.hcatalog.data.transfer.HCatReader)2 HCatInputFormat (org.apache.hive.hcatalog.mapreduce.HCatInputFormat)2 HCatOutputFormat (org.apache.hive.hcatalog.mapreduce.HCatOutputFormat)2