2024年java打代码基础

java打代码基础package com hlbdx import org apache hadoop conf Configuratio import org apache hadoop fs import org apache hadoop fs permission FsPermission import org testng annotations Test import java io

大家好,我是讯享网,很高兴认识大家。

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();

}

小讯
上一篇 2025-01-02 09:12
下一篇 2024-12-27 10:09

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/1544.html