java与hdfs

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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
建立连接的第二种写法,在有些情况下系统会调用localhost系统用户,用这种写法可以把root用户写在语句里:

FileSystem fs=FileSystem(new URI(“hdfs://192.168.237.101:9000”),new Configuration(),“root”);
1
输出测试结果报错的处理:
报错信息:

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.
1
2
解决方案:
在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"));
}
}
1
2
3
4
5
6
7
8
报错:
在这里插入图片描述

Exception in thread “main” org.apache.hadoop.security.AccessControlException: Permission denied: user=chaokeaimuzhi, access=WRITE, inode="/":root:supergroup:drwxr-xr-x
1
获取了当前系统的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"));
}
}
1
2
3
4
5
6
在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"));
}
}
1
2
3
4
5
6
7
8
9
将本地文件写入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]));
}
}
1
2
3
4
5
6
7
8
报错: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]));
}
}
1
2
3
4
5
6
7
使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值