微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【无标题】

Gzip解压缩代码实现·

1.运行虚拟机
2.在Idea的Maven中打包代码
3.将打包文件拖入虚拟机中
4.执行jar包

在Idea的Maven中打包代码

将xml文件压缩成gzip文件

/**
 * @Time : 2021/10/23 15:24
 * @Auther : Carapace
 * @File : DeflateCodeDemo.java
 * Software: IntelliJ IDEA
 */

package com.GzipCodeDemo;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IoUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class GzipCodeDemo {
    public static void main(String[] args) throws Exception {
        String inpath="/home/atguigu/comments.xml";    //自行选择原文件路径

        String outpath="/home/atguigu/comments.gzip";	//自行选择压缩出来文件路径

        Configuration conf = new Configuration();

        //压缩类型
        String codecclassname ="org.apache.hadoop.io.compress.GzipCodec";

        Class<?> codecclass = Class.forName(codecclassname);

        //创建压缩类型的实例的第一种写法
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecclass,conf);

        //依据outoath输出路径进行和输出流包装,产生新的压缩流
        FileOutputStream fos = new FileOutputStream(outpath);

        //创建输出压缩类
        CompressionOutputStream comOut = codec.createOutputStream(fos);

        //依据压缩路径inpath并写入流
        IoUtils.copyBytes(new FileInputStream(inpath),comOut,1024,false);
        comOut.finish();





    }
}

解压gzip文件解压成xml文件

/**
 * @Time : 2021/10/23 18:20
 * @Auther : Carapace
 * @File : DeflateCodeDemo2.java
 * Software: IntelliJ IDEA
 */

package com.GzipCodeDemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IoUtils;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class GzipCodeDemo2 {
    public static void main(String[] args) throws IOException {


        String inputpath = "/home/atguigu/comments.gzip";	//自行选择原文件路径

        String outputpath = "/home/atguigu/gzip_comments.xml";	//自行选择解压出来文件路径

        Configuration conf = new Configuration();

        //直接实例化codec对象
        GzipCodec codec = new GzipCodec();

        //检查并设置conf对象
        ReflectionUtils.setConf(codec, conf);

        //创建压缩流
        CompressionInputStream comIn = codec.createInputStream(new FileInputStream(inputpath));

        //写入流
        IoUtils.copyBytes(comIn, new FileOutputStream(outputpath), 1024);
        comIn.close();
    }
}

然后在Idea中进行打包,在Maven中先进行clean,然后package

在这里插入图片描述

在target中生成文件,找到文件位置,加载到虚拟机中

在这里插入图片描述

在虚拟机中 运行 代码

hadoop jar + 包名 +  copy Reference(包名类名)

注:包名如果觉得长,可以自行更改
包名类名获取方式:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐