APIの使用法
1.準備
1.1、解凍
Hadoopインストールパッケージを中国語以外のパスに解凍します(例:D:\ users \ hadoop-2.6.0-cdh5.14.2)
1.2、環境変数
WindowsでHADOOP_HOME環境変数を構成します(Windowsでjdk環境変数
を構成する方法と同様)
1.3。新築
開発ツールを使用してMavenプロジェクトを作成する
1.4、依存パッケージ
対応する依存関係をインポートします。これは次のとおりです。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
</dependencies>
注:Mavenリポジトリはcdh関連の依存関係をサポートしていません。Clouderaは
それ自体で関連リポジトリを構築しているため、Clouderaリポジトリをpomに個別に追加する必要があります。
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
1.5、テスト
パッケージcn.big.dataを作成し、HdfsClientクラスを作成し、Junitメソッドを使用し
てディレクトリの作成をテストします。
package cn.big.data;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class HdfsClient {
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"), conf, "root");
// 2 创建目录
fs.mkdirs(new Path("/myApi"));
// 3 关闭资源
fs.close();
}
}
1.6、注意が必要な事項
アイデアがログを印刷できない場合、コンソールには次の情報のみが表示されます
1.log4j:WARNNoappenderscouldbefoundforlogger(org.apache.hadoop.
util.Shell).
2.log4j:WARNPleaseinitializethelog4jsystemproperly.
3.log4j:WARNSeehttp://logging.apache.org/log4j/1.2/faq.html#noconfi
gformoreinfo.
プロジェクトのsrc / main / resourcesディレクトリに新しいファイルを作成し、
「log4j.properties」という名前を付けて、ファイルに入力する必要があります。
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
2.使用方法
2.1、HDFSファイルのアップロード
@Test
public void upLoad() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
// 设置副本存储数量为1,默认是3
configuration.set("dfs.replication","1");
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//上传文件
fs.copyFromLocalFile(new Path("D:\\study\\codes\\hadoop\\HdfsClientDemo\\data\\hdfsDemo\\test.txt"),new Path("/myApi/"));
//关闭资源
fs.close();
System.out.println("ok");
}
2.2、HDFSファイルのダウンロード
@Test
public void downLoad() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//下载文件
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false,new Path("/myApi/test.txt"),new Path("D:\\study\\codes\\hadoop\\HdfsClientDemo\\HdfsTest"),true);
fs.close();
}
2.3、HDFSフォルダーの削除
@Test
public void dRemove() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//删除文件夹
fs.delete(new Path("/myApi/remove"),true);
fs.close();
}
2.4、HDFSファイル名の変更
public void fRename() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//修改文件名
fs.rename(new Path("/myApi/test.txt"),new Path("/myApi/testRename.txt"));
fs.close();
}
2.5、HDFSファイルの詳細ビュー
@Test
public void testListFiles() throws IOException, URISyntaxException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),true);
while (listFiles.hasNext()){
LocatedFileStatus status = listFiles.next();
//输出详情
//文件名称
System.out.println(status.getPath().getName());
//长度
System.out.println(status.getLen());
//权限
System.out.println(status.getPermission());
//组
System.out.println(status.getGroup());
//获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
//获取块存储的主机节点
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-------------------------------");
}
}
2.6、HDFSファイルとフォルダーの判断
@Test
public void testListStatus() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
if (fileStatus.isFile()){
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
}
fs.close();
}
2.7、HDFS I / Oストリーム操作
2.7.1ファイルのアップロード
@Test
public void putFileToHDFS() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//创建输入流
FileInputStream fis = new FileInputStream(new File("D:\\study\\codes\\hadoop\\HdfsClientDemo\\HdfsTest\\test.txt"));
//获取输出流
FSDataOutputStream fos = fs.create(new Path("/myApi/testIO.txt"));
//执行流拷贝
IOUtils.copyBytes(fis,fos,configuration);
//关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
2.7.2ファイルのダウンロード
@Test
public void getFileFromHDFS() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
//获取输入流
FSDataInputStream fis = fs.open(new Path("/myApi/testIO.txt"));
//获取输出流
FileOutputStream fos = new FileOutputStream(new File("D:\\study\\codes\\hadoop\\HdfsClientDemo\\HdfsTest\\IODownload.txt"));
//流的对拷
IOUtils.copyBytes(fis,fos,configuration);
//关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fs.close();
}
2.8、ポジショニングファイルの読み取り
ここでは、hdfsファイルを読み取る任意の場所を設定できることを強調します。これは、mapreduceスライシングinputsplitとsparkパーティションを理解するのに役立ちます。
まず、HadoopインストールパッケージをHDFSファイルシステムにアップロードします
最初の作品をダウンロードする
@Test
public void readFileSeek1() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
FSDataInputStream fis = fs.open(new Path("/myApi//hadoop-2.6.0-cdh5.14.2.tar.gz"));
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Dongue\\Desktop\\seek\\hadoop-2.6.0-cdh5.14.2.tar.gz.part1"));
//流的拷贝
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fis.read(buf);
fos.write(buf);
}
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
ダウンロードに成功
2番目のピースをダウンロードする
@Test
public void readFileSeek2() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.247.130:9000"),configuration,"root");
FSDataInputStream fis = fs.open(new Path("/myApi//hadoop-2.6.0-cdh5.14.2.tar.gz"));
//定位输入数据位置
fis.seek(1024*1024*128);
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Dongue\\Desktop\\seek\\hadoop-2.6.0-cdh5.14.2.tar.gz.part2"));
//流的对拷
IOUtils.copyBytes(fis,fos,configuration);
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
ファイルをマージする
ウィンドウコマンドウィンドウで実行
type hadoop-2.6.0-cdh5.14.2.tar.gz.part2 >> hadoop-2.6.0-cdh5.14.2.tar.gz.part1
合併後、完全なHadoopインストールパッケージファイルが作成されます