1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapreduce;
21  
22  import java.util.ArrayList;
23  
24  import org.apache.hadoop.hbase.HConstants;
25  import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser;
26  import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException;
27  import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.ParsedLine;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.Test;
30  
31  import com.google.common.base.Joiner;
32  import com.google.common.base.Splitter;
33  import com.google.common.collect.Iterables;
34  
35  import static org.junit.Assert.*;
36  
37  public class TestImportTsv {
38    @Test
39    public void testTsvParser() throws BadTsvLineException {
40      TsvParser parser = new TsvParser("col_a,col_b:qual,HBASE_ROW_KEY,col_d", "\t");
41      assertBytesEquals(Bytes.toBytes("col_a"), parser.getFamily(0));
42      assertBytesEquals(HConstants.EMPTY_BYTE_ARRAY, parser.getQualifier(0));
43      assertBytesEquals(Bytes.toBytes("col_b"), parser.getFamily(1));
44      assertBytesEquals(Bytes.toBytes("qual"), parser.getQualifier(1));
45      assertNull(parser.getFamily(2));
46      assertNull(parser.getQualifier(2));
47      
48      byte[] line = Bytes.toBytes("val_a\tval_b\tval_c\tval_d");
49      ParsedLine parsed = parser.parse(line, line.length);
50      checkParsing(parsed, Splitter.on("\t").split(Bytes.toString(line)));
51      assertEquals(2, parser.getRowKeyColumnIndex());
52    }
53  
54    private void checkParsing(ParsedLine parsed, Iterable<String> expected) {
55      ArrayList<String> parsedCols = new ArrayList<String>();
56      for (int i = 0; i < parsed.getColumnCount(); i++) {
57        parsedCols.add(Bytes.toString(
58            parsed.getLineBytes(),
59            parsed.getColumnOffset(i),
60            parsed.getColumnLength(i)));
61      }
62      if (!Iterables.elementsEqual(parsedCols, expected)) {
63        fail("Expected: " + Joiner.on(",").join(expected) + "\n" + 
64            "Got:" + Joiner.on(",").join(parsedCols));
65      }
66    }
67    
68    private void assertBytesEquals(byte[] a, byte[] b) {
69      assertEquals(Bytes.toStringBinary(a), Bytes.toStringBinary(b));
70    }
71  }