block块的获取

用流copy获取第二个block块的内容

public void downloadBySeek() throws IllegalArgumentException, IOException {
		// 获取hadoop的压缩包文件
		RemoteIterator<LocatedFileStatus> listFiles = 
				fileSystem.listFiles(new Path("/list/hadoop-2.7.3.tar.gz"), false);
		// 创建读入流,读入hadoop压缩包.
		FSDataInputStream fsDataInputStream =
				fileSystem.open(new Path("/list/hadoop-2.7.3.tar.gz"));
		// 获取该文件的所有块.
		while (listFiles.hasNext()) {
			// 
			LocatedFileStatus next = listFiles.next();
			// 获取 改文件的所有block块信息
			BlockLocation[] blockLocations = next.getBlockLocations();
			// 循环块
			for (int i = 0; i < blockLocations.length; i++) {
				// 判断是第二个块,进行读取到流中.
				if (i == 1) {
					fsDataInputStream.seek(blockLocations[i].getOffset());
					break;
				}
			}
			
		}
		// 创建输出流,指定位置.
		FileOutputStream foStream = 
				new FileOutputStream("C:/Users/Administrator/Desktop/ll/oo.tar.gz");
		// 进行输出.
		org.apache.commons.compress.utils.IOUtils.copy(fsDataInputStream, foStream);
		
	}

获取所有block内容(通过设置偏移量实现)

// 将所有block块内容下载下来
	public void getBlocks() throws FileNotFoundException, IllegalArgumentException, IOException {
		// 通过fileSystem的listFiles方法可以自动实现递归(自带递归)列出文件类型,返回的是一个远程可迭代对象,需要传入两个参数,第一个参数是服务器路径,第二个参数是否递归
		RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/list/had.zip"), false);
		// 用流打开要读取的文件
		FSDataInputStream open = 
				fileSystem.open(new Path("/list/had.zip"));
		while (listFiles.hasNext()) {
			LocatedFileStatus next = listFiles.next();
			BlockLocation[] locations = 
					next.getBlockLocations();
			for (int i = 0; i < locations.length; i++) {
				// 为了不用每次读取一个输入流,直接设置他的偏移量.
				// seek是对输入流的文件指针做操作的
				open.seek(0);
				FileOutputStream fos = 
						new FileOutputStream("C:/Users/Administrator/Desktop/ll/had1" + i + ".zip");
				// 语法糖
				org.apache.commons.io.IOUtils.copyLarge(open, fos, locations[i].getOffset(), locations[i].getLength());
			}
			
		}
	}

获取所有block内容(用org.apache.commons.io.IOUtils.copyLarge()的四参方法)

// 将所有block块内容下载下来
	public void getBlocksSec() throws FileNotFoundException, IllegalArgumentException, IOException {
		RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/list/had.zip"), false);
		FSDataInputStream open = 
				fileSystem.open(new Path("/list/had.zip"));
		while (listFiles.hasNext()) {
			LocatedFileStatus next = listFiles.next();
			BlockLocation[] locations = 
					next.getBlockLocations();
			Long sum = 0L;
			for (int i = 0; i < locations.length; i++) {
				
				FileOutputStream fos = 
						new FileOutputStream("C:/Users/Administrator/Desktop/ll/had1" + i + ".zip");
				long length = locations[i].getLength();
				long offset = locations[i].getOffset();
				// 每次的读取是一个新的block块
				// 参数一:读入流
				// 参数二:写出流
				// 参数三:读入的偏移量
				// 参数四:正在读取的block块的长度.
				org.apache.commons.io.IOUtils.copyLarge
				(open, fos, sum, locations[i].getLength());
				// 因为开始的偏移量为0,所以sum要在最后进行加.
				sum = length + offset;
			}
			
		}
	}

获取文件内容,通过实例化实现

public void testSeek() throws IllegalArgumentException, IOException {
		BlockInfo blockInfo1 = new BlockInfo(0, 6);
		BlockInfo blockInfo2 = new BlockInfo(6, 6);
		BlockInfo blockInfo3 = new BlockInfo(12, 3);
		List<BlockInfo> blockInfos = new ArrayList<>();
		blockInfos.add(blockInfo1);
		blockInfos.add(blockInfo2);
		blockInfos.add(blockInfo3);
		FSDataInputStream inputStream = fileSystem.open(new Path("/list/in.md"));
		int sum = 0;
		for (int i = 0; i < blockInfos.size(); i++) {
			int offset = blockInfos.get(i).getOffset();
			int length = blockInfos.get(i).getLength();
			FileOutputStream fileOutputStream = new FileOutputStream("C:/Users/Administrator/Desktop/hdfs/output/seek"+i+".txt");
			// 偏移量设置为零,指针移到偏移量为0的位置.
			org.apache.commons.io.IOUtils.copyLarge(inputStream, fileOutputStream, 0, length);
			sum = offset+length;
			
		}
	}

// 实例化类
public class BlockInfo {
	private int offset;
	private int length;

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public BlockInfo() {
		super();
		// TODO Auto-generated constructor stub
	}

	public BlockInfo(int offset, int length) {
		super();
		this.offset = offset;
		this.length = length;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Block Change Tracking(BCT)是 Oracle 数据库中一种技术,用于跟踪在备份之间更改的数据块,以加速增量备份和恢复。如果你想从 BCT 中获取变数据块,可以按照以下步骤进行: 1. 首先,你需要确认 BCT 已经启用。你可以使用以下命令来检查: ``` SELECT status FROM v$block_change_tracking; ``` 如果返回的状态为 ENABLED,则表示 BCT 已经启用。如果状态为 DISABLED,则需要通过以下命令启用 BCT: ``` ALTER DATABASE ENABLE BLOCK CHANGE TRACKING; ``` 2. 接下来,你需要创建一个包含变数据块的文件。你可以使用以下命令来创建该文件: ``` ALTER DATABASE CREATE BLOCK CHANGE TRACKING FILE '/path/to/bct/file'; ``` 其中 '/path/to/bct/file' 是你想要创建的 BCT 文件的路径。请确保该路径对于数据库服务器上的 Oracle 进程是可读写的。 3. 等待一段时间后,BCT 文件将包含所有在最后一次备份之后更改的数据块。你可以使用以下命令来获取变数据块: ``` SELECT file#, block# FROM v$backup_block_change_tracking WHERE status = 'YES'; ``` 该命令将返回一个包含文件号和块号的结果集,表示包含变数据块的数据文件和该文件中的变数据块的块号。 请注意,BCT 文件只包含最后一次备份之后更改的数据块,因此如果你想获取之前的变数据块,需要使用相应的增量备份文件。同时,BCT 文件也需要定期清理或重新创建,以确保其有效性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值