标签:系统变量 linux文件 ring interrupt 执行 客户端连接 适合处理大数据 读取 main
优点:
缺点:
HDFS在文件上是分块存储的,2.x版本默认块大小是128M,可以通过dfs.blocksize配置。HDFS块大小取决于磁盘传输速率
sbin/start-dfs.sh
sbin/start-yarn.sh
hadoop fs -help rm
hadoop fs -ls /
hadoop fs -mkdir /study
hadoop fs -mkdir -p /study/mmc
hadoop fs -moveFromLocal test.txt /usr/mmc
hadoop fs -appendToFile test2.txt /usr/mmc/test.txt
hadoop fs -cat /usr/mmc/test.txt
hadoop fs -copyToLocal /usr/mmc/test.txt ./test.txt
hadoop fs -cp /usr/mmc/test.txt /study/mmc
hadoop fs -get /usr/mmc/test.txt
hadoop fs -getmerge /usr/mmc/* merge.txt
hadoop fs -tail /usr/mmc/test.txt
hadoop fs -rm /study/mmc/test.txt
hadoop fs -du -h /
<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>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
public class HDFSClient {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
Configuration configuration=new Configuration();
//获取客户端连接
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.1.21:9000"),configuration,"root");
//创建目录
fs.mkdirs(new Path("/mmc"));
//关闭资源
fs.close();
System.out.println("over");
}
}
很简单,自己看api就会用
public class HDFSClient {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
}
FileSystem fs;
@Before
public void before() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration=new Configuration();
//获取客户端连接
fs=FileSystem.get(new URI("hdfs://192.168.1.21:9000"),configuration,"root");
}
@Test
public void testmkdir() throws URISyntaxException, IOException, InterruptedException {
//创建目录
fs.mkdirs(new Path("/mmc"));
//关闭资源
fs.close();
System.out.println("over");
}
/**
* 上传文件
* @throws IOException
*/
@Test
public void testCopyFromLocalFile() throws IOException {
fs.copyFromLocalFile(new Path("d://a.txt"),new Path("/mmc"));
}
/**
* 删除文件
* @throws IOException
*/
@Test
public void testDelete() throws IOException {
fs.delete(new Path("/mmc"),true);
}
/**
* 查看文件详情
* @throws IOException
*/
@Test
public void testListFiles() throws IOException {
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/usr"), true);
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
if(fileStatus.isFile()){
System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
}
}
}
}
/**
* @description: IO流操作HDFS
* @author: mmc
* @create: 2020-05-28 23:01
**/
public class HDFSIO {
/**
* 上传文件
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void putFileToHDFS() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration=new Configuration();
//获取客户端连接
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.1.21:9000"),configuration,"root");
//获取输入流
FileInputStream fis=new FileInputStream("d://a.txt");
//获取输出流
FSDataOutputStream outputStream = fs.create(new Path("/usr/mmc/a.txt"));
//复制流
IOUtils.copyBytes(fis,outputStream,configuration);
IOUtils.closeStream(fis);
IOUtils.closeStream(outputStream);
fs.close();
}
@Test
public void getFileFromHDFS() throws URISyntaxException, IOException, InterruptedException{
Configuration configuration=new Configuration();
//获取客户端连接
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.1.21:9000"),configuration,"root");
//输入流
FSDataInputStream inputStream = fs.open(new Path("/usr/mmc/a.txt"));
FileOutputStream outputStream=new FileOutputStream("e://b.txt");
IOUtils.copyBytes(inputStream,outputStream,configuration);
IOUtils.closeStream(outputStream);
IOUtils.closeStream(inputStream);
fs.close();
}
/**
* 下载部分文件
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
public void readFileSeek1() throws URISyntaxException, IOException, InterruptedException{
Configuration configuration=new Configuration();
//获取客户端连接
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.1.21:9000"),configuration,"root");
//输入流
FSDataInputStream inputStream = fs.open(new Path("/usr/mmc/a.txt"));
FileOutputStream outputStream=new FileOutputStream("e://b.txt.part1");
//只拷贝部分流(128M)
byte[] buf=new byte[1024];
for (int i=0;i<1024*128;i++){
inputStream.read(buf);
outputStream.write(buf);
}
IOUtils.closeStream(outputStream);
IOUtils.closeStream(inputStream);
fs.close();
}
/**
* 下载剩余部分文件
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
public void readFileSeek2() throws URISyntaxException, IOException, InterruptedException{
Configuration configuration=new Configuration();
//获取客户端连接
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.1.21:9000"),configuration,"root");
//输入流
FSDataInputStream inputStream = fs.open(new Path("/usr/mmc/a.txt"));
//指定起点开始读取
inputStream.seek(1024*1024*128);
FileOutputStream outputStream=new FileOutputStream("e://b.txt.part2");
IOUtils.copyBytes(inputStream,outputStream,configuration);
IOUtils.closeStream(outputStream);
IOUtils.closeStream(inputStream);
fs.close();
}
}
标签:系统变量 linux文件 ring interrupt 执行 客户端连接 适合处理大数据 读取 main
原文地址:https://www.cnblogs.com/javammc/p/13028140.html