标签:com ip地址 try print boolean comm stat direct length
@Test
public void listStatus() {
Configuration conf = new Configuration();
//使用hdfs的fs功能,客户端就会访问core-site.xml配置文件
//这里是设置core-site.xml中的属性fs.defaultFS和属性值hdfs://192.168.xx.xx:9000
//注意写自己的ip地址
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
//查看的路径
FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
for(int i = 0; i < listStatus.length; ++i)
{ String dpath = listStatus[i].getPath().toString();
System.out.println(dpath);
}
fileSystem.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void lsrtest() {
// 要递归遍历的路径
lsr("/");
}
public static List<String> lsr(String path) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
FileStatus[] listStatus = fileSystem.listStatus(new Path(path));
for(int i = 0; i < listStatus.length; ++i)
{
String abpath = listStatus[i].getPath().toString();
System.out.println(abpath);
boolean directory = listStatus[i].isDirectory();
if (directory) {
lsr(abpath);
}
}
fileSystem.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Test
public void mkdir() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
boolean mkdirs = fileSystem.mkdirs(new Path("/lyx02/lyx002/lyx0002"));
System.out.println(mkdirs?"创建成功":"创建失败");
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void createNewFile() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
boolean createNewFile = fileSystem.createNewFile(new Path("/lyx02/lyx002/22.txt"));
System.out.println(createNewFile?"创建成功":"创建失败");
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void put() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
//hdfs下的路径
FSDataInputStream in = fileSystem.open(new Path("/1.txt"));
FileOutputStream destFile = new FileOutputStream(new File("D:\\111.txt"));
BufferedOutputStream out = new BufferedOutputStream(destFile);
int count = -1;
byte[]buffer = new byte[1024 *8];
while((count=in.read(buffer))!=-1) {
out.write(buffer,0,count);
}
in.close();
out.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void get() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
FSDataOutputStream out = fileSystem.create(new Path("/555.txt"));
FileInputStream srcFile = new FileInputStream(new File("D:\\111.txt"));
BufferedInputStream in = new BufferedInputStream(srcFile);
int count = -1;
byte[]buffer = new byte[1024 *8];
while((count=in.read(buffer))!=-1) {
out.write(buffer,0,count);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void copyFromLocalFile() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
fileSystem.copyFromLocalFile(new Path("D:\\111.txt"), new Path("/666.txt"));
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void copyToLocalFile() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.xx.xx:9000");
try {
FileSystem fileSystem = FileSystem.get(conf);
fileSystem.copyToLocalFile(false,new Path("/666.txt"),new Path("D:\\666.txt"),true);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
标签:com ip地址 try print boolean comm stat direct length
原文地址:https://www.cnblogs.com/lyx666/p/12345386.html