Hadoop的Mapper是怎么从HDFS上读取TextInputFormat数据的


LineRecordReader.next(LongWritable key, Text value)
LineReader.readLine(Text str, int maxLineLength, int maxBytesToConsume)
DataInputStream.read(byte b[]) /* DFSDataInputStream继承此方法 */
DFSInputStream.read(long position, byte[] buffer, int offset, int length)
DFSInputStream.fetchBlockByteRange(LocatedBlock block, long start,long end, byte[] buf, int offset)
BlockReader.readAll(byte[] buf, int offset, int len)
FSInputChecker.readFully(InputStream stm, byte[] buf, int offset, int len)
BlockReader.read(byte[] buf, int off, int len)
FSInputChecker.read(byte[] b, int off, int len)
FSInputChecker.read1(byte b[], int off, int len)
FSInputChecker.readChecksumChunk(byte b[], final int off, final int len)
BlockReader.readChunk(long pos, byte[] buf, int offset, int len, byte[] checksumBuf)
IOUtils.readFullyreadFully( InputStream in, byte buf[], int off, int len)
DataInputStream.read(byte b[], int off, int len)
BufferedInputStream.read(byte b[], int off, int len)
BufferedInputStream.read1(byte[] b, int off, int len)
org.apache.hadoop.net.SocketInputStream.read(byte[] b, int off, int len)
org.apache.hadoop.net.SocketInputStream.read(ByteBuffer dst)
org.apache.hadoop.net.SocketIOWithTimeout.doIO(ByteBuffer buf, int ops)
org.apache.hadoop.net.SocketInputStream.Reader.performIO(ByteBuffer buf)
sun.nio.ch.SocketChannelImpl.read(ByteBuffer buf)
sun.nio.ch.IOUtiil.read(FileDescriptor fd, ByteBuffer dst, long position, NativeDispatcher nd, Object lock)
sun.nio.ch.IOUtiil.readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb, long position, NativeDispatcher nd,Object lock)
sun.nio.ch.SocketDispatcher.read(FileDescriptor fd, long address, int len)
sun.nio.ch.SocketDispatcher.read0(FileDescriptor fd, long address, int len) /* Native Method,根据不同的JDK实现不同 */
Hadoop框架中,Java通常用于编写MapReduce任务或者使用Hadoop的分布式文件系统HDFSHadoop Distributed File System)。读取数据的基本步骤如下: 1. **配置环境**:首先,需要在你的项目中添加Hadoop依赖,并确保Hadoop集群已经配置好。 2. **创建输入format**:Hadoop使用InputFormat接口来指定数据源的格式。例如,如果你的数据存储在文本文件中,可以使用`TextInputFormat`,如果是二进制文件则可能用到`SequenceFileInputFormat`等。 3. **创建RecordReader**:通过InputFormat创建`RecordReader`,它负责从文件中逐行读取数据并提供给map函数处理。 4. **编写Mapper函数**:Mapper接收从RecordReader读来的键值对(key-value),对数据进行处理,生成中间键值对。 5. **将结果传递给Reducer(可选)**:如果使用的是MapReduce模式,Mapper的结果会被排序并发送给Reducer,Reducer进一步处理数据,生成最终结果。 6. **Job提交和执行**:最后,你需要创建一个`Job`实例,设置Mapper、Reducer和其他相关属性,然后提交到Hadoop集群上执行。 ```java Configuration conf = new Configuration(); FileInputFormat.addInputPath(conf, new Path("your_data_path")); Mapper mapperClass = YourMapper.class; Reducer reducerClass = YourReducer.class; Job job = Job.getInstance(conf, "your_job_name"); job.setJarByClass(MyDriverClass.class); job.setMapperClass(mapperClass); job.setReducerClass(reducerClass); // 其他配置... if (job.waitForCompletion(true)) { // 如果成功,打印输出 FileOutputFormat.getOutputPath(job).toString(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值