Hbase Map / reduce-如何访问表的各个列?

时间:2012-04-17 09:45:28

标签: mapreduce hbase

我有一个名为User的表,有两列,一列名为visitorId,另一列名为friend,这是一个字符串列表。我想检查VisitorId是否在好友列表中。任何人都可以指导我如何访问地图功能中的表列? 我无法想象如何从hbase中的map函数输出数据。 我的代码如下:

ublic class MapReduce {

static class Mapper1 extends TableMapper<ImmutableBytesWritable, Text> {

    private int numRecords = 0;
    private static final IntWritable one = new IntWritable(1);       
    private final IntWritable ONE = new IntWritable(1);
    private Text text = new Text();

    @Override
    public void map(ImmutableBytesWritable row, Result values, Context context) throws IOException {

        //What should i do here??
        ImmutableBytesWritable userKey = new ImmutableBytesWritable(row.get(), 0, Bytes.SIZEOF_INT);

        context.write(userkey,One);     
    }

            //context.write(text, ONE);
        } catch (InterruptedException e) {
            throw new IOException(e);
        }

    }
}



public static void main(String[] args) throws Exception {
    Configuration conf = HBaseConfiguration.create();
    Job job = new Job(conf, "CheckVisitor");
    job.setJarByClass(MapReduce.class);
    Scan scan = new Scan();
    Filter f = new RowFilter(CompareOp.EQUAL,new SubstringComparator("mId2"));
    scan.setFilter(f);
    scan.addFamily(Bytes.toBytes("visitor"));
    scan.addFamily(Bytes.toBytes("friend"));
    TableMapReduceUtil.initTableMapperJob("User", scan, Mapper1.class, ImmutableBytesWritable.class,Text.class, job);

}

}

1 个答案:

答案 0 :(得分:0)

所以结果值实例将包含扫描程序的完整行。 要从结果中获取适当的列,我会执行以下操作: -

VisitorIdVal = value.getColumnLatest(Bytes.toBytes(columnFamily1),Bytes.toBytes(“VisitorId”))

friendlistVal = value.getColumnLatest(Bytes.toBytes(columnFamily2),Bytes.toBytes(“friendlist”))

此处VisitorIdVal和friendlistVal的类型为 keyValue http://archive.cloudera.com/cdh/3/hbase/apidocs/org/apache/hadoop/hbase/KeyValue.html,要获取其值,您可以执行 Bytes.toString(VisitorIdVal.getValue()) 从列中提取值后,您可以在“friendlist”中检查“VisitorId”