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

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

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

1.运行wordcount测试MapReduce

administrator@ubuntu:~$ mkdir inputadministrator@ubuntu:~$ cd inputadministrator@ubuntu:~/input$ echo "hello world" >test1.txtadministrator@ubuntu:~/input$ echo "hello hadoop" >test2.txtadministrator@ubuntu:~/input$ lstest1.txt  test2.txtadministrator@ubuntu:~/input$ cat test1.txthello worldadministrator@ubuntu:~/input$ cd ~administrator@ubuntu:~$ cd Hadoop/hadoop-1.2.1/binadministrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -put ~/input inadministrator@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.txtadministrator@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 ./outFound 3 items-rw-r--r--   1 administrator supergroup          0 2014-07-23 11:34 /user/administrator/out/_SUCCESSdrwxr-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-00000administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -cat ./out/part-r-00000hadoop1hello2world1

2.HDFS命令行操作

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

./hadoop fs -ls

./hadoop fs -put 参数 参数

2.1 下载hdfs上的文件

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

administrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop dfs -lsFound 2 itemsdrwxr-xr-x   - administrator supergroup          0 2014-07-23 11:30 /user/administrator/indrwxr-xr-x   - administrator supergroup          0 2014-07-23 11:34 /user/administrator/outadministrator@ubuntu:~/Hadoop/hadoop-1.2.1/bin$ ./hadoop fs -get in ~/inadministrator@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 inDeleted 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 dsthdfs中路径 * @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);}}

我喜欢旅游,喜欢离开自己过腻歪的城市,

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

相关文章:

你感兴趣的文章:

标签云: