2012-09-03 8 views
5

Potrzebuję załadować dane z pliku tekstowego do Map Reduce, jestem gogglingiem od wielu dni, ale nie znalazłem żadnego właściwego rozwiązania dla mojej pracy. Czy istnieje metoda lub klasa, która odczytuje plik tekst/csv z systemu i zapisuje dane w tabeli HBASE. To naprawdę bardzo ważne dla mnie, proszę, jeśli ktoś mi pomoże w poznaniu MapReduce F/w.odczytać plik tekstowy z System do Hbase MapReduce

Odpowiedz

2

Do odczytu z pliku tekstowego przede wszystkim plik tekstowy powinien być w formacie hdfs. musisz określić formatu wejściowego i outputformat szuka pracy

Job job = new Job(conf, "example"); 
FileInputFormat.addInputPath(job, new Path("PATH to text file")); 
job.setInputFormatClass(TextInputFormat.class); 
job.setMapperClass(YourMapper.class); 
job.setMapOutputKeyClass(Text.class); 
job.setMapOutputValueClass(Text.class); 
TableMapReduceUtil.initTableReducerJob("hbase_table_name", YourReducer.class, job); 
job.waitForCompletion(true); 

YourReducer powinien rozciąga org.apache.hadoop.hbase.mapreduce.TableReducer<Text, Text, Text>

przykładowy kod reduktor

public class YourReducer extends TableReducer<Text, Text, Text> {  
private byte[] rawUpdateColumnFamily = Bytes.toBytes("colName"); 
/** 
* Called once at the beginning of the task. 
*/ 
@Override 
protected void setup(Context context) throws IOException, InterruptedException { 
// something that need to be done at start of reducer 
} 

@Override 
public void reduce(Text keyin, Iterable<Text> values, Context context) throws IOException, InterruptedException { 
// aggregate counts 
int valuesCount = 0; 
for (Text val : values) { 
    valuesCount += 1; 
    // put date in table 
    Put put = new Put(keyin.toString().getBytes()); 
    long explicitTimeInMs = new Date().getTime(); 
    put.add(rawUpdateColumnFamily, Bytes.toBytes("colName"), explicitTimeInMs,val.toString().getBytes()); 
    context.write(keyin, put); 


     } 
    } 
} 

klasa odwzorowujący Próbka

public static class YourMapper extends Mapper<LongWritable, Text, Text, IntWritable> { 
private final static IntWritable one = new IntWritable(1); 
private Text word = new Text(); 
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
    String line = value.toString(); 
    StringTokenizer tokenizer = new StringTokenizer(line); 
    while (tokenizer.hasMoreTokens()) { 
     word.set(tokenizer.nextToken()); 
     context.write(word, one); 
     } 
    } 
} 
Powiązane problemy