Hadoop 我是否必须在自定义RecordReader中的nextkeyvalue()中将图像文件路径作为键进行迭代才能读取多个图像文件?

Hadoop 我是否必须在自定义RecordReader中的nextkeyvalue()中将图像文件路径作为键进行迭代才能读取多个图像文件?,hadoop,mapreduce,Hadoop,Mapreduce,我正在尝试从hdfs读取图像。我已经编写了自己的imageinputformat和imageRecordReader的自定义实现 在ImageRecordReader中,在nextkeyvalue方法()中——这是API,我必须指定用于读取图像的for循环,例如for(Path:Path)等,因为我将输入表单HDFS指定为图像目录。或者它将自己读取图像,因为图像被分割成不同的映射任务&每个映射将获得图像 我在这里有点困惑。我必须在方法initialize或nextkeyvalue()中使用for

我正在尝试从hdfs读取图像。我已经编写了自己的imageinputformat和imageRecordReader的自定义实现

在ImageRecordReader中,在nextkeyvalue方法()中——这是API,我必须指定用于读取图像的for循环,例如for(Path:Path)等,因为我将输入表单HDFS指定为图像目录。或者它将自己读取图像,因为图像被分割成不同的映射任务&每个映射将获得图像


我在这里有点困惑。我必须在方法initialize或nextkeyvalue()中使用for循环吗?如果是,在nextkeyvalue()的initialize()方法中应该在哪里使用它?(查看上面的链接方法详细信息)。

为什么不使用
编写一个
序列文件,而不是实现自己的格式

示例中有一些随机图像,您应该将路径存储在
yourImagePaths
中:

// omitted try / catch and finally statements
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path output = new Path("/tmp/out.seq");

List<String> yourImagePaths = new LinkedList<>();
    // TODO fill your image paths here
SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, output,
    Text.class, BytesWritable.class);

for (String file : yourImagePaths) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    org.apache.hadoop.io.IOUtils.copyBytes(fs.open(new Path(file)), out, conf);
    writer.append(new Text(file), new BytesWritable(out.toByteArray()));
}

writer.close();
//省略了try/catch和finally语句
Configuration conf=新配置();
FileSystem fs=FileSystem.get(conf);
路径输出=新路径(“/tmp/out.seq”);
列出YourImagePath=newLinkedList();
//TODO在此处填充图像路径
SequenceFile.Writer Writer=新的SequenceFile.Writer(fs、conf、output、,
Text.class,BytesWritable.class);
for(字符串文件:YourImagePath){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
org.apache.hadoop.io.IOUtils.copyBytes(fs.open(新路径(文件)),out,conf);
append(新文本(文件),新字节可写(out.toByteArray());
}
writer.close();
基本上,它将路径作为键(用于标识回图像)写入,将图像中的原始字节作为值写入

现在您可以在Hadoop作业中阅读它,它将自动拆分。
你只需要说输入键是
Text
,值是
BytesWritable
,必须使用
SequenceFileInputFormat

你为什么不用
编写一个
SequenceFile
,而不是实现你自己的格式?@Thomas..thanx回答。。我试过了,但输出显示输入图像不是序列文件。因此无法继续…我已经给了您一个答案,说明如何在Hadoop中实际执行此操作。这将生成mapreduce作业的输入。是的,但是为大量图像生成sequence文件将花费太多时间。具体取决于您执行此操作的频率和所需时间写下你的全部输入格式,可能会更快:哦,是的..明白了。。。我不能,因为要从hdfs获取输入,我需要另一个序列文件。@Thomas…有没有更好更快的方法读取图像?因为有很多方法可以读取文本文件,但没有遇到任何用于图像的方法。。