第VI章:HDFS I / O操作の流れ(拡張子)
私たちは、フレームを学ぶ上でHDFSオペレーティングシステムのAPIが封入されています。それを達成する方法の上記のAPIを達成するために、独自の操作?データのアップロードやダウンロードがIOストリームの方法によって達成することができます。
6.1 HDFSファイルのアップロード
- 要件:DのHDFSのルートにローカルディスク上のファイルをアップロードするQQWhatsnew.txt内部QQ
- コードを記述
/**
* 把本地D盘上的/QQ/QQWhatsnew.txt文件上传到HDFS根目录
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
*/
@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI ("hdfs://hadoop101:9000"), configuration, "zhangyong");
// 2 创建输入流
FileInputStream fis = new FileInputStream(new File ("d:/QQ/QQWhatsnew.txt"));
// 3 获取输出流
FSDataOutputStream fos = fs.create(new Path ("/QQWhatsnew.txt"));
// 4 流对拷
IOUtils.copyBytes(fis, fos, configuration);
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
6.2 HDFSファイルのダウンロード
- ローカルの電子ドライブへHDFSからbanhua.txtダウンロードファイル:要件
- コードを記述
/**
* 文件下载
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
*/
@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"), configuration, "zhangyong");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/QQWhatsnew.txt"));
// 3 获取输出流
FileOutputStream fos = new FileOutputStream(new File("d:/QQWhatsnew.txt"));
// 4 流的对拷
IOUtils.copyBytes(fis, fos, configuration);
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
6.3ファイルの読み取りを見つけます
- / Hadoop- HDFSのブロックは、このようなルートディレクトリ3.1.2.tar.gzなどの大きなファイルで読む:需要を
- 書き込みコード
(1)最初のブロックをダウンロード
// 1 获取文件系统
Configuration configuration = new Configuration ();
FileSystem fs = FileSystem.get (new URI ("hdfs://hadoop101:9000"), configuration, "zhangyong");
// 2 获取输入流
FSDataInputStream fis = fs.open (new Path ("/hadoop-3.1.2.tar.gz"));
// 3 创建输出流
FileOutputStream fos = new FileOutputStream (new File ("d:/hadoop-3.1.2.tar.gz.part1"));
// 4 流的拷贝
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fis.read (buf);
fos.write (buf);
}
// 5关闭资源
IOUtils.closeStream (fis);
IOUtils.closeStream (fos);
fs.close ();
}
(2)第二のブロックをダウンロード
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"), configuration, "zhangyong");
// 2 打开输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.2.tar.gz"));
// 3 定位输入数据位置
fis.seek(1024*1024*128);
// 4 创建输出流
FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-3.1.2.tar.gz.part2"));
// 5 流的对拷
IOUtils.copyBytes(fis, fos, configuration);
// 6 关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
(3)マージファイルを
、その後、\、および次のコマンド、データのマージを実行します。ディレクトリのEへのウィンドウコマンドでウィンドウに
タイプのHadoop-3.1.2.tar.gz.part2 >> Hadoopの-3.1.2.tar.gzを 。 パート1
、合併の完了後に、Hadoopの-3.1.2.tar.gz.part1名前を変更したHadoopの-2.7.2.tar.gz。タールを抽出すると、非常に完全なパッケージを見つけました。