主函数
- package test;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.util.ToolRunner;
- public class TxtHbase {
- public static void main(String [] args) throws Exception{ Driver.run(new Configuration(),new THDriver(),args); } }
读取数据时比较简单,编写Mapper函数,读取<key,value>值就行了。
- package test;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.client.Result;
- import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
- import org.apache.hadoop.hbase.mapred.TableMap;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapred.MapReduceBase;
- import org.apache.hadoop.mapred.OutputCollector;
- import org.apache.hadoop.mapred.Reporter;
- public class MapperClass extends MapReduceBase implements
- TableMap<Text, Text> {
- static final String NAME = "GetDataFromHbaseTest";
- private Configuration conf;
- public void map(ImmutableBytesWritable row, Result values,
- OutputCollector<Text, Text> output, Reporter reporter)
- throws IOException {
- StringBuilder sb = new StringBuilder();
- for (Entry<byte[], byte[]> value : values.getFamilyMap(
- "fam1".getBytes()).entrySet()) {
- String cell = value.getValue().toString();
- if (cell != null) {
- sb.append(new String(value.getKey())).append(new String(cell));
- }
- }
- output.collect(new Text(row.get()), new Text(sb.toString()));
- }
要