Search in sources :

Example 1 with ReaderContext

use of org.apache.hive.hcatalog.data.transfer.ReaderContext in project hive by apache.

the class DataReaderMaster method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    // This config contains all the configuration that master node wants to provide
    // to the HCatalog.
    Properties externalConfigs = new Properties();
    externalConfigs.load(new FileReader(args[0]));
    Map<String, String> config = new HashMap<String, String>();
    for (Entry<Object, Object> kv : externalConfigs.entrySet()) {
        config.put((String) kv.getKey(), (String) kv.getValue());
    }
    // This piece of code runs in master node and gets necessary context.
    ReaderContext context = runsInMaster(config);
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(args[1])));
    oos.writeObject(context);
    oos.flush();
    oos.close();
// Master node will serialize readercontext and will make it available  at slaves.
}
Also used : HashMap(java.util.HashMap) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) FileOutputStream(java.io.FileOutputStream) FileReader(java.io.FileReader) Properties(java.util.Properties) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Example 2 with ReaderContext

use of org.apache.hive.hcatalog.data.transfer.ReaderContext in project hive by apache.

the class TestReaderWriter method test.

@Test
public void test() throws MetaException, CommandNeedRetryException, IOException, ClassNotFoundException {
    driver.run("drop table mytbl");
    driver.run("create table mytbl (a string, b int)");
    Iterator<Entry<String, String>> itr = hiveConf.iterator();
    Map<String, String> map = new HashMap<String, String>();
    while (itr.hasNext()) {
        Entry<String, String> kv = itr.next();
        map.put(kv.getKey(), kv.getValue());
    }
    WriterContext cntxt = runsInMaster(map);
    File writeCntxtFile = File.createTempFile("hcat-write", "temp");
    writeCntxtFile.deleteOnExit();
    // Serialize context.
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(writeCntxtFile));
    oos.writeObject(cntxt);
    oos.flush();
    oos.close();
    // Now, deserialize it.
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(writeCntxtFile));
    cntxt = (WriterContext) ois.readObject();
    ois.close();
    runsInSlave(cntxt);
    commit(map, true, cntxt);
    ReaderContext readCntxt = runsInMaster(map, false);
    File readCntxtFile = File.createTempFile("hcat-read", "temp");
    readCntxtFile.deleteOnExit();
    oos = new ObjectOutputStream(new FileOutputStream(readCntxtFile));
    oos.writeObject(readCntxt);
    oos.flush();
    oos.close();
    ois = new ObjectInputStream(new FileInputStream(readCntxtFile));
    readCntxt = (ReaderContext) ois.readObject();
    ois.close();
    for (int i = 0; i < readCntxt.numSplits(); i++) {
        runsInSlave(readCntxt, i);
    }
}
Also used : HashMap(java.util.HashMap) ObjectOutputStream(java.io.ObjectOutputStream) FileInputStream(java.io.FileInputStream) WriterContext(org.apache.hive.hcatalog.data.transfer.WriterContext) Entry(java.util.Map.Entry) FileOutputStream(java.io.FileOutputStream) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) HCatBaseTest(org.apache.hive.hcatalog.mapreduce.HCatBaseTest)

Example 3 with ReaderContext

use of org.apache.hive.hcatalog.data.transfer.ReaderContext in project hive by apache.

the class TestReaderWriter method runsInMaster.

private ReaderContext runsInMaster(Map<String, String> config, boolean bogus) throws HCatException {
    ReadEntity entity = new ReadEntity.Builder().withTable("mytbl").build();
    HCatReader reader = DataTransferFactory.getHCatReader(entity, config);
    ReaderContext cntxt = reader.prepareRead();
    return cntxt;
}
Also used : ReadEntity(org.apache.hive.hcatalog.data.transfer.ReadEntity) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) HCatReader(org.apache.hive.hcatalog.data.transfer.HCatReader)

Example 4 with ReaderContext

use of org.apache.hive.hcatalog.data.transfer.ReaderContext in project hive by apache.

the class DataReaderMaster method runsInMaster.

private static ReaderContext runsInMaster(Map<String, String> config) throws HCatException {
    ReadEntity.Builder builder = new ReadEntity.Builder();
    ReadEntity entity = builder.withTable(config.get("table")).build();
    HCatReader reader = DataTransferFactory.getHCatReader(entity, config);
    ReaderContext cntxt = reader.prepareRead();
    return cntxt;
}
Also used : ReadEntity(org.apache.hive.hcatalog.data.transfer.ReadEntity) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) HCatReader(org.apache.hive.hcatalog.data.transfer.HCatReader)

Example 5 with ReaderContext

use of org.apache.hive.hcatalog.data.transfer.ReaderContext in project hive by apache.

the class DataReaderSlave method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(args[0])));
    ReaderContext cntxt = (ReaderContext) ois.readObject();
    ois.close();
    String[] inpSlitsToRead = args[1].split(",");
    List<InputSplit> splits = cntxt.getSplits();
    for (int i = 0; i < inpSlitsToRead.length; i++) {
        InputSplit split = splits.get(Integer.parseInt(inpSlitsToRead[i]));
        HCatReader reader = DataTransferFactory.getHCatReader(split, cntxt.getConf());
        Iterator<HCatRecord> itr = reader.read();
        File f = new File(args[2] + "-" + i);
        f.delete();
        BufferedWriter outFile = new BufferedWriter(new FileWriter(f));
        while (itr.hasNext()) {
            String rec = itr.next().toString().replaceFirst("\\s+$", "");
            System.err.println(rec);
            outFile.write(rec + "\n");
        }
        outFile.close();
    }
}
Also used : FileWriter(java.io.FileWriter) FileInputStream(java.io.FileInputStream) HCatReader(org.apache.hive.hcatalog.data.transfer.HCatReader) BufferedWriter(java.io.BufferedWriter) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) File(java.io.File) InputSplit(org.apache.hadoop.mapreduce.InputSplit) HCatRecord(org.apache.hive.hcatalog.data.HCatRecord) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ReaderContext (org.apache.hive.hcatalog.data.transfer.ReaderContext)5 File (java.io.File)3 HCatReader (org.apache.hive.hcatalog.data.transfer.HCatReader)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 HashMap (java.util.HashMap)2 ReadEntity (org.apache.hive.hcatalog.data.transfer.ReadEntity)2 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 InputSplit (org.apache.hadoop.mapreduce.InputSplit)1 HCatRecord (org.apache.hive.hcatalog.data.HCatRecord)1 WriterContext (org.apache.hive.hcatalog.data.transfer.WriterContext)1 HCatBaseTest (org.apache.hive.hcatalog.mapreduce.HCatBaseTest)1 Test (org.junit.Test)1