数据同步Kettle+Commons IO

2020-06-24  本文已影响0人  uq

需求

内网的报告信息,需要在App上查看

内网环境:

App环境:

内网出报告后,将报告同步到阿里云OSS,并将PostgreSQL数据同步到MySQL

实现

数据库同步——Kettle

组成

术语

安装

安装数据库驱动

PostgreSQL驱动
MySQL驱动
下载数据库驱动放置在/xxx/data-integration/lib

编写转换

输入

输出

表字段 目标表的字段
流字段 源数据表的字段
用来查询的关键字 通过此条件匹配数据,匹配成功则更新数据,匹配失败则插入数据
更新字段 设置要同步的字段,更新Y表示该字段执行插入和更新,N表示该字段只执行插入

运行


编写作业

定时调度

转换

连线

运行


文件同步——commons-io

原理

    public void run() {
        while (running) {
            for (final FileAlterationObserver observer : observers) {
                observer.checkAndNotify();
            }
            if (!running) {
                break;
            }
            try {
                Thread.sleep(interval);
            } catch (final InterruptedException ignored) {
                // ignore
            }
        }
    }

    public FileAlterationMonitor() {
        this(10000);
    }
    private void checkAndNotify(final FileEntry parent, final FileEntry[] previous, final File[] files) {
        int c = 0;
        final FileEntry[] current = files.length > 0 ? new FileEntry[files.length] : FileEntry.EMPTY_ENTRIES;
        for (final FileEntry entry : previous) {
            
            while (c < files.length && comparator.compare(entry.getFile(), files[c]) > 0) {
                current[c] = createFileEntry(parent, files[c]);
                // 触发onDirectoryCreate/onFileCreate
                doCreate(current[c]);
                c++;
            }

            if (c < files.length && comparator.compare(entry.getFile(), files[c]) == 0) {
                // 触发onDirectoryChange/onFileChange
                doMatch(entry, files[c]);
                checkAndNotify(entry, entry.getChildren(), listFiles(files[c]));
                current[c] = entry;
                c++;
            } else {
                checkAndNotify(entry, entry.getChildren(), FileUtils.EMPTY_FILE_ARRAY);
                // 触发 onDirectoryDelete/onFileDelete
                doDelete(entry);
            }
        }
        for (; c < files.length; c++) {
            current[c] = createFileEntry(parent, files[c]);
            // 触发onDirectoryCreate/onFileCreate
            doCreate(current[c]);
        }
        parent.setChildren(current);
    }
protected FileAlterationObserver(final FileEntry rootEntry, final FileFilter fileFilter,
                                     final IOCase caseSensitivity) {
        ...
        if (caseSensitivity == null || caseSensitivity.equals(IOCase.SYSTEM)) {
            this.comparator = NameFileComparator.NAME_SYSTEM_COMPARATOR;
        } else if (caseSensitivity.equals(IOCase.INSENSITIVE)) {
            this.comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
        } else {
            this.comparator = NameFileComparator.NAME_COMPARATOR;
        }
    }

依赖

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.7</version>
</dependency>

使用

public class ReportAlterationListener extends FileAlterationListenerAdaptor {

    @Override
    public void onStart(FileAlterationObserver observer) {
        super.onStart(observer);
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
        super.onStop(observer);
    }

    @Override
    public void onFileCreate(File file) {
        // upload to oss
    }
}
public void start() {
        // 设置监控目录和规则
        FileAlterationObserver observer = new FileAlterationObserver(
                "/Users/xxx/",
                FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".pdf")));
        observer.addListener(new ReportAlterationListener());

        // 默认10000ms
        FileAlterationMonitor monitor = new FileAlterationMonitor(3000, observer);
        monitor.start();
}

其他

上一篇下一篇

猜你喜欢

热点阅读