赞
踩
hadoop fs -put ./zhang /
hadoop fs -ls /
- // 读取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();
- }
- // 读取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();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。