hdfs在生产APP应用中主要是客户端的开发,主要步骤是根据hdfs提供的api构建hdfs的访问客户端对象,并通过该客户端对象操作hdfs上的文件。 案例如下。
package lyl.hadoop;
importorg.Apache.Hadoop.conf.configuration;
import org.apache.hadoop.fs.*;
importorg.Apache.Hadoop.io.I outils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
公共类Hadoop文件系统{
publicstaticvoidmain (字符串[ ] args ) throws IOException { )。
读取(;
}
//*
从HDFS读取文件并输出到控制台
* @throws IOException
*/
公共静态语音读取() throws IOException { )。
配置配置=new配置(;
文件系统文件系统=文件系统. get (configuration;
path path=new path ((/user/flume/hive logs/flum edata.1517279361739 );
fsdatainputstreaminputstream=file system.open (path );
Ioutils.copybytes(Inputstream,System.out,4094 );
I outils.close stream (inputstream );
}
//*
将本地文件写入HDFS
* @throws IOException
*/
公共静态语音写入() throws IOException { )。
配置配置=new配置(;
文件系统文件系统=文件系统. get (configuration;
路径路径=new path ((/user/write.txt );
fsdataoutputstreamfsdataoutputstream=file system.create (路径;
fileinputstreamfileinputstream=new file inputstream (new file ) (d : (自述. txt ) );
I outils.copy bytes (文件输入流,fsdata输出流,4094 );
I outils.close stream (文件输入流;
}
//*
删除HDFS文件
* @throws IOException
*/
公共静态语音删除() throws IOException { )。
配置配置=new配置(;
文件系统文件系统=文件系统. get (configuration;
路径路径=new path ((/user/readme.txt );
file system.delete (路径,true;
}
//*
*创建目录
* @throws IOException
*/
公共静态void mkdir () throws IOException {
配置配置=new配置(;
文件系统文件系统=文件系统. get (configuration;
路径路径=new path ((/lyl );
filesystem.mkdirs(path;
}
//*
*获取文件或目录(如blocksizereplicationownerpermission )的特定信息
* @throws IOException
*/
公共静态语音获取器() throws IOException { )。
配置配置=new配置(;
文件系统文件系统=文件系统. get (configuration;
路径路径=new path ((/user );
filestatusfilestatus=file system.getfilestatus (path;
system.out.println(filestatus;
}
//*
*获取此目录中所有文件的具体信息
* @throws IOException
*/
公共服务语音文件安装(throwsioexception )。
配置配置=new配置(;
文件系统文件系统=文件系统. get (configuration;
路径路径=new path ((/user );
filestatus [ ] filestatus=file system.list status (path );
for (filestatusfilestatus 1: filestatus ) {
system.out.println(filestatus1;
}
}
}
BD明星原创文章。 主讲人: Liuyanling,转载请注明来源: http://big data-star.com/archives/1426