hadoop学习笔记1.使用shell和JAVA API操作HDFS

我们接着上一节来,上次我们已经把伪分布式环境搭建好了。


OK,我们先来测试一下 ,跑跑自带的例子wordcount


1.运行wordcount测试MapReduce

administrator@ubuntu:~$ mkdir input
administrator@ubuntu:~$ cd input
administrator@ubuntu:~/input$ echo "hello world" >test1.txt
administrator@ubuntu:~/input$ echo "hello hadoop" >test2.txt
administrator@ubuntu:~/input$ ls
test1.txt  test2.txt
administrator@ubuntu:~/input$ cat test1.txt
hello world
administrator@ubuntu:~/input$ cd ~
administrator@ubuntu:~$ cd Hadoop/hadoop-1.2.1/bin
administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -put ~/input in
administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -ls ./in/*\
> 
-rw-r--r--   1 administrator supergroup         12 2014-07-23 11:30 /user/administrator/in/test1.txt
-rw-r--r--   1 administrator supergroup         13 2014-07-23 11:30 /user/administrator/in/test2.txt
administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop jar ../hadoop-examples-1.2.1.jar wordcount in out

首先,在主目录下建立input文件夹    mkdir   input

接着进入input中   使用echo “hello world” 》test1.txt    (意思是,将hello world写入test1.txt文件中)

使用  cat 可以查看txt中的内容

第三步:./hadoop fs -put ~/input in      (将主目录/input   目录下的所有文件上传到 hdfs的in目录下)

./hadoop fs -ls ./in/*\     (查看in目录下的所有文件)   

第四步:上传完成后,我们运行wordcount实例

./hadoop jar ../hadoop-examples-1.2.1.jar wordcount in out

后面的参数in   指的是要计算hdfs中in目录下的文件

out    将计算好的结果保存到out

第五步:我们可以进入localhost:50070中查看结果  也可以使用命令来查看结果

administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -ls ./out
Found 3 items
-rw-r--r--   1 administrator supergroup          0 2014-07-23 11:34 /user/administrator/out/_SUCCESS
drwxr-xr-x   - administrator supergroup          0 2014-07-23 11:34 /user/administrator/out/_logs
-rw-r--r--   1 administrator supergroup         25 2014-07-23 11:34 /user/administrator/out/part-r-00000
administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -cat ./out/part-r-00000
hadoop	1
hello	2
world	1

2.HDFS命令行操作

前面我们已经使用过怎么用命令查看/上传到HDFS了

./hadoop fs -ls

./hadoop fs -put 参数  参数

2.1 下载hdfs上的文件

接下来,我们如何使用命令把hdfs中的文件下载下来呢??

administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop dfs -ls
Found 2 items
drwxr-xr-x   - administrator supergroup          0 2014-07-23 11:30 /user/administrator/in
drwxr-xr-x   - administrator supergroup          0 2014-07-23 11:34 /user/administrator/out
administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -get in ~/in
administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -get out ~/out

./hadoop fs -get 参数  参数    

2.2 删除hdfs上的文件

administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -rmr in
Deleted hdfs://localhost:9000/user/administrator/in


3.使用JAVA API来操作hdfs

这个是官方api的地址: http://hadoop.apache.org/docs/r1.2.1/api/index.html

3.1 使用API操作远程hdfs

首先,新建一个JAVA项目,然后再项目属性中添加Hadoop所需要的java包

有根目录下的和lib目录下的

ok ,下面是简单的封装了用于操作hdfs的工具类
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FSOutputSummer;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;


public class HDFSUTIL {
	static final String HDFS_PATH="hdfs://192.168.1.137:9000/";
	static FileSystem hdfs=null;
	static Configuration conf=null;
	static{
		try {
			conf=new Configuration();
			hdfs=FileSystem.get(new URI(HDFS_PATH), conf);
		} catch (IOException | URISyntaxException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 上传本地文件到HDFS中
	 * @param src 本地文件路径
	 * @param dst	hdfs中路径
	 * @throws IOException
	 * @call HDFSUTIL.Upload(new Path("C:\\PBLog.txt"), new Path("/AppHello"));
	 */
	public static void Upload(Path src,Path dst) throws IOException
	{
		hdfs.copyFromLocalFile(src, dst);
		System.out.println("Upload to "+conf.get("fs.default.name"));
		FileStatus status[] =hdfs.listStatus(dst);
		for (FileStatus fileStatus : status) {
			System.out.println(fileStatus.getPath());
		}
	}
	/**
	 * 创建HDFS文件
	 * @param dst   HDFS中的文件路径
	 * @param buff  将要写入的值
	 * @throws IOException
	 * @ HDFSUTIL.CreateFile(new Path("/AppHello/test.txt") , "Hello Buff".getBytes());
	 */
	public static void CreateFile(Path dst,byte[] buff) throws IOException
	{
		 FSDataOutputStream outputStream=hdfs.create(dst);
		 outputStream.write(buff, 0, buff.length);
		 outputStream.close();
	}
	/**
	 * 重命名文件
	 * @param oldName  hdfs旧文件
	 * @param newName   hdfs新文件名
	 * @throws IOException
	 * @call HDFSUTIL.Rename(new Path("/AppHello/test.txt"), new Path("/AppHello/testnew.txt"));
	 */
	public static void Rename(Path oldName,Path newName) throws IOException
	{
		hdfs.rename(oldName, newName);
	}
	/**
	 * 查看文件状态
	 * @param dst HDFS中的文件路径
	 * @return
	 * @throws IOException
	 * @call
	 * FileStatus status= HDFSUTIL.ViewStatus(new Path("/AppHello/testnew.txt"));
	 *	System.out.println(status.getPermission()+":"+status.getModificationTime());
	 */
	public static FileStatus ViewStatus(Path dst) throws IOException
	{
		return hdfs.getFileStatus(dst);
	}
	/**
	 * 创建目录
	 * @param dst  HDFS中的文件路径
	 * @throws IOException
	 * @call
	 *  HDFSUTIL.CreateDir(new Path("/AppHello1"));
	 */
	public static void CreateDir(Path dst) throws IOException
	{
		hdfs.mkdirs(dst);
	}
	/**
	 * 删除
	 * @param dst HDFS中的文件路径
	 * @throws IOException
	 * @return true is Delete success 
	 * @call
	 * HDFSUTIL.Delete(new Path("/AppHello1"));
	 * HDFSUTIL.Delete(new Path("/AppHello/testnew.txt"));
	 */
	public static boolean Delete(Path dst) throws IOException
	{
		return hdfs.deleteOnExit(dst);
	}
	/**
	 * 读取目录下的所有文件
	 * @param dst
	 * @return 返回文件路径数组
	 * @throws IOException
	 * @call
	 * Path[] paths= HDFSUTIL.ViewdirFile(new Path("/user/administrator/input"));
		for (Path path : paths) {
			System.out.println(path);
		}
	 */
	public static Path[] ViewdirFile(Path dst) throws IOException
	{
	 FileStatus[] status=	hdfs.listStatus(dst);
	 return FileUtil.stat2Paths(status);
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值