Search in sources :

Example 1 with BadTsvLineException

use of org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException in project hbase by apache.

the class TestImportTsvParser method testTsvParseAttributesKey.

@Test
public void testTsvParseAttributesKey() throws BadTsvLineException {
    TsvParser parser = new TsvParser("HBASE_ROW_KEY,col_a,HBASE_TS_KEY,HBASE_ATTRIBUTES_KEY", "\t");
    assertEquals(0, parser.getRowKeyColumnIndex());
    byte[] line = Bytes.toBytes("rowkey\tval_a\t1234\tkey=>value");
    ParsedLine parse = parser.parse(line, line.length);
    assertEquals(18, parse.getAttributeKeyOffset());
    assertEquals(3, parser.getAttributesKeyColumnIndex());
    String[] attributes = parse.getIndividualAttributes();
    assertEquals("key=>value", attributes[0]);
    try {
        line = Bytes.toBytes("rowkey\tval_a\t1234");
        parser.parse(line, line.length);
        fail("Should get BadTsvLineException on empty rowkey.");
    } catch (BadTsvLineException ignored) {
    }
    parser = new TsvParser("HBASE_ATTRIBUTES_KEY,col_a,HBASE_ROW_KEY,HBASE_TS_KEY", "\t");
    assertEquals(2, parser.getRowKeyColumnIndex());
    line = Bytes.toBytes("key=>value\tval_a\trowkey\t1234");
    parse = parser.parse(line, line.length);
    assertEquals(0, parse.getAttributeKeyOffset());
    assertEquals(0, parser.getAttributesKeyColumnIndex());
    attributes = parse.getIndividualAttributes();
    assertEquals("key=>value", attributes[0]);
    try {
        line = Bytes.toBytes("val_a");
        ParsedLine parse2 = parser.parse(line, line.length);
        fail("Should get BadTsvLineException when number of columns less than rowkey position.");
    } catch (BadTsvLineException ignored) {
    }
    parser = new TsvParser("col_a,HBASE_ATTRIBUTES_KEY,HBASE_TS_KEY,HBASE_ROW_KEY", "\t");
    assertEquals(3, parser.getRowKeyColumnIndex());
    line = Bytes.toBytes("val_a\tkey0=>value0,key1=>value1,key2=>value2\t1234\trowkey");
    parse = parser.parse(line, line.length);
    assertEquals(1, parser.getAttributesKeyColumnIndex());
    assertEquals(6, parse.getAttributeKeyOffset());
    String[] attr = parse.getIndividualAttributes();
    int i = 0;
    for (String str : attr) {
        assertEquals(("key" + i + "=>" + "value" + i), str);
        i++;
    }
}
Also used : BadTsvLineException(org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException) ParsedLine(org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.ParsedLine) TsvParser(org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser) Test(org.junit.Test)

Example 2 with BadTsvLineException

use of org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException in project hbase by apache.

the class TestImportTsvParser method testTsvParserParseRowKey.

@Test
public void testTsvParserParseRowKey() throws BadTsvLineException {
    TsvParser parser = new TsvParser("HBASE_ROW_KEY,col_a,HBASE_TS_KEY", "\t");
    assertEquals(0, parser.getRowKeyColumnIndex());
    byte[] line = Bytes.toBytes("rowkey\tval_a\t1234");
    Pair<Integer, Integer> rowKeyOffsets = parser.parseRowKey(line, line.length);
    assertEquals(0, rowKeyOffsets.getFirst().intValue());
    assertEquals(6, rowKeyOffsets.getSecond().intValue());
    try {
        line = Bytes.toBytes("\t\tval_a\t1234");
        parser.parseRowKey(line, line.length);
        fail("Should get BadTsvLineException on empty rowkey.");
    } catch (BadTsvLineException ignored) {
    }
    parser = new TsvParser("col_a,HBASE_ROW_KEY,HBASE_TS_KEY", "\t");
    assertEquals(1, parser.getRowKeyColumnIndex());
    line = Bytes.toBytes("val_a\trowkey\t1234");
    rowKeyOffsets = parser.parseRowKey(line, line.length);
    assertEquals(6, rowKeyOffsets.getFirst().intValue());
    assertEquals(6, rowKeyOffsets.getSecond().intValue());
    try {
        line = Bytes.toBytes("val_a");
        rowKeyOffsets = parser.parseRowKey(line, line.length);
        fail("Should get BadTsvLineException when number of columns less than rowkey position.");
    } catch (BadTsvLineException ignored) {
    }
    parser = new TsvParser("col_a,HBASE_TS_KEY,HBASE_ROW_KEY", "\t");
    assertEquals(2, parser.getRowKeyColumnIndex());
    line = Bytes.toBytes("val_a\t1234\trowkey");
    rowKeyOffsets = parser.parseRowKey(line, line.length);
    assertEquals(11, rowKeyOffsets.getFirst().intValue());
    assertEquals(6, rowKeyOffsets.getSecond().intValue());
}
Also used : BadTsvLineException(org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException) TsvParser(org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser) Test(org.junit.Test)

Example 3 with BadTsvLineException

use of org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException in project hbase by apache.

the class TsvImporterCustomTestMapperForOprAttr method populatePut.

@Override
protected void populatePut(byte[] lineBytes, ParsedLine parsed, Put put, int i) throws BadTsvLineException, IOException {
    KeyValue kv;
    kv = new KeyValue(lineBytes, parsed.getRowKeyOffset(), parsed.getRowKeyLength(), parser.getFamily(i), 0, parser.getFamily(i).length, parser.getQualifier(i), 0, parser.getQualifier(i).length, ts, KeyValue.Type.Put, lineBytes, parsed.getColumnOffset(i), parsed.getColumnLength(i));
    if (parsed.getIndividualAttributes() != null) {
        String[] attributes = parsed.getIndividualAttributes();
        for (String attr : attributes) {
            String[] split = attr.split(ImportTsv.DEFAULT_ATTRIBUTES_SEPERATOR);
            if (split == null || split.length <= 1) {
                throw new BadTsvLineException(msg(attributes));
            } else {
                if (split[0].length() <= 0 || split[1].length() <= 0) {
                    throw new BadTsvLineException(msg(attributes));
                }
                put.setAttribute(split[0], Bytes.toBytes(split[1]));
            }
        }
    }
    put.add(kv);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) BadTsvLineException(org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException)

Aggregations

BadTsvLineException (org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException)3 TsvParser (org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser)2 Test (org.junit.Test)2 KeyValue (org.apache.hadoop.hbase.KeyValue)1 ParsedLine (org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.ParsedLine)1