use of com.alibaba.otter.node.etl.common.io.EncryptedData in project otter by alibaba.
the class RowDataHttpPipe method saveDbBatch.
// ======================== help method ===================
// 保存对应的dbBatch
private HttpPipeKey saveDbBatch(DbBatch dbBatch) {
RowBatch rowBatch = dbBatch.getRowBatch();
// 转化为proto对象
BatchProto.RowBatch.Builder rowBatchBuilder = BatchProto.RowBatch.newBuilder();
rowBatchBuilder.setIdentity(build(rowBatch.getIdentity()));
// 处理具体的字段rowData
for (EventData eventData : rowBatch.getDatas()) {
BatchProto.RowData.Builder rowDataBuilder = BatchProto.RowData.newBuilder();
rowDataBuilder.setPairId(eventData.getPairId());
rowDataBuilder.setTableId(eventData.getTableId());
if (eventData.getSchemaName() != null) {
rowDataBuilder.setSchemaName(eventData.getSchemaName());
}
rowDataBuilder.setTableName(eventData.getTableName());
rowDataBuilder.setEventType(eventData.getEventType().getValue());
rowDataBuilder.setExecuteTime(eventData.getExecuteTime());
// add by ljh at 2012-10-31
if (eventData.getSyncMode() != null) {
rowDataBuilder.setSyncMode(eventData.getSyncMode().getValue());
}
if (eventData.getSyncConsistency() != null) {
rowDataBuilder.setSyncConsistency(eventData.getSyncConsistency().getValue());
}
// 构造key column
for (EventColumn keyColumn : eventData.getKeys()) {
rowDataBuilder.addKeys(buildColumn(keyColumn));
}
// 构造old key column
if (CollectionUtils.isEmpty(eventData.getOldKeys()) == false) {
for (EventColumn keyColumn : eventData.getOldKeys()) {
rowDataBuilder.addOldKeys(buildColumn(keyColumn));
}
}
// 构造其他 column
for (EventColumn column : eventData.getColumns()) {
rowDataBuilder.addColumns(buildColumn(column));
}
rowDataBuilder.setRemedy(eventData.isRemedy());
rowDataBuilder.setSize(eventData.getSize());
if (StringUtils.isNotEmpty(eventData.getSql())) {
rowDataBuilder.setSql(eventData.getSql());
}
if (StringUtils.isNotEmpty(eventData.getDdlSchemaName())) {
rowDataBuilder.setDdlSchemaName(eventData.getDdlSchemaName());
}
if (StringUtils.isNotEmpty(eventData.getHint())) {
rowDataBuilder.setHint(eventData.getHint());
}
rowDataBuilder.setWithoutSchema(eventData.isWithoutSchema());
// 添加一条rowData记录
rowBatchBuilder.addRows(rowDataBuilder.build());
}
// 处理下FileBatch
FileBatch fileBatch = dbBatch.getFileBatch();
BatchProto.FileBatch.Builder fileBatchBuilder = null;
fileBatchBuilder = BatchProto.FileBatch.newBuilder();
fileBatchBuilder.setIdentity(build(fileBatch.getIdentity()));
// 构造对应的proto对象
for (FileData fileData : fileBatch.getFiles()) {
BatchProto.FileData.Builder fileDataBuilder = BatchProto.FileData.newBuilder();
fileDataBuilder.setPairId(fileData.getPairId());
fileDataBuilder.setTableId(fileData.getTableId());
if (fileData.getNameSpace() != null) {
fileDataBuilder.setNamespace(fileData.getNameSpace());
}
if (fileData.getPath() != null) {
fileDataBuilder.setPath(fileData.getPath());
}
fileDataBuilder.setEventType(fileData.getEventType().getValue());
fileDataBuilder.setSize(fileData.getSize());
fileDataBuilder.setLastModifiedTime(fileData.getLastModifiedTime());
// 添加一条fileData记录
fileBatchBuilder.addFiles(fileDataBuilder.build());
}
// 处理构造对应的文件url
String filename = buildFileName(rowBatch.getIdentity(), ClassUtils.getShortClassName(dbBatch.getClass()));
// 写入数据
File file = new File(htdocsDir, filename);
OutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(file));
com.alibaba.otter.node.etl.model.protobuf.BatchProto.RowBatch rowBatchProto = rowBatchBuilder.build();
// 输出大小
output.write(ByteUtils.int2bytes(rowBatchProto.getSerializedSize()));
// 输出row batch
rowBatchProto.writeTo(output);
com.alibaba.otter.node.etl.model.protobuf.BatchProto.FileBatch fileBatchProto = fileBatchBuilder.build();
// 输出大小
output.write(ByteUtils.int2bytes(fileBatchProto.getSerializedSize()));
// 输出file batch
fileBatchProto.writeTo(output);
output.flush();
} catch (IOException e) {
throw new PipeException("write_byte_error", e);
} finally {
IOUtils.closeQuietly(output);
}
HttpPipeKey key = new HttpPipeKey();
key.setUrl(remoteUrlBuilder.getUrl(rowBatch.getIdentity().getPipelineId(), filename));
key.setDataType(PipeDataType.DB_BATCH);
key.setIdentity(rowBatch.getIdentity());
Pipeline pipeline = configClientService.findPipeline(rowBatch.getIdentity().getPipelineId());
if (pipeline.getParameters().getUseFileEncrypt()) {
// 加密处理
EncryptedData encryptedData = encryptFile(file);
key.setKey(encryptedData.getKey());
key.setCrc(encryptedData.getCrc());
}
return key;
}
use of com.alibaba.otter.node.etl.common.io.EncryptedData in project otter by alibaba.
the class AbstractHttpPipe method encryptFile.
protected EncryptedData encryptFile(File file) {
// 构造校验对象,这里考虑性能只将file path做为加密源
EncryptedData encryptedData = null;
try {
encryptedData = EncryptUtils.encrypt(file.getPath().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// ignore
}
// 写入校验信息
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file, "rw");
long origLength = file.length();
int keyLength = ByteUtils.stringToBytes(encryptedData.getKey()).length;
int crcLength = ByteUtils.stringToBytes(encryptedData.getCrc()).length;
long totalLength = origLength + crcLength + keyLength;
raf.setLength(totalLength);
raf.seek(origLength);
raf.write(ByteUtils.stringToBytes(encryptedData.getKey()), 0, keyLength);
raf.seek(origLength + keyLength);
raf.write(ByteUtils.stringToBytes(encryptedData.getCrc()), 0, crcLength);
} catch (Exception e) {
throw new PipeException("write_encrypted_error", e);
} finally {
IOUtils.closeQuietly(raf);
}
return encryptedData;
}
use of com.alibaba.otter.node.etl.common.io.EncryptedData in project otter by alibaba.
the class AttachmentHttpPipe method archiveFile.
// 处理对应的附件
private HttpPipeKey archiveFile(final FileBatch fileBatch) {
// 处理构造对应的文件url
String filename = buildFileName(fileBatch.getIdentity(), ClassUtils.getShortClassName(fileBatch.getClass()));
File file = new File(htdocsDir, filename);
// 压缩对应的文件数据
List<FileData> fileDatas = fileBatch.getFiles();
Pipeline pipeline = configClientService.findPipeline(fileBatch.getIdentity().getPipelineId());
int poolSize = pipeline.getParameters().getFileLoadPoolSize();
boolean useLocalFileMutliThread = pipeline.getParameters().getUseLocalFileMutliThread();
ArchiveBean archiveBean = getArchiveBean();
// 调整线程池大小
archiveBean.adjustPoolSize(poolSize);
// 设置是否启用local多线程同步
archiveBean.setUseLocalFileMutliThread(useLocalFileMutliThread);
boolean done = archiveBean.pack(file, fileDatas, new ArchiveRetriverCallback<FileData>() {
public InputStream retrive(FileData fileData) {
boolean miss = false;
try {
if (StringUtils.isNotEmpty(fileData.getNameSpace())) {
throw new RuntimeException(fileData + " is not support!");
} else {
File source = new File(fileData.getPath());
if (source.exists() && source.isFile()) {
return new LazyFileInputStream(source);
} else {
miss = true;
return null;
}
}
} finally {
if (miss && logger.isInfoEnabled()) {
MDC.put(OtterConstants.splitPipelineLoadLogFileKey, String.valueOf(fileBatch.getIdentity().getPipelineId()));
logger.info(FileloadDumper.dumpMissFileDatas(fileBatch.getIdentity(), fileData));
}
}
}
});
if (done == false) {
// 直接返回
return null;
}
HttpPipeKey key = new HttpPipeKey();
key.setUrl(remoteUrlBuilder.getUrl(fileBatch.getIdentity().getPipelineId(), filename));
key.setDataType(PipeDataType.FILE_BATCH);
key.setIdentity(fileBatch.getIdentity());
if (encrypt || pipeline.getParameters().getUseFileEncrypt()) {
// 加密处理
EncryptedData encryptedData = encryptFile(file);
key.setKey(encryptedData.getKey());
key.setCrc(encryptedData.getCrc());
}
return key;
}
Aggregations