Hadoop分布式文件系统--HDFS

一、配置伪分布模式:
1.配置伪分布式,需要修改$HADOOP_HOME/conf下的三个文件:core-site.xml、hdfs-site.xml和mapred-site.xml。
组件名称 配置文件 属性名称 伪分布模式值
Common core-site.xml fs.default.name hdfs://localhost/
HDFS hdfs-site.xml dfs.replication 1
Mapreduce mapred-site.xml mapred.job.tracker localhost:8021

2.配置SSH(参见上一篇文章)
3.格式化HDFS文件系统
hadoop namenode -format
4.开启hdfs和mapreduce服务:
cd $HADOOP_HOME/bin
sh start-dfs.sh
sh start-mapred.sh
5.使用hadoop fs 命令
(1)从本地向hdfs服务器上传文件。
hadoop fs -copyFromLocal 1901 /user/root/1901.txt
其中,/user/root/1901.txt是默认路径,还有其他两种写法分别是hdfs://localhost/user/root/1901.txt 或者 1901.txt
(2)从hdfs服务器拷贝文件到本地
hadoop fs -copyToLocal hdfs://localhost/user/root/1901.txt 1901.txt
(3)hdfs服务器上创建目录。
hadoop fs -mkdir book
(4)hdfs服务器上列出目录。
hadoop fs -ls .

二、Hadoop HDFS的Filesystem类:
(1)URLCat.java:
/**
 * 
 */
package com.hadoop.mn;

import java.io.InputStream;
import java.net.URL;

import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;

/**
 * @author Chase_Sun
 * 从Hadoop URL中读取数据,使用Java.net.URL对象打开数据流,进而读取数据。
 * 如果程序的其他组件已经声明了一个URLStreamHandlerFactory的实例,将无法再用这种方法从Hadoop中读取数据。  
 *
 */
public class URLCat {
	
	static{
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
	}

	/**
	 * @param args[0]  要读取的文件在hdfs服务器上的网络地址,例如:hdfs://localhost/user/root/qualify.txt
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		InputStream in = null;
		try{
			in = new URL(args[0]).openStream();
			IOUtils.copyBytes(in, System.out, 4096, false);
		}
		finally{
			IOUtils.closeStream(in);
		}

	}

}

执行这个类:
hadoop com/hadoop/mn/URLCat hdfs://localhost/user/root/qulify.txt
运行结果:
He sits on the top of tree
but he dose not to get down.
I am so scared of that.

猜你喜欢

转载自chase-sun.iteye.com/blog/2148163