1.从hadoop URL读取数据
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
@Test
public void readByURL() throws Exception {
URL _url = new URL("hdfs://master:9000/test.txt");//指定路径
InputStream in = _url.openStream();//打开流
IOUtils.copyBytes(in, System.out, 4096, false);
//传参:System.out:表示输出到控制台
4096:缓冲大小buffsize
false:是否关闭流
IOUtils.closeStream(in);
2.
通过FileSystem API访问读取HDFS数据
/*
* 通过FileSystem API访问读取HDFS数据
* $>hadoop fs -cat /spaceQuota/text.txt
*/
@Test
public void readByFS() throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
String _path = "hdfs://master:9000/spaceQuota/text.txt";
FSDataInputStream in = fs.open(new Path(_path));
IOUtils.copyBytes(in, System.out, 4096, true);
}
3. 通过FileSystem API创建文件夹
/*
* 通过FileSystem API创建文件夹
* $>hadoop fs -mkdir /mkdir_byAPI //没有修改权限,可能会报错
* 注意:修改集群权限: $>hadoop fs -chmod -R a+w /
*/
@Test
public void mkdirByAPI()throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path _path = new Path("/mkdir_byAPI");
fs.mkdirs(_path);
fs.close();
}
4.FileStatus对象,获取文件的相关属性
/*
* 通过FileSystem API获取一些相关参数,有一些过期的方法,如何采用现有的
*/
@Test
public void getPar()throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
FileStatus fstu = fs.getFileStatus(new Path("/spaceQuota/text.txt"));
long _blocksize = fstu.getBlockSize();
System.out.println("getAccessTime="+fstu.getAccessTime());
System.out.println("blockszie="+_blocksize);
System.out.println("getGroup="+fstu.getGroup());
System.out.println("getLen="+fstu.getLen());
System.out.println("getModificationTime="+fstu.getModificationTime());
System.out.println("getOwner="+fstu.getOwner());
System.out.println("getReplication="+fstu.getReplication());
System.out.println("getPath="+fstu.getPath());
}