1 在Hadoop集群中编译并运行《权威指南》中的例3.2
2 自行在本地文件系统生成一个大约一百多字节的文本文件,写一段程序(可以利用Java API或C API),读入这个文件,并将其第101-120字节的内容写入HDFS成为一个新文件,提供代码和执行结果演示抓图
3 2的反向操作,在HDFS中生成一个大约一百多字节的文本文件,写一段程序(可以利用Java API或C API),读入这个文件,并将其第101-120字节的内容写入本地文件系统成为一个新文件,提供代码和执行结果演示抓图
1
2
package com.keon.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import java.io.*;
import java.net.URI;
/**
- Created by keon on 16/8/14.
*/
public class Exercise2 {
static String PATH = "/exercise2_2";
static String PATH_NEW = "/exercise2_2_1";
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(PATH), conf);
FSDataInputStream in = fs.open(new Path(PATH));
System.out.println("读取全部");
IOUtils.copyBytes(in, System.out, 4096, false);
byte[] buffer = new byte[20];
in.read(101, buffer, 0, 20);
System.out.println("读取101-120,结果");
System.out.println(new String(buffer));
in.close();
InputStream in2 = new ByteArrayInputStream(buffer);
FileSystem fs2 = FileSystem.get(URI.create(PATH_NEW), conf);
OutputStream out2 = fs2.create(new Path(PATH_NEW), new Progressable() {
@Override
public void progress() {
System.out.print("...");
}
});
System.out.print("读取写入:");
IOUtils.copyBytes(in2, out2, 4096, true);
}
}
3
package com.keon.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import java.io.*;
import java.net.URI;
/**
- Created by keon on 16/8/14.
*/
public class Exercise3 {
static String PATH = "/exercise2_3";
static String PATH_NEW = "output/exercise2_3_1";
static String content = "别让生活的压力挤走快乐:不管昨天发生了什么,不管昨天的自己有多难堪,有多无奈,有多苦涩,都过去了,不会再来,也无法更改。就让昨天把所有的苦、所有的累、所有的痛远远地带走吧,而今天,我要收拾心情,重新上路!";
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
InputStream in = new ByteArrayInputStream(content.getBytes());
FileSystem fs = FileSystem.get(URI.create(PATH), conf);
OutputStream out = fs.create(new Path(PATH), new Progressable() {
@Override
public void progress() {
System.out.print("...");
}
});
System.out.print("写入中:");
IOUtils.copyBytes(in, out, 4096, true);
System.out.println("");
FSDataInputStream in2 = fs.open(new Path(PATH));
System.out.println("读取全部");
IOUtils.copyBytes(in2, System.out, 4096, false);
byte[] buffer = new byte[20];
in2.read(101, buffer, 0, 20);
in2.close();
System.out.println("读取101-120,结果");
System.out.println(new String(buffer));
File file = new File(PATH_NEW);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fop = new FileOutputStream(file);
fop.write(buffer);
fop.flush();
fop.close();
}
}