大概画了一下数据采集的流程(基础套路)
HDFS客户端编程应用场景:数据采集
业务系统采集数据:
获取基本信息:
将数据发到日志系统的服务器上。
日志系统的web服务器通过采集程序将数据发到HDFS上(简单明了就是:把文件传到HDFS)
接下来新建一个项目用代码来展示一下HDFS上传文件:
首先需要把会用到的包导入
hadoop-common-2.7.3.jar
hadoop-2.7.3\share\hadoop\common\lib\下的所有依赖包
hadoop-hdfs-2.7.3.jar
hadoop-hdfs-client-2.7.3.jar
hadoop-2.7.3\share\hadoop\hdfs\lib下的所有依赖包
接下来直接上代码:(上传文件到HDFS,本地可以无需hadoop环境,但是下载的时候,客户端要去访问本地磁盘,它会优先去调hadoop系统里的C语言库来访问本地磁盘,[c语言比java访问本地磁盘效率高],而这个库是在hadoop安装包,所以需要配环境变量)
在windows开发环境中做一些准备工作:
- 在windows的某个路径中解压一份windows版本的hadoop安装包
- 将解压出的hadoop目录配置到windows的环境变量中:HADOOP_HOME
public static void main(String[] args) throws Exception {
/**
* Configuration参数对象的机制:
* 构造时,会加载jar包中的默认配置 xx-default.xml
* 再加载 用户配置xx-site.xml ,覆盖掉默认参数
* 构造完成之后,还可以conf.set("p","v"),会再次覆盖用户配置文件中的参数值
*/
// new Configuration()会从项目的classpath中加载core-default.xml hdfs-default.xml core-site.xml hdfs-site.xml等文件
Configuration conf = new Configuration();
// 指定本客户端上传文件到hdfs时需要保存的副本数为:2
conf.set("dfs.replication", "2");
// 指定本客户端上传文件到hdfs时切块的规格大小:64M
conf.set("dfs.blocksize", "32m");
// 构造一个访问指定HDFS系统的客户端对象: 参数1:——HDFS系统的URI,参数2:——客户端要特别指定的参数,参数3:客户端的身份(用户名)
FileSystem fs = FileSystem.get(new URI("hdfs://hdp-01:9000/"), conf, "root");
// 上传一个文件到HDFS中
fs.copyFromLocalFile(new Path("D:/apache-tomcat-7.0.69.zip"), new Path("/zjx/"));
fs.close();
}
要点:要对hdfs中的文件进行操作,代码中首先需要获得一个hdfs的客户端对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hdp-01:9000"),conf,"root");
利用fs对象的方法进行文件操作
下面我封装一下,再了解下对文件的其他操作:
FileSystem fs = null;
@Before
public void init() throws Exception{
Configuration conf = new Configuration();
conf.set("dfs.replication", "2");
conf.set("dfs.blocksize", "64m");
fs = FileSystem.get(new URI("hdfs://hdp-01:9000/"), conf, "root");
}
/**
* 从HDFS中下载文件到客户端本地磁盘
* @throws IOException
* @throws IllegalArgumentException
*/
@Test
public void testGet() throws IllegalArgumentException, IOException{
fs.copyToLocalFile(new Path("/zjx.txt"), new Path("f:/"));
fs.close();
}
/**
* 在hdfs内部移动文件\修改名称
*/
@Test
public void testRename() throws Exception{
fs.rename(new Path("/zjx.log"), new Path("/zjx/zjxzjx.log"));
fs.close();
}
/**
* 在hdfs中创建文件夹
*/
@Test
public void testMkdir() throws Exception{
fs.mkdirs(new Path("/zjx/zjx"));
fs.close();
}
/**
* 在hdfs中删除文件或文件夹
*/
@Test
public void testRm() throws Exception{
fs.delete(new Path("/zjx"), true);
fs.close();
}
/**
* 查询hdfs指定目录下的文件信息
*/
@Test
public void testLs() throws Exception{
// 只查询文件的信息,不返回文件夹的信息
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), true);
while(iter.hasNext()){
LocatedFileStatus status = iter.next();
System.out.println("文件全路径:"+status.getPath());
System.out.println("块大小:"+status.getBlockSize());
System.out.println("文件长度:"+status.getLen());
System.out.println("副本数量:"+status.getReplication());
System.out.println("块信息:"+Arrays.toString(status.getBlockLocations()));
System.out.println("--------------------------------");
}
fs.close();
}
/**
* 查询hdfs指定目录下的文件和文件夹信息
*/
@Test
public void testLs2() throws Exception{
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status:listStatus){
System.out.println("文件全路径:"+status.getPath());
System.out.println(status.isDirectory()?"这是文件夹":"这是文件");
System.out.println("块大小:"+status.getBlockSize());
System.out.println("文件长度:"+status.getLen());
System.out.println("副本数量:"+status.getReplication());
System.out.println("--------------------------------");
}
fs.close();
}
/**
* 读取hdfs中的文件的内容
*
* @throws IOException
* @throws IllegalArgumentException
*/
@Test
public void testReadData() throws IllegalArgumentException, IOException {
FSDataInputStream in = fs.open(new Path("/test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
in.close();
fs.close();
}
/**
* 读取hdfs中文件的指定偏移量范围的内容
* @throws IOException
* @throws IllegalArgumentException
*/
@Test
public void testRandomReadData() throws IllegalArgumentException, IOException {
FSDataInputStream in = fs.open(new Path("/xx.dat"));
// 将读取的起始位置进行指定
in.seek(12);
// 读16个字节
byte[] buf = new byte[16];
in.read(buf);
System.out.println(new String(buf));
in.close();
fs.close();
}
/**
* 往hdfs中的文件写内容
* @throws IOException
* @throws IllegalArgumentException
*/
@Test
public void testWriteData() throws IllegalArgumentException, IOException {
FSDataOutputStream out = fs.create(new Path("/zjx.jpg"), false);
FileInputStream in = new FileInputStream("D:/images/zjxzjx.jpg");
byte[] buf = new byte[1024];
int read = 0;
while ((read = in.read(buf)) != -1) {
out.write(buf,0,read);
}
in.close();
out.close();
fs.close();
}