HDFS命令所在目录:
/home/hadoop/hadoop-2.7.2/bin
执行命令时需要带./代表当前目录:
./hdfs dfs - ls /
查看环境变量$PATH
echo $PATH
HDFS Shell接口
显示指定目录下文件的详细信息
~~~
hdfs dfs -ls /
hdfs dfs -ls /music
~~~
创建目录
~~~
hdfs dfs -mkdir /music
~~~
创建多级目录
~~~
hdfs dfs -mkdir -p /music/sub
~~~
上传文件
~~~
hdfs dfs -put /home/hadoop/aa.jpg /music
hdfs dfs -put /home/hadoop/aa.jpg hdfs://master:9000/music
~~~
下载文件
~~~
hdfs dfs -copyToLocal /music/aa.jpg /home/hadoop/copy.jpg
~~~
复制文件
~~~
hdfs dfs -cp /music/aa.jpg /music/bb.jpg
~~~
移动和重命名文件
~~~
hdfs dfs -mv /home/hadoop/aa.jpg /music/bb.jpg
hdfs dfs -mv /home/hadoop/aa.jpg /music/sub/bb.jpg
~~~
删除文件
~~~
hdfs dfs -rm /music/aa.jpg
hdfs dfs -rm -r /music/*
~~~
查看文件内容
~~~
hdfs dfs -cat /music/test.txt
~~~
追加内容
~~~
hdfs dfs -appendToFile README.txt /music/test.txt
~~~
HDFS编程
1. 把master上配置好的集群hadoop-2.7.2导出到``window``中。
在sftp命令下:
~~~
lcd e:/tmp
get /home/hadoop/hadoop-2.7.2
~~~
2. 把hadoop-eclipse-plugin-2.7.2.jar放到eclipse的plugins目录下。
在eclipse下配置插件,windows->
show view->other
![](https://box.kancloud.cn/287d8cf9588c6227542ebdede7d3c478_1159x383.png)
![](https://box.kancloud.cn/9451949027f5d68ba11d47ae472b2a93_531x370.png)
3. 在eclipse中选择Windows→Preference按钮,弹出一个对话框,在该对话框左侧会多出一个Hadoop Map/Reduce选项,然后单击此选项,在右侧设置Hadoop的安装目录。
4. 创建MapReduce工程
选择File→Other命令,找到Map/Reduce Project,然后选择它,如下所示:
![](https://box.kancloud.cn/948d87c70eed1e9f10a4d338e5a67ef7_525x500.png)
5. 创建测试类(上传文件), Run As Java Application
~~~
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestHDFS {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
/*conf.set("fs.defaultFS", "hdfs://192.168.231.4:9000");
FileSystem hdfs = FileSystem.get(conf);*/
FileSystem hdfs = FileSystem.get(new URI("hdfs://192.168.231.4:9000"),conf,"hadoop");
Path src = new Path("e:/xx.txt");
Path dst = new Path("/music/xx.txt");
hdfs.copyFromLocalFile(src, dst);
System.out.println("Upload to :"+conf.get("fs.default.name"));
FileStatus files[] = hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
~~~
6. 创建测试类(创建文件)
~~~
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.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestHDFS2 {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
//conf.set("fs.defaultFS", "hdfs://192.168.1.109:9000");
//FileSystem hdfs = FileSystem.get(conf);
FileSystem hdfs = FileSystem.get(new URI("hdfs://192.168.231.4:9000"),conf,"hadoop");
byte[] buff = "hello word".getBytes();
Path dst = new Path("/music/hello.txt");
FSDataOutputStream outputStream = hdfs.create(dst);
outputStream.write(buff,0,buff.length);
outputStream.close();
boolean isExists = hdfs.exists(dst);
System.out.println(isExists);
}
}
~~~
7. 创建测试类(读取文件)
~~~
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestHDFS3 {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(new URI("hdfs://192.168.231.4:9000"),conf,"hadoop");
FSDataInputStream is = hdfs.open(new Path("/music/xx.txt"));
int i = is.read();
while(i != -1)
{
System.out.print((char)i);
i = is.read();
}
is.close();
}
}
~~~
8. 创建测试类(查看/music目录下所有文件)
~~~
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestHDFS4 {
public static void main(String[] args) throws IOException, InterruptedException {
Configuration conf = new Configuration();
String uri = "hdfs://192.168.231.4:9000";
FileSystem hdfs = FileSystem.get(URI.create(uri),conf,"hadoop");
FileStatus[] status = hdfs.listStatus(new Path("/music"));
for(FileStatus sta : status){
System.out.println(sta);
}
}
}
~~~
9. 创建测试类(删除文件及目录)
~~~
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestHDFS5 {
public static void main(String[] args) throws IOException, InterruptedException {
Configuration conf = new Configuration();
String uri = "hdfs://192.168.231.4:9000";
FileSystem hdfs = FileSystem.get(URI.create(uri),conf,"hadoop");
//Path dst = new Path("/music/aa.jpg");
Path dst = new Path("/music");
boolean isdeleted = hdfs.delete(dst, true);
System.out.println(isdeleted);
}
}
~~~
- 空白目录
- 第一章 Linux虚拟机安装
- 第二章 SSH配置
- 第三章 jdk配置
- 第四章 Hadoop配置-单机
- 第五章 Hadoop配置-集群
- 第六章 HDFS
- 第七章 MapReduce
- 7.1 MapReduce(上)
- 7.2 MapReduce(下)
- 7.3 MapReduce实验1 去重
- 7.4 MapReduce实验2 单例排序
- 7.5 MapReduce实验3 TopK
- 7.6 MapReduce实验4 倒排索引
- 第八章 Hive
- Hive安装
- 数据定义
- 数据操作
- 第九章 HBase
- 第十章 SaCa RealRec数据科学平台
- 第十一章 Spark Core
- 第十二章 Spark Streaming
- 第十章 Spark测试题