Hbase mapreduce 作业:所有列值为空

Hbase mapreduce job: all column values are null

我正在尝试从 HBase 数据库在 table 上的 Java 中创建 map-reduce 作业。使用 here 中的示例和互联网上的其他内容,我成功地编写了一个简单的行计数器。但是,尝试编写一个实际对列中的数据执行某些操作的程序是不成功的,因为接收到的字节始终为空。

我的部分驱动程序是这样的:

/* Set main, map and reduce classes */
job.setJarByClass(Driver.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

Scan scan = new Scan();
scan.setCaching(500);
scan.setCacheBlocks(false);

/* Get data only from the last 24h */
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try {
    long now = timestamp.getTime();
    scan.setTimeRange(now - 24 * 60 * 60 * 1000, now);
} catch (IOException e) {
    e.printStackTrace();
}

/* Initialize the initTableMapperJob */
TableMapReduceUtil.initTableMapperJob(
        "dnsr",
        scan,
        Map.class,
        Text.class,
        Text.class,
        job);

/* Set output parameters */
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setOutputFormatClass(TextOutputFormat.class);

如您所见,table 称为 dnsr。我的映射器看起来像这样:

@Override
    public void map(ImmutableBytesWritable row, Result value, Context context)
            throws InterruptedException, IOException {
        byte[] columnValue = value.getValue("d".getBytes(), "fqdn".getBytes());
        if (columnValue == null)
            return;

        byte[] firstSeen = value.getValue("d".getBytes(), "fs".getBytes());
        // if (firstSeen == null)
        //     return;

        String fqdn = new String(columnValue).toLowerCase();
        String fs = (firstSeen == null) ? "empty" : new String(firstSeen);

        context.write(new Text(fqdn), new Text(fs));
    }

一些注意事项:

我在 javascript 中创建了一个简单的 table 扫描器,它正在查询完全相同的 table 和列,我可以清楚地看到那里的值。使用命令行并手动进行查询,我可以清楚地看到 fs 值不为空,它们是稍后可以转换为字符串(表示日期)的字节。

我总是得到 null 可能是什么问题?

谢谢!

更新: 如果我获得特定列族中的所有列,我不会收到 fs。但是,在 javascript return fs 中实现的简单扫描器作为 dnsr table.

中的列
@Override
public void map(ImmutableBytesWritable row, Result value, Context context)
        throws InterruptedException, IOException {
    byte[] columnValue = value.getValue(columnFamily, fqdnColumnName);
    if (columnValue == null)
        return;
    String fqdn = new String(columnValue).toLowerCase();

    /* Getting all the columns */
    String[] cns = getColumnsInColumnFamily(value, "d");
    StringBuilder sb = new StringBuilder();
    for (String s : cns) {
        sb.append(s).append(";");
    }

    context.write(new Text(fqdn), new Text(sb.toString()));
}

我使用了 here 的答案来获取所有列名。

最后,我找到了 'problem'。 Hbase 是一个面向列的数据存储。在这里,数据按列存储和检索,因此如果只需要一些数据,则只能读取相关数据。每个列族都有一个或多个列限定符(列),每一列都有多个单元格。有趣的是每个单元格都有自己的时间戳。

为什么会出现这个问题?好吧,当您进行范围搜索时,只会返回时间戳在该范围内的单元格,因此您最终可能会得到带有 "missing cells" 的行。在我的例子中,我有一个 DNS 记录和其他字段,例如 firstSeenlastSeenlastSeen是每次看到那个域都会更新的字段,firstSeen第一次出现后会保持不变。一旦我将远程 map reduce 作业更改为简单的 map reduce 作业(使用所有时间数据),一切都很好(但作业需要更长的时间才能完成)。

干杯!