首页 技术 正文
技术 2022年11月19日
0 收藏 978 点赞 5,150 浏览 2198 个字

1.下载文件到本地

public class HdfsUrlTest {

static{

//注册url 让java程序识别hdfs的url

URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());

}

public static void main(String[] args) {

InputStream in = null;

OutputStream out = null;

try {

String file = “hdfs://hadoop-yarn.test.com:8020/user/testi/conf/core-site.xml”;

URL fileUrl = new URL(file);

in = fileUrl.openStream();

out = new FileOutputStream(new File(“d:/core-site.xml”));

//下载文件到本地

IOUtils.copyBytes(in,out,4096, false);

} catch (Exception e) {

e.printStackTrace();

}finally{

IOUtils.closeStream(in);}}}

2.查看集群信息

public static void cluserStatus() throws Exception{

FileSystem fs = HdfsUtil.getFs();

DistributedFileSystem dfs  = (DistributedFileSystem)fs;

FsStatus fss = dfs.getStatus();

DatanodeInfo[] datanodeInfos = dfs.getDataNodeStats();

for(DatanodeInfo datanodeinfo : datanodeInfos){

System.out.println(datanodeinfo.getHostName());}}

3.创建一个文件夹

public void testHDFSMkdir() throws Exception{

String hdfsUrl = “hdfs://192.168.10.11:8020”;

Configuration conf= new Configuration();

FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);

Path path = new Path(“/bigdata”);

fs.mkdirs(path);}

4.创建一个文件

public void testCreateFile() throws Exception{

String hdfsUrl = “hdfs://192.168.10.11:8020”;

Configuration conf= new Configuration();

FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);

Path path = new Path(“/bigdata/a.txt”);

FSDataOutputStream out = fs.create(path);

out.write(“hello hadoop”.getBytes());}

5.文件重命名

public void testRenameFile() throws Exception{

String hdfsUrl = “hdfs://192.168.10.11:8020”;

Configuration conf= new Configuration();

FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);

Path path = new Path(“/bigdata/a.txt”);

Path newPath = new Path(“/bigdata/b.txt”);

System.out.println(fs.rename(path, newPath));}

6.上传文件

public static void write() throws Exception{

FileSystem fs = HdfsUtil.getFs();

OutputStream outStream = fs.create(new Path(“/user/lcc/conf/put-core-site.xml”));

FileInputStream inStream = new FileInputStream(new File(“d:/core-site.xml”));

IOUtils.copyBytes(inStream, outStream, 4096, true);}

Hadoop程序想读取hdfs中的数据,最简单的方法是使用java的URL对象打开一个数据流,并从中读取数据。

需要一个fsUrlStreamHandlerFactory实例调用set过的一个URL,这种方法java虚拟机只能调用一次,缺点是如果程序的其他部分也设置了这个,会导致无法再从hadoop中读取数据。

新方法,需要使用filesystem的api打开一个文件的输入流。

文件在hadoop文件系统中被视为一个hadoop path对象,把一个文件夹或文件路径看做为一个hadoop文件系统的URL。

三种方法。

Get(conf)和get(uri,conf),newInstance

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850