一 : API编程查看文件内容
先上传一文件到HDFS
hadoop fs -put ./zhang /
查看文件是否传上去
hadoop fs -ls /
用命令行客户端查看文件内容
用API编程查看文件内容
代码 :
// 读取HDFS中的文件的内容
@Test
public void testReadContent() throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://marshal:9000/");
FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(new Path("/zhang"));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line=null;
while((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
in.close();
fs.close();
}
结果 :
二 : API编程读取HDFS中的文件的指定偏移量范围内的数据内容
上传一个文件zhang2
查看文件内容 :
API编程产看文件偏移量指定内容
代码 :
// 读取HDFS中的文件的指定偏移量范围内的数据内容
@Test
public void testReadContent2() throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://marshal:9000/");
FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(new Path("/zhang2"));
in.seek(60);
// 读/qingshu.txt中的从60偏移量开始的数据,总共20个字节
byte[] b = new byte[4];
int read = -1;
long count = 0;
while((read=in.read(b))!=-1) {
System.out.print(new String(b,0,read));
count += read;
if(count==20) {
break;
}
}
in.close();
fs.close();
}