当前位置:网站首页 > Java基础 > 正文

java打代码基础

package com.hlbdx;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.*;

import org.apache.hadoop.fs.permission.FsPermission;

import org.testng.annotations.Test;

import java.io.IOException;

/**

* @program: bigdata-demo01

* @description: 大数据实例1

* @author: Mr.ZhaoYong

* @create: 2020-01-13 11:50

**/

public class BigDataDemo1 {

@Test

public void mkdirToHdfs() throws IOException {

Configuration configuration = new Configuration();

configuration.set("fs.defaultFS","hdfs://node01:8020");

FileSystem fileSystem = FileSystem.get(configuration);

fileSystem.mkdirs(new Path("/kkb/dir"));

fileSystem.close();

}

@Test

public void uploadFile() throws IOException {

Configuration configuration = new Configuration();

configuration.set("fs.defaultFS","hdfs://node01:8020");

FileSystem fileSystem = FileSystem.get(configuration);

fileSystem.copyFromLocalFile(new Path("F:\testfile\aa.txt"),

new Path("hdfs://node01:8020/kkb/dir"));

fileSystem.close();

}

@Test

public void downloadFile() throws IOException {

Configuration configuration = new Configuration();

configuration.set("fs.defaultFS","hdfs://node01:8020");

FileSystem fileSystem = FileSystem.get(configuration);

java打代码基础 fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/kkb/dir/aa.txt"),

new Path("F:\testfile\bb.txt"));

fileSystem.close();

}

@Test

public void delFile() throws IOException {

Configuration configuration = new Configuration();

configuration.set("fs.defaultFS","hdfs://node01:8020");

FileSystem fileSystem = FileSystem.get(configuration);

fileSystem.deleteOnExit(new Path("hdfs://node01:8020/kkb/dir/aa.txt"));

fileSystem.close();

}

@Test

public void renameFile() throws IOException {

Configuration configuration = new Configuration();

configuration.set("fs.defaultFS","hdfs://node01:8020");

FileSystem fileSystem = FileSystem.get(configuration);

fileSystem.rename(new Path("hdfs://node01:8020/kkb/dir/aa.txt"),

new Path("hdfs://node01:8020/kkb/dir/bb.xml"));

fileSystem.close();

}

@Test

public void listFiles() throws IOException {

Configuration configuration = new Configuration();

configuration.set("fs.defaultFS","hdfs://node01:8020");

FileSystem fileSystem = FileSystem.get(configuration);

RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);

while (locatedFileStatusRemoteIterator.hasNext()){

LocatedFileStatus next = locatedFileStatusRemoteIterator.next();

String name = next.getPath().getName();

System.out.println("name = " + name);

long len = next.getLen();

System.out.println("len = " + len);

FsPermission permission = next.getPermission();

System.out.println("permission = " + permission);

String group = next.getGroup();

System.out.println("group = " + group);

BlockLocation[] blockLocations = next.getBlockLocations();

for (BlockLocation blk:blockLocations){

String[] cachedHosts = blk.getHosts();

for(String host:cachedHosts){

System.out.println("host = " + host);

}

}

}

fileSystem.close();

}

版权声明


相关文章:

  • 以java为基础的社区2024-10-16 15:18:03
  • 惠州java开发免费基础教程2024-10-16 15:18:03
  • java编程基础交给湖南岚鸿精神2024-10-16 15:18:03
  • java基础循环重难点2024-10-16 15:18:03
  • java web开发实战经典基础2024-10-16 15:18:03
  • java零基础语句案例2024-10-16 15:18:03
  • java要电脑基础吗2024-10-16 15:18:03
  • java基础案例教程豆瓣评分2024-10-16 15:18:03
  • java基础包和类2024-10-16 15:18:03
  • java核心基础355讲解2024-10-16 15:18:03