略,请同学们自行百度查看,此处不在阐述。
可以参考此博客
Hadoop
后,我们可以在CMD
窗口输入winutils
进行测试,若成功输出,则安装配置成功。若弹出某命令无法找到,则可能是系统缺少基本微软运行库。bin
文件里是否有hadoop.dll
,以及<,没有就去下载对应Hadoop
版本的bin
文件后添加:下载地址:微软基本运行库下载地址。Hadoop
与JDK
的安装环境中不要包含空格,避免出现不必要的错误。<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.7.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.7.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>2.7.2</version></dependency><dependency><groupId&ls</groupId><artifactId&ls</artifactId><version>1.8</version><scope>system</scope><systemPath>${JAVA_HOME}/lib/tools.jar</systemPath></dependency>
</dependencies>
log4j.properties
,在文件中填入如下信息:Logger=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
public class HdfsClient{ @Testpublic void put() throws IOException,InterruptedException {//获取一个HDFs的抽象封装对象Configuration configuration = new Configuration();FileSystem fileSystem = (ate("hdfs://hadoop100:9000"), configuration, user:"xqzhao");//用这个对象操作文件系统pyToLocalFile(new Path("/test"), new Path("d:\"));//关闭文件系统filesystem.close( );}
}
注·:可以将上面的第一步和第三步分别封装为before()
和after()
两个函数,并使用@Before
和@After
两个注解分别进行标注。效果如下:
public class HDFSClient {private Filesystem fs;@Beforepublic void before() throws IOException,InterruptedException {fs = (ate("hdfs://hadoop102:9000"), new Configuration(), user:"xqzhao");System.out.println("Before!!!!!!");}@Testpublic void delete() throws IOException {boolean delete = fs.delete( new Path("/1.txt"),recursive: true);}@Afterpublic void after() throws IOException {system.out.print1n( "After!!!!!!!!!!");fs.close();}
}
客户端去操作HDFS时,是需要一个用户身份的。默认情况下,HDFS客户端API会从JVM中获取一个参数来作为自己的用户身份:
-DHADOOP_USER_NAME=xqzhao
,xqzhao为用户名称。
在Java中操作HDFS,主要涉及以下Class:
Configuration
FileSystem
FileSystem fs = FileSystem. get(conf)
get
方法从conf
中的一个参数fs.defaultFS
的配置值判断具体是什么类型的文件系统- 如果我们的代码中没有指定
fs.defaultFs
,并且工程ClassPath下也没有给定相应的配置,conf
中的默认值就来自于Hadoop的Jar包中的- 默认值为
file:///
,则获取的不是一个DistributedFileSystem的实例,而是一个本地文件系统的客户端对象
@Test
public void CopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {...// 2 上传文件pyFromLocalFile(new Path("e:/"), new Path("/"));// 3 关闭文件系统fileSystem.close();
}
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><property><name&plication</name><value>1</value></property>
</configuration>
参数优先级排序: (1) 客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的默认配置
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 执行下载操作// boolean delSrc 指是否将原文件删除// Path src 指要下载的文件路径// Path dst 指将文件下载到的路径// boolean useRawLocalFileSystem 是否开启文件校验fs.copyToLocalFile(false, new Path("/"), new Path("e:/"), true);// 3 关闭资源fs.close();
}
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 执行删除fs.delete(new Path("/0508/"), true);// 3 关闭资源fs.close();
}
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu"); // 2 修改文件名称fs.rename(new Path("/"), new Path("/"));// 3 关闭资源fs.close();
}
查看文件名称、权限、长度、块信息
`@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{// 1获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu"); // 2 获取文件详情RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);while(listFiles.hasNext()){LocatedFileStatus status = ();// 输出详情// 文件名称System.out.Path().getName());// 长度System.out.Len());// 权限System.out.Permission());// 分组System.out.Group());// 获取存储的块信息BlockLocation[] blockLocations = BlockLocations();for (BlockLocation blockLocation : blockLocations) {// 获取块存储的主机节点String[] hosts = Hosts();for (String host : hosts) {System.out.println(host);}}System.out.println("-----------班长的分割线----------");}// 3 关闭资源
fs.close();
}
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件配置信息Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 判断是文件还是文件夹FileStatus[] listStatus = fs.listStatus(new Path("/"));for (FileStatus fileStatus : listStatus) {// 如果是文件if (fileStatus.isFile()) {System.out.println("f:"Path().getName());}else {System.out.println("d:"Path().getName());}}// 3 关闭资源fs.close();
}
需求:把本地e盘上的文件上传到HDFS根目录
编写代码
@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 创建输入流FileInputStream fis = new FileInputStream(new File("e:/"));// 3 获取输出流FSDataOutputStream fos = fs.create(new Path("/"));// 4 流对拷pyBytes(fis, fos, configuration);// 5 关闭资源IOUtils.closeStream(fos);IOUtils.closeStream(fis);fs.close();
}
需求:从HDFS上下载文件到本地e盘上
编写代码
// 文件下载
@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 获取输入流FSDataInputStream fis = fs.open(new Path("/"));// 3 获取输出流FileOutputStream fos = new FileOutputStream(new File("e:/"));// 4 流的对拷pyBytes(fis, fos, configuration);// 5 关闭资源IOUtils.closeStream(fos);IOUtils.closeStream(fis);fs.close();
}
需求:分块读取HDFS上的大文件,比如根目录下的/hadoop-2.7.
编写代码
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 获取输入流FSDataInputStream fis = fs.open(new Path("/hadoop-2.7."));// 3 创建输出流FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.part1"));// 4 流的拷贝byte[] buf = new byte[1024];for(int i =0 ; i < 1024 * 128; i++){ad(buf);fos.write(buf);}// 5关闭资源IOUtils.closeStream(fis);IOUtils.closeStream(fos);
fs.close();
}
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = (new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2 打开输入流FSDataInputStream fis = fs.open(new Path("/hadoop-2.7."));// 3 定位输入数据位置fis.seek(1024*1024*128);// 4 创建输出流FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.part2"));// 5 流的对拷pyBytes(fis, fos, configuration);// 6 关闭资源IOUtils.closeStream(fis);IOUtils.closeStream(fos);
}
在Window命令窗口中进入到目录E:,然后执行如下命令,对数据进行合并
type hadoop-2.7.part2 >> hadoop-2.7.part1
合并完成后,将hadoop-2.7.part1
重新命名为hadoop-2.7.
。解压发现该tar包非常完整。
cd /export/servers/hadoop-2.7.5
sbin/stop-dfs.sh
cd /export/servers/hadoop-2.7.5/etc/hadoop
l
<property><name>abled</name><value>true</value>
</property>
Hadoop
集群当中有大量的小文件,那么每个小文件都需要维护一份元数据信息,会大大的增加集群管理元数据的内存压力,所以在实际工作当中,如果有必要一定要将小文件合并成大文件进行一起处理HDFS
的Shell命令模式
下,可以通过命令行将很多的hdfs文件
合并成一个大文件下载到本地cd /export/servers
hdfs dfs -getmerge /config/*.xml ./l
@Test
public void mergeFile( ) throws Exception{//获取分布式文件系统FileSystem fileSystem = (new URI("hdf://192.168.52.250:8020"), new Configuration(), "root");FSDataOutputStream outputStream = ate(newPath("/"));//获取本地文件系统LocalFileSystem local = Local(new Configuration());//通过本地文件系统获取文件列表,为一个集合FileStatus[] fileStatuses = local.listStatus(newPath("file:///E:\input" ) );for (FileStatus fileStatus : fileStatuses) {FSDataInputStream inputStream = local.Path());py(inputStream, outputStream);IOUtils.closeQuietly(inputStream);}IOUtils.closeQuietly(outputStream);local.close();fileSystem.close();
}
本文发布于:2024-02-01 00:05:37,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671713932375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |