分页读取hdfs文件

分页读取hdfs文件。

通过流是可以随机读的。

准备数据

放到 /jimo/linux.csv

# cat linux.csv 
0,0
1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9
10,10
11,11
12,12
13,13
14,14
15,15
16,16
17,17
18,18
19,19

第一次读取10行

读取时记录读了多少个字符

final FileSystem fs = getFileSystem();
int batch = 10;
long pos = 0;

final String filePath = "/jimo/linux.csv";
final FSDataInputStream fis = fs.open(new Path(filePath));
try (InputStreamReader inputStreamReader = new InputStreamReader(fis);
	 BufferedReader br = new BufferedReader(inputStreamReader)) {
	String line;
	int row = 1;
	while (row <= batch && (line = br.readLine()) != null) {
		System.out.println(line);
		// System.out.println("pos---" + fis.getPos() + ",ir="); // 每次都是100
		row++;
		// 如果是windows文件,需要加2,因为\r\n, linux和mac只需要加1
		pos += line.length() + 1;
	}
	// 这样获取是一批数据,not ok
	// pos = fis.getPos();
	System.out.println("POS=======" + pos); // 40
}

实验:通过 fis.getPos() 获取的是一批数据的长度,这里实验数据比较小,所以全获取了一直是100,一批默认是16k。

于是通过手动计算每行的长度之和。

第二次读取10行

再接着读后面10行

// 再次从pos读取
final FSDataInputStream fis2 = fs.open(new Path(filePath));
// 偏移
fis2.seek(pos);
try (InputStreamReader inputStreamReader = new InputStreamReader(fis2);
	 BufferedReader br = new BufferedReader(inputStreamReader)) {
	// br.skip(pos);
	String line;
	int row = 1;
	while (row <= batch && (line = br.readLine()) != null) {
		System.out.println(line);
		row++;
	}
}

虽然 getPos 不好使,但 seek还是好使的。

完整代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


   @Test
    public void testSeekRead() throws Exception {

        final FileSystem fs = getFileSystem();
        int batch = 10;
        long pos = 0;

        final String filePath = "/jimo/linux.csv";
        final FSDataInputStream fis = fs.open(new Path(filePath));
        try (InputStreamReader inputStreamReader = new InputStreamReader(fis);
             BufferedReader br = new BufferedReader(inputStreamReader)) {
            String line;
            int row = 1;
            while (row <= batch && (line = br.readLine()) != null) {
                System.out.println(line);
                // System.out.println("pos---" + fis.getPos() + ",ir="); // 每次都是100
                row++;
                // 如果是windows文件,需要加2,因为\r\n, linux和mac只需要加1
                pos += line.length() + 1;
            }
            // 这样获取是一批数据,not ok
            // pos = fis.getPos();
            System.out.println("POS=======" + pos); // 40
        }

        // 再次从pos读取
        final FSDataInputStream fis2 = fs.open(new Path(filePath));
        // 偏移
        fis2.seek(pos);
        try (InputStreamReader inputStreamReader = new InputStreamReader(fis2);
             BufferedReader br = new BufferedReader(inputStreamReader)) {
            // br.skip(pos);
            String line;
            int row = 1;
            while (row <= batch && (line = br.readLine()) != null) {
                System.out.println(line);
                row++;
            }
        }
    }

    private FileSystem getFileSystem() throws IOException, InterruptedException, URISyntaxException {
		// 此Util是从文件中读取hadoop集群配置
        Configuration conf = HadoopUtils.getHadoopConfiguration("D:\\export\\hdfs-conf");
        conf.set("dfs.client.use.datanode.hostname", "true");
        String hdfsPath = "hdfs://10.10.10.10:9000";
        return FileSystem.get(new URI(hdfsPath), conf, "hdfs");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值