计算机操作系统(5)

2019-04-14 13:57

package org.tony.hdfs;

import java.io.IOException; import java.net.URI;

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; 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;

public class HDFSTest { public static void main(String[] args) throws Exception { //uploadLocalFile2HDFS(\ //createNewHDFSFile(\ //String str = new String(readHDFSFile(\ //System.out.println(str); //mkdir(\ //deleteDir(\ //listAll(\

//getDateNodeHost(); //testRename(); testUpload(); } //???HDFS????????н???????? public static void getDateNodeHost() throws IOException{ Configuration conf = getConf();

FileSystem fs=FileSystem.get(conf);

DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); for(int i=0;i

System.out.println(\ } }

/* * upload the local file to the hds * ·?????·?? */ /*public static void uploadLocalFile2HDFS(String s, String d) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); Path src = new Path(s); Path dst = new Path(d); hdfs.copyFromLocalFile(src, dst); hdfs.close(); }*/ /* * create a new file in the hdfs. * notice that the toCreateFilePath is the full path * and write the content to the hdfs file. */ public static void createNewHDFSFile(String toCreateFilePath, String content) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); FSDataOutputStream os = hdfs.create(new Path(toCreateFilePath)); os.write(content.getBytes(\ os.close(); hdfs.close(); } /* * delete the hdfs file * notice that the dst is the full path name */ public static boolean deleteHDFSFile(String dst) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); Path path = new Path(dst); boolean isDeleted = hdfs.delete(path); hdfs.close(); return isDeleted;

} /*

* read the hdfs file content

* notice that the dst is the full path name */

public static byte[] readHDFSFile(String dst) throws Exception { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); // check if the file exists Path path = new Path(dst); if ( fs.exists(path) ) { FSDataInputStream is = fs.open(path); // get the file info to create the buffer FileStatus stat = fs.getFileStatus(path); // create the buffer byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat.getLen()))]; is.readFully(0, buffer); is.close(); fs.close(); return buffer; } else { throw new Exception(\ } } /*

* make a new dir in the hdfs * the dir may like '/tmp/testdir' */

public static void mkdir(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); fs.mkdirs(new Path(dir));

fs.close(); } /*

* delete a dir in the hdfs * dir may like '/tmp/testdir' */

public static void deleteDir(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); fs.delete(new Path(dir)); fs.close(); }

//?????????? hdfs?????????

private static Configuration getConf(){ Configuration conf = new Configuration(); // ??仰??????Щ???????hadoop????????е???? conf.set(\ conf.set(\ return conf; } /**

* @Title: listAll

* @Description: ?г???????????? * @return void ???????? * @throws */

public static void listAll(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); FileStatus[] stats = fs.listStatus(new Path(dir)); for(int i = 0; i < stats.length; ++i) { if (!stats[i].isDir()) { // regular file System.out.println(stats[i].getPath().toString()); } else {

// dir System.out.println(stats[i].getPath().toString()); } // else if(stats[i].()) // { // // is s symlink in linux // System.out.println(stats[i].getPath().toString()); // } } fs.close(); } public static void testRename() throws Exception{ Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf);

Path dst = new Path(\

Path frpath = new Path(\

Path topath = new Path(\

fs.rename(frpath, topath);

FileStatus files[] = fs.listStatus(dst);

for(FileStatus file : files){

System.out.println(file.getPath());

} } public static void testUpload() throws Exception{

Configuration conf = getConf(); FileSystem fs = FileSystem.get(URI.create(\


计算机操作系统(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:谈现代室内设计中的中式风格

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: