spring - 如何使用原始 InputStream 实例化 FSDataInputStream?

标签 spring apache hadoop

我真的不明白如何创建可搜索和可定位可读的输入流...

Resource resource = new ClassPathResource("somefile");
InputStream bla = resource.getInputStream();
FSDataInputStream inputStream = new FSDataInputStream (bla);

在 FS 线上 throw :

java.lang.IllegalArgumentException: In is not an instance of Seekable or PositionedReadable

我需要做模拟,这对我来说是个障碍。

最佳答案

FSDataInputStream构造函数,如下图,定义在FSDataInputStream.java中期望 InputStream 参数是 Seekableinstance> 或 PositionedReadable

public FSDataInputStream(InputStream in) throws IOException 
 {
    super(in);
    if( !(in instanceof Seekable) || !(in instanceof PositionedReadable) ) {
      throw new IllegalArgumentException(
          "In is not an instance of Seekable or PositionedReadable");
    }
 }

希望以下解决方案对您有所帮助。

import java.io.*;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.PositionedReadable;
import org.apache.hadoop.fs.Seekable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class SeekableTest {

    public static void main(String[] args) throws IOException
    {
        Resource resource = new ClassPathResource("somefile");
        InputStream in = resource.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int read;

        while ((read = in.read(buf)) > 0)
          baos.write(buf, 0, read);

        byte data[] = baos.toByteArray();
        SeekableByteArrayInputStream bais = new SeekableByteArrayInputStream(data);
        FSDataInputStream in2 = new FSDataInputStream(bais);
    }

    static class SeekableByteArrayInputStream extends ByteArrayInputStream implements Seekable, PositionedReadable {

        public SeekableByteArrayInputStream(byte[] buf)
        {
            super(buf);
        }
        @Override
        public long getPos() throws IOException{
            return pos;
        }

        @Override
        public void seek(long pos) throws IOException {
          if (mark != 0)
            throw new IllegalStateException();

          reset();
          long skipped = skip(pos);

          if (skipped != pos)
            throw new IOException();
        }

        @Override
        public boolean seekToNewSource(long targetPos) throws IOException {
          return false;
        }

        @Override
        public int read(long position, byte[] buffer, int offset, int length) throws IOException {

          if (position >= buf.length)
            throw new IllegalArgumentException();
          if (position + length > buf.length)
            throw new IllegalArgumentException();
          if (length > buffer.length)
            throw new IllegalArgumentException();

          System.arraycopy(buf, (int) position, buffer, offset, length);
          return length;
        }

        @Override
        public void readFully(long position, byte[] buffer) throws IOException {
          read(position, buffer, 0, buffer.length);

        }

        @Override
        public void readFully(long position, byte[] buffer, int offset, int length) throws IOException {
          read(position, buffer, offset, length);
        }
    }
}

引用:accumulo

关于spring - 如何使用原始 InputStream 实例化 FSDataInputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24910503/

相关文章:

java - Spring-Integration:如何通过网关触发文件读取

java - 在动态创建的类中实例化 spring bean

php - 在 Windows 上使用 XAMPP 安装 PHP YAML 扩展

postgresql - 在 Docker 中使用 Sqoop 导入 PostgreSQL

spring - 如何在 doWithSpring 中获取 beanBuilder 实例

java - Hibernate 和 Spring 事务管理器 : Transaction Not Successfully Started

linux - 安装期间 Google Sitemap 生成器错误 'is not a supported Apache binary or control script'

java - JSP 中的 while 循环执行 javascript 不起作用?

hadoop - 1 个大 Hadoop 和 Hbase 集群 vs 1 个 Hadoop 集群 + 1 个 Hbase 集群

hadoop - Hadoop流随着python下降