Search in sources :

Example 21 with FileData

use of com.alibaba.otter.shared.etl.model.FileData in project otter by alibaba.

the class HttpPipeIntegration method test_rowData.

@Test
public void test_rowData() {
    final Node currentNode = new Node();
    currentNode.setId(1L);
    currentNode.setIp("127.0.0.1");
    currentNode.setParameters(new NodeParameter());
    final Pipeline pipeline = new Pipeline();
    pipeline.getParameters().setRetriever(RetrieverType.ARIA2C);
    // mock一下
    new NonStrictExpectations() {

        {
            configClientService.currentNode();
            returns(currentNode);
            configClientService.findPipeline(anyLong);
            returns(pipeline);
        }
    };
    Identity identity = new Identity();
    identity.setChannelId(100L);
    identity.setPipelineId(100L);
    identity.setProcessId(100L);
    FileBatch fileBatch = new FileBatch();
    fileBatch.setIdentity(identity);
    File localFile = new File(tmp, "httpPipeTest.jpg");
    FileData localFileData = new FileData();
    localFileData.setPath(localFile.getPath());
    localFileData.setEventType(EventType.INSERT);
    localFileData.setLastModifiedTime(new Date().getTime());
    localFileData.setSize(100L);
    localFileData.setTableId(1L);
    fileBatch.getFiles().add(localFileData);
    RowBatch rowBatch = new RowBatch();
    rowBatch.setIdentity(identity);
    EventData eventData = new EventData();
    eventData.setTableId(1L);
    eventData.setSchemaName("otter");
    eventData.setTableName("test");
    eventData.setEventType(EventType.INSERT);
    eventData.setExecuteTime(100L);
    EventColumn primaryKey = new EventColumn();
    primaryKey.setColumnName("id");
    primaryKey.setColumnType(1);
    primaryKey.setColumnValue("1");
    primaryKey.setKey(true);
    primaryKey.setNull(false);
    eventData.getKeys().add(primaryKey);
    EventColumn column = new EventColumn();
    column.setColumnName("name");
    column.setColumnType(1);
    column.setColumnValue("test");
    column.setKey(false);
    column.setNull(false);
    eventData.getColumns().add(column);
    rowBatch.merge(eventData);
    DbBatch dbBatch = new DbBatch();
    dbBatch.setRowBatch(rowBatch);
    dbBatch.setFileBatch(fileBatch);
    HttpPipeKey key = rowDataHttpPipe.put(dbBatch);
    DbBatch target = rowDataHttpPipe.get(key);
    want.bool(target.getRowBatch().getIdentity().equals(identity));
    want.object(target).notNull();
}
Also used : FileBatch(com.alibaba.otter.shared.etl.model.FileBatch) EventColumn(com.alibaba.otter.shared.etl.model.EventColumn) Node(com.alibaba.otter.shared.common.model.config.node.Node) Date(java.util.Date) EventData(com.alibaba.otter.shared.etl.model.EventData) DbBatch(com.alibaba.otter.shared.etl.model.DbBatch) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) HttpPipeKey(com.alibaba.otter.node.etl.common.pipe.impl.http.HttpPipeKey) RowBatch(com.alibaba.otter.shared.etl.model.RowBatch) NodeParameter(com.alibaba.otter.shared.common.model.config.node.NodeParameter) Identity(com.alibaba.otter.shared.etl.model.Identity) File(java.io.File) FileData(com.alibaba.otter.shared.etl.model.FileData) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.node.etl.BaseOtterTest)

Example 22 with FileData

use of com.alibaba.otter.shared.etl.model.FileData in project otter by alibaba.

the class OtterTransformerFactory method transform.

/**
     * 转化FileBatch对象
     */
public Map<Class, BatchObject> transform(FileBatch fileBatch) {
    final Identity identity = translateIdentity(fileBatch.getIdentity());
    List<FileData> fileDatas = fileBatch.getFiles();
    Map<Class, BatchObject> result = new HashMap<Class, BatchObject>();
    // 初始化默认值
    result.put(FileData.class, initBatchObject(identity, FileData.class));
    for (FileData fileData : fileDatas) {
        // 进行转化
        Long tableId = fileData.getTableId();
        Pipeline pipeline = configClientService.findPipeline(identity.getPipelineId());
        // 针对每个同步数据,可能会存在多路复制的情况
        List<DataMediaPair> dataMediaPairs = ConfigHelper.findDataMediaPairByMediaId(pipeline, tableId);
        for (DataMediaPair pair : dataMediaPairs) {
            if (!pair.getSource().getId().equals(tableId)) {
                // 过滤tableID不为源的同步
                continue;
            }
            Object item = fileDataTransformer.transform(fileData, new OtterTransformerContext(identity, pair, pipeline));
            if (item == null) {
                continue;
            }
            // 合并结果
            merge(identity, result, item);
        }
    }
    return result;
}
Also used : DataMediaPair(com.alibaba.otter.shared.common.model.config.data.DataMediaPair) HashMap(java.util.HashMap) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) BatchObject(com.alibaba.otter.shared.etl.model.BatchObject) BatchObject(com.alibaba.otter.shared.etl.model.BatchObject) Identity(com.alibaba.otter.shared.etl.model.Identity) FileData(com.alibaba.otter.shared.etl.model.FileData)

Example 23 with FileData

use of com.alibaba.otter.shared.etl.model.FileData in project otter by alibaba.

the class OtterTransformerFactory method merge.

// =============================== helper method
// ============================
// 将生成的item对象合并到结果对象中
private synchronized void merge(Identity identity, Map<Class, BatchObject> data, Object item) {
    Class clazz = item.getClass();
    BatchObject batchObject = data.get(clazz);
    // 初始化一下对象
    if (batchObject == null) {
        batchObject = initBatchObject(identity, clazz);
        data.put(clazz, batchObject);
    }
    // 进行merge处理
    if (batchObject instanceof RowBatch) {
        ((RowBatch) batchObject).merge((EventData) item);
    } else if (batchObject instanceof FileBatch) {
        ((FileBatch) batchObject).getFiles().add((FileData) item);
    } else {
        throw new TransformException("no support Data[" + clazz.getName() + "]");
    }
}
Also used : FileBatch(com.alibaba.otter.shared.etl.model.FileBatch) RowBatch(com.alibaba.otter.shared.etl.model.RowBatch) BatchObject(com.alibaba.otter.shared.etl.model.BatchObject) TransformException(com.alibaba.otter.node.etl.transform.exception.TransformException) FileData(com.alibaba.otter.shared.etl.model.FileData)

Example 24 with FileData

use of com.alibaba.otter.shared.etl.model.FileData in project otter by alibaba.

the class ArchiveBeanTest method test_simple.

@Test
public void test_simple() {
    File[] files = new File[10];
    List<FileData> fileDatas = new ArrayList<FileData>();
    File archiveFile = new File(tmp, "pack.zip");
    File unpack = new File(tmp, "unpack");
    ArchiveBean archiveBean = new ArchiveBean();
    try {
        archiveBean.afterPropertiesSet();
    } catch (Exception e1) {
        want.fail();
    }
    try {
        // for (int i = 0; i < 10; i++) {
        // files[i] = new File(tmp, "archiveTest_" + i + ".txt");
        // datas[i] = getBlock((i + 1) * 1024);
        // NioUtils.write(datas[i], files[i]);
        //
        // FileData filedata = new FileData();
        // filedata.setPath(files[i].getPath());
        // fileDatas.add(filedata);
        // }
        archiveBean.pack(archiveFile, fileDatas, new ArchiveRetriverCallback<FileData>() {

            public InputStream retrive(FileData source) {
                try {
                    return new FileInputStream(new File(source.getPath()));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    want.fail();
                }
                return null;
            }
        });
        // 开始解压
        List<File> result = archiveBean.unpack(archiveFile, unpack);
        want.bool(result.size() == fileDatas.size());
    // File dir = new File(unpack, archiveFile.getParent());
    // File[] unpackFiles = dir.listFiles();
    //
    // List<File> unpackFilesList = Arrays.asList(unpackFiles);
    // Collections.sort(unpackFilesList); // 排序一下
    // for (int i = 0; i < unpackFilesList.size(); i++) {
    // byte[] data = NioUtils.read(unpackFilesList.get(i));
    // check(data, datas[i]);
    // }
    } catch (Exception e) {
        want.fail();
    } finally {
        for (int i = 0; i < files.length; i++) {
            NioUtils.delete(files[i]);
        }
        NioUtils.delete(archiveFile);
        NioUtils.delete(unpack);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) File(java.io.File) FileData(com.alibaba.otter.shared.etl.model.FileData) ArchiveBean(com.alibaba.otter.node.etl.common.pipe.impl.http.archive.ArchiveBean) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.node.etl.BaseOtterTest)

Aggregations

FileData (com.alibaba.otter.shared.etl.model.FileData)24 File (java.io.File)15 ArrayList (java.util.ArrayList)10 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)9 FileBatch (com.alibaba.otter.shared.etl.model.FileBatch)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)6 Identity (com.alibaba.otter.shared.etl.model.Identity)5 DataMediaPair (com.alibaba.otter.shared.common.model.config.data.DataMediaPair)4 EventColumn (com.alibaba.otter.shared.etl.model.EventColumn)4 EventData (com.alibaba.otter.shared.etl.model.EventData)4 RowBatch (com.alibaba.otter.shared.etl.model.RowBatch)4 FileInputStream (java.io.FileInputStream)4 BaseOtterTest (com.alibaba.otter.node.etl.BaseOtterTest)3 PipeException (com.alibaba.otter.node.etl.common.pipe.exception.PipeException)3 ArchiveBean (com.alibaba.otter.node.etl.common.pipe.impl.http.archive.ArchiveBean)3 LoadException (com.alibaba.otter.node.etl.load.exception.LoadException)3 BatchObject (com.alibaba.otter.shared.etl.model.BatchObject)3 FileNotFoundException (java.io.FileNotFoundException)3 FileOutputStream (java.io.FileOutputStream)3