hadoop3.x:Sequence File序列化文件-读文件 作者:马育民 • 2021-04-04 11:41 • 阅读:10112 # 创建maven工程 略 [idea创建maven工程必须修改idea配置](https://www.malaoshi.top/show_1IXSDwcJyst.html "idea创建maven工程必须修改idea配置") # pom.xml ``` org.apache.hadoop hadoop-common 3.0.3 org.apache.hadoop hadoop-hdfs 3.0.3 org.apache.hadoop hadoop-client 3.0.3 ``` 在 pom.xml 点击右键 -> 【maven】 -> 【reimport】 导入依赖 # log4j.properties 打印 hadoop debug 信息 在 `resources` 文件夹下创建 `log4j.properties` 文件,内容如下: ``` log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n ``` # java ``` package top.malaoshi; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.SequenceFile; import org.apache.hadoop.io.Writable; import org.apache.hadoop.util.ReflectionUtils; public class Read { public static void main(String[] args) throws Exception { //设置客户端运行身份 以root去操作访问HDFS System.setProperty("HADOOP_USER_NAME","root"); //Configuration 用于指定相关参数属性 Configuration conf = new Configuration(); //指定要读取的文件 Path path=new Path("hdfs://hadoop1:8020/4.txt.gz"); SequenceFile.Reader.Option option1 = SequenceFile.Reader.file(path); SequenceFile.Reader reader = null; try { reader = new SequenceFile.Reader(conf,option1); //通过反射创建key Writable key = (Writable) ReflectionUtils.newInstance( reader.getKeyClass(), conf); //通过反射创建value Writable value = (Writable) ReflectionUtils.newInstance( reader.getValueClass(), conf); while (reader.next(key, value)) { System.out.println(key+"===="+value); } } finally { IOUtils.closeStream(reader); } } } ``` 原文出处:http://malaoshi.top/show_1IX2AIIojnL0.html