grandes tecnologias de dados HDFS (6) Hadoop - hdfs um operações de I / O de transmissão

Capítulo VI: HDFS um fluxo de operação de I / O (extensão)

O HDFS acima operando API sistema aprendemos quadro são encapsulados. operações próprias para alcançar a API acima de como alcançá-lo? Upload e download de dados pode ser alcançada por meio de fluxo de IO.

6.1 HDFS upload de arquivos

  1. Requisitos: QQ dentro QQWhatsnew.txt para fazer upload de arquivos no disco local para a raiz HDFS do d
  2. Escrever código
	/**
     * 把本地D盘上的/QQ/QQWhatsnew.txt文件上传到HDFS根目录
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI ("hdfs://hadoop101:9000"), configuration, "zhangyong");

        // 2 创建输入流
        FileInputStream fis = new FileInputStream(new File ("d:/QQ/QQWhatsnew.txt"));

        // 3 获取输出流
        FSDataOutputStream fos = fs.create(new Path ("/QQWhatsnew.txt"));

        // 4 流对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

6.2 HDFS download de arquivo

  1. Requisitos: arquivos banhua.txt download a partir de HDFS para o e unidade local
  2. Escrever código
/**
     *  文件下载
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"), configuration, "zhangyong");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/QQWhatsnew.txt"));

        // 3 获取输出流
        FileOutputStream fos = new FileOutputStream(new File("d:/QQWhatsnew.txt"));

        // 4 流的对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

6.3 localizar a leitura de arquivos

  1. / Bloco Hadoop- HDFS ler em arquivos grandes, como o 3.1.2.tar.gz diretório raiz: Demanda
  2. Escrever código
    (1) Baixar o primeiro bloco
   // 1 获取文件系统
        Configuration configuration = new Configuration ();
        FileSystem fs = FileSystem.get (new URI ("hdfs://hadoop101:9000"), configuration, "zhangyong");

        // 2 获取输入流
        FSDataInputStream fis = fs.open (new Path ("/hadoop-3.1.2.tar.gz"));

        // 3 创建输出流
        FileOutputStream fos = new FileOutputStream (new File ("d:/hadoop-3.1.2.tar.gz.part1"));

        // 4 流的拷贝
        byte[] buf = new byte[1024];

        for (int i = 0; i < 1024 * 128; i++) {
            fis.read (buf);
            fos.write (buf);
        }

        // 5关闭资源
        IOUtils.closeStream (fis);
        IOUtils.closeStream (fos);
        fs.close ();
    }

(2) Descarga segundo bloco

@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{

	// 1 获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"), configuration, "zhangyong");
		
	// 2 打开输入流
	FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.2.tar.gz"));
		
	// 3 定位输入数据位置
	fis.seek(1024*1024*128);
		
	// 4 创建输出流
	FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-3.1.2.tar.gz.part2"));
		
	// 5 流的对拷
	IOUtils.copyBytes(fis, fos, configuration);
		
	// 6 关闭资源
	IOUtils.closeStream(fis);
	IOUtils.closeStream(fos);
}

(3) arquivos de mesclagem
para a janela no comando Janela para o diretório E: \, e então execute o seguinte comando, a intercalação de dados
tipo hadoop-3.1.2.tar.gz.part2 >> hadoop-3.1.2.tar.gz . parte 1
após a realização da fusão, o hadoop-3.1.2.tar.gz.part1 renomeado hadoop-2.7.2.tar.gz. Extraindo o alcatrão encontrados pacote muito completo.

Publicado 37 artigos originais · ganhou elogios 7 · vista 1179

Acho que você gosta

Origin blog.csdn.net/zy13765287861/article/details/104656391
Recomendado
Clasificación