2020.9.4课堂笔记(使用idea连接HDFS)

tech2025-12-11  2

Java与hadoop交互

1、Configuration cfg=new Configuration(); 2、cfg.set(“fs.defaultFS”,“hdfs://ip地址:9000”); 3、获取文件系统:FileSystem fs=FileSystem.get(cfg); 1~3合起来的写法,与HDFS文件系统建立连接: FileSystem fs=FileSystem(new URI(“hdfs://ip地址:9000”),new Configuration(),“root”); 4 获取hdfs路径 FSDATAInputStream fsis=fs.open(“hdfs上的文件路径”); 5、查看流

在IDEA中如何给main方法args[]传递参数: Run->EditConfigurations->Program arguments: 参数与参数之间用空格分隔,参数传递进来的值需要加双引号 如图所示: args[0]="/test/java/hdfs-test/README1.txt" args[1]=“Hello.txt”

从HDFS读取数据:

public class TestHDFS { public static void main(String[] args) throws Exception{ //与HDFS文件系统建立连接 Configuration conf=new Configuration(); conf.set("fs.defaultFS","hdfs://192.168.237.101:9000"); FileSystem fs=FileSystem.get(conf); //获取HDFS路径 FSDataInputStream fis = fs.open(new Path(args[0])); //查看流 int tmp; while ((tmp=fis.read())!=-1){ System.out.print((char)tmp); } fis.close(); } }

建立连接的第二种写法,在有些情况下系统会调用localhost系统用户,用这种写法可以把root用户写在语句里:

FileSystem fs=FileSystem(new URI("hdfs://192.168.237.101:9000"),new Configuration(),"root");

输出测试结果报错的处理:

报错信息:

ERROR util.Shell : Failed to locate the winutils binary in the hadoop binary path java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoo binaries.

解决方案: 在window环境下配置安装hadoop 1.使用管理员权限解压hadoop.tar.gz文件,当前使用的版本: hadoop-2.6.0-cdh5.14.2.tar.gz (注:解压软件的管理员权限获得,先找到解压软件的安装目录,然后右键,使用管理员权限运行解压软件的exe运行程序,举例7-zip 安装目录:D:\Program Files\7-Zip,右键管理员身份运行7zFM.exe) 2.解压hadoopBin.rar文件至hadoop安装路径的bin目录下 3.复制bin目录下的hadoop.dll文件至C:\Windows\System32目录下 4.配置hadoop环境变量: %HADOOP_HOME%=hadoop的安装路径 在PATH变量中添加:%HADOOP_HOME%\bin %HADOOP_HOME%\sbin 5.重启电脑即可解决报错问题

在HDFS创建文件夹:

public class TestHDFS { public static void main(String[] args) throws Exception{ Configuration conf=new Configuration(); conf.set("fs.defaultFS","hdfs://192.168.237.101:9000"); FileSystem fs=FileSystem.get(conf); fs.mkdirs(new Path("/hello/nihao/feichanghao")); } }

报错:

Exception in thread "main" org.apache.hadoop.security.AccessControlException: Permission denied: user=chaokeaimuzhi, access=WRITE, inode="/":root:supergroup:drwxr-xr-x

获取了当前系统的username,应该使用替代写法

public class TestHDFS { public static void main(String[] args) throws Exception{ FileSystem fs=FileSystem.get(new URI("hdfs://192.168.237.101:9000"),new Configuration(),"root") fs.mkdirs(new Path("/hello/nihao/feichanghao")); } }

在HDFS中删除文件/文件夹

public class TestHDFS { public static void main(String[] args) throws Exception{ FileSystem fs=FileSystem.get(new URI("hdfs://192.168.237.101:9000"),new Configuration(),"root") //删除文件 fs.deleteOnExit(new Path("/test/java/hdfs-test/README.txt")); //删除文件夹 fs.deleteOnExit(new Path("/hello")); } }

将本地文件写入HDFS

public class TestHDFS { public static void main(String[] args) throws Exception{ FileSystem fs=FileSystem.get(new URI("hdfs://192.168.237.101:9000"),new Configuration(),"root"); //copy从本地文件复制到HDFS //参数(src,dst)将src的文件内容复制到dst文件 fs.copyFromLocalFile(new Path(args[1]),new Path(args[0])); } }

报错:INFO fs.FSInputChecker: Found checksum error: 查原因为:Hadoop在往hdfs上put file的时候,会进行校验。如果有对应的".XXXX.crc"文件,会与其进行比对校验。如果不对就会报上面这个错误。 crc文件是在getmerge文件夹的时候自动生成的,无参数可以关闭。 所以,修改了HadoopUtil,在put file之前check下crc文件是否存在 解决方案:存在同名crc文件则删掉。或者,使用不同的文件名。

从HDFS下载文件到本地

public class TestHDFS { public static void main(String[] args) throws Exception{ FileSystem fs=FileSystem.get(new URI("hdfs://192.168.237.101:9000"),new Configuration(),"root"); //参数(src,dst)将src的文件内容复制到dst文件 fs.copyToLocalFile(new Path(args[0]),new Path(args[1])); } }

使用Hadoop jar执行java的jar包

1、编写测试完成 2、打jar包的时候把main方法指定 3、编译生成jar包,把jar包上传至Linux 4、hadoop jar jar包的路径 main方法类的路径 参数列表 hadoop jar testhdfs.jar cn.kgc.kb09.hdfs.TestHDFS /test/java/hdfs-test/README.txt

File->Project Structure->Artifacts->"+"->JAR->from modules with dependences->选择main Class

->若已经生成过了要删除原来的jar包,并且删除META-INF文件夹-> apply->ok->Build->Build Artifacts->找到刚才生成的Artifact->Build 这时候jar包会出现在out文件夹生成的对应artifacts目录下

在hadoop中运行jar包的方法: hadoop jar jar包的路径 main方法类的路径 参数列表

hadoop jar testhdfs.jar cn.kgc.kb09.hdfs.TestHDFS /test/java/hdfs-test/README.txt
最新回复(0)