2012-09-03 2 views
5

텍스트 파일에서 Map Reduce로 데이터를로드해야합니다. 여러 날 동안 고글 거리고 있지만 내 작업에 적합한 솔루션을 찾지 못했습니다. 시스템에서 text/csv 파일을 읽고 HBASE 테이블에 데이터를 저장하는 모든 메소드 또는 클래스가 있습니까? MapReduce F/w를 알면 어느 누구라도 나를 도울 수 있습니다.System에서 Hbase MapReduce로 텍스트 파일을 읽습니다.

답변

2

텍스트 파일에서 읽기 위해서는 먼저 텍스트 파일이 hdfs에 있어야합니다. 당신은 작업을위한 입력 형식과 outputformat을 지정해야

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해야 org.apache.hadoop.hbase.mapreduce.TableReducer<Text, Text, Text>

샘플 감속기 코드를 확장

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); 


     } 
    } 
} 

샘플 매퍼 클래스

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); 
     } 
    } 
} 
관련 문제