博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdfs java API
阅读量:5148 次
发布时间:2019-06-13

本文共 3158 字,大约阅读时间需要 10 分钟。

package com.hadoop.hdfs;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.BlockLocation;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hdfs.DistributedFileSystem;import org.apache.hadoop.hdfs.protocol.DatanodeInfo;import org.apache.hadoop.io.IOUtils;public class HdfsPractise {	public static void main(String[] args) throws IOException {		//makeDir();		//touchFile();		//uploadFromLocal();		//reName();		//delFile();		//searchBlock();		//getNodeInfo();	}		private static void makeDir() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");		FileSystem fs = FileSystem.get(conf);		if(!fs.exists(new Path("/test4"))) {			fs.mkdirs(new Path("/test4"));		}	}	private static void touchFile() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");		FileSystem fs = FileSystem.get(conf);		//fs.setReplication(new Path("/test1"), (short) 1);		FSDataOutputStream out = fs.create(new Path("/test2/sample_age_data.txt"));		FileInputStream in = new FileInputStream("d:\\sample_age_data.txt");		IOUtils.copyBytes(in, out, 1024, true);	}	private static void uploadFromLocal() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");		FileSystem fs = FileSystem.get(conf);		fs.copyFromLocalFile(new Path("d:\\sample_age_data.txt"), new Path("/test2"));	}	private static void reName() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");		FileSystem fs = FileSystem.get(conf);		fs.rename(new Path("/test2/sample_age_data.txt"), new Path("/test2/fff"));	}	private static void delFile() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");		FileSystem fs = FileSystem.get(conf);		fs.delete(new Path("/test2"), true);	}	private static void searchBlock() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");		FileSystem fs = FileSystem.get(conf);		FileStatus fst = fs.getFileStatus(new Path("/test1/aaa.txt"));	    BlockLocation[] bls=fs.getFileBlockLocations(fst, 0, fst.getLen());	    for (int i = 0,h=bls.length; i < h; i++) {	        String[] hosts= bls[i].getHosts();	        System.out.println("block_"+i+"_location:  "+hosts[0]);	    }	}	private static void getNodeInfo() throws IOException {		Configuration conf = new Configuration();		conf.set("fs.defaultFS", "hdfs://118.192.153.114:8020");				FileSystem fs = FileSystem.get(conf);		DistributedFileSystem hdfs = (DistributedFileSystem) fs;	    DatanodeInfo[] dns=hdfs.getDataNodeStats();	    for (int i = 0,h=dns.length; i < h; i++) {	        System.out.println("datanode_"+i+"_name:  "+dns[i].getHostName());	    }	}	}

  

转载于:https://www.cnblogs.com/water-sky/p/5752971.html

你可能感兴趣的文章
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
【Linux】ping命令详解
查看>>
Oracle中包的创建
查看>>
关于PHP会话:session和cookie
查看>>
jQuery on(),live(),trigger()
查看>>
treegrid.bootstrap使用说明
查看>>
[Docker]Docker拉取,上传镜像到Harbor仓库
查看>>
导航,头部,CSS基础
查看>>
[USACO 2017 Feb Gold] Tutorial
查看>>
gzip
查看>>
转负二进制(个人模版)
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
我对于脚本程序的理解——百度轻应用有感
查看>>
面试时被问到的问题
查看>>
注解小结
查看>>
list control控件的一些操作
查看>>
一月流水账
查看>>