You've already forked DataMate
init datamate
This commit is contained in:
35
runtime/datax/nfswriter/src/main/assembly/package.xml
Normal file
35
runtime/datax/nfswriter/src/main/assembly/package.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<assembly
|
||||
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||
<id></id>
|
||||
<formats>
|
||||
<format>dir</format>
|
||||
</formats>
|
||||
<includeBaseDirectory>false</includeBaseDirectory>
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>plugin.json</include>
|
||||
<include>plugin_job_template.json</include>
|
||||
</includes>
|
||||
<outputDirectory>plugin/writer/nfswriter</outputDirectory>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<directory>target/</directory>
|
||||
<includes>
|
||||
<include>nfswriter-0.0.1-SNAPSHOT.jar</include>
|
||||
</includes>
|
||||
<outputDirectory>plugin/writer/nfswriter</outputDirectory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
|
||||
<dependencySets>
|
||||
<dependencySet>
|
||||
<useProjectArtifact>false</useProjectArtifact>
|
||||
<outputDirectory>plugin/writer/nfswriter/libs</outputDirectory>
|
||||
<scope>runtime</scope>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
</assembly>
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.modelengine.edatamate.plugin.writer.nfswriter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.DirectoryNotEmptyException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 一个简单的 Linux NAS 挂载工具类
|
||||
* 仅适用于 Linux,需具备 sudo 权限或 root。
|
||||
*/
|
||||
public final class MountUtil {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MountUtil.class);
|
||||
|
||||
private MountUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 挂载远程目录
|
||||
*
|
||||
* @param remote 远程地址,如 192.168.1.1:/test
|
||||
* @param mountPoint 本地挂载点,如 /mnt/nas
|
||||
* @param type 文件系统类型:nfs、cifs ...
|
||||
* @param options 额外挂载参数,如 ro,vers=3 或 username=xxx,password=xxx
|
||||
*/
|
||||
public static void mount(String remote, String mountPoint, String type, String options) {
|
||||
try {
|
||||
Path mp = Paths.get(mountPoint);
|
||||
if (isMounted(mountPoint)) {
|
||||
throw new IOException("Already mounted: " + mountPoint);
|
||||
}
|
||||
|
||||
Files.createDirectories(mp);
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder();
|
||||
if (options == null || options.isEmpty()) {
|
||||
pb.command("mount", "-t", type, remote, mountPoint);
|
||||
} else {
|
||||
pb.command("mount", "-t", type, "-o", options, remote, mountPoint);
|
||||
}
|
||||
LOG.info(pb.command().toString());
|
||||
pb.redirectErrorStream(true);
|
||||
Process p = pb.start();
|
||||
StringBuilder output = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
output.append(line).append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
int rc = p.waitFor();
|
||||
if (rc != 0) {
|
||||
throw new RuntimeException("Mount failed, exit=" + rc + ", output: " + output);
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载挂载点
|
||||
*
|
||||
* @param mountPoint 挂载点路径
|
||||
* @throws IOException 卸载失败
|
||||
* @throws InterruptedException 进程等待中断
|
||||
*/
|
||||
public static void umount(String mountPoint) throws IOException, InterruptedException {
|
||||
if (!isMounted(mountPoint)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder("umount", "-l", mountPoint);
|
||||
pb.redirectErrorStream(true);
|
||||
Process p = pb.start();
|
||||
StringBuilder output = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
output.append(line).append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
int rc = p.waitFor();
|
||||
if (rc != 0) {
|
||||
throw new RuntimeException("Mount failed, exit=" + rc + ", output: " + output);
|
||||
}
|
||||
|
||||
// 清理空目录
|
||||
try {
|
||||
Files.deleteIfExists(Paths.get(mountPoint));
|
||||
} catch (DirectoryNotEmptyException ignore) {
|
||||
// 目录非空,保留
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断挂载点是否已挂载
|
||||
*
|
||||
* @param mountPoint 挂载点路径
|
||||
* @return true 表示已挂载
|
||||
* @throws IOException 读取 /proc/mounts 失败
|
||||
*/
|
||||
public static boolean isMounted(String mountPoint) throws IOException {
|
||||
Path procMounts = Paths.get("/proc/mounts");
|
||||
if (!Files.exists(procMounts)) {
|
||||
throw new IOException("/proc/mounts not found");
|
||||
}
|
||||
String expected = mountPoint.trim();
|
||||
List<String> lines = Files.readAllLines(procMounts);
|
||||
return lines.stream()
|
||||
.map(l -> l.split("\\s+"))
|
||||
.filter(a -> a.length >= 2)
|
||||
.anyMatch(a -> a[1].equals(expected));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.modelengine.edatamate.plugin.writer.nfswriter;
|
||||
|
||||
import com.alibaba.datax.common.element.Record;
|
||||
import com.alibaba.datax.common.exception.CommonErrorCode;
|
||||
import com.alibaba.datax.common.exception.DataXException;
|
||||
import com.alibaba.datax.common.plugin.RecordReceiver;
|
||||
import com.alibaba.datax.common.spi.Writer;
|
||||
import com.alibaba.datax.common.util.Configuration;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class NfsWriter extends Writer {
|
||||
public static class Job extends Writer.Job {
|
||||
private Configuration jobConfig;
|
||||
private String mountPoint;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.jobConfig = super.getPluginJobConf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare() {
|
||||
this.mountPoint = "/dataset/mount/" + UUID.randomUUID();
|
||||
this.jobConfig.set("mountPoint", this.mountPoint);
|
||||
new File(this.mountPoint).mkdirs();
|
||||
MountUtil.mount(this.jobConfig.getString("ip") + ":" + this.jobConfig.getString("path"),
|
||||
mountPoint, "nfs", StringUtils.EMPTY);
|
||||
String destPath = this.jobConfig.getString("destPath");
|
||||
new File(destPath).mkdirs();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Configuration> split(int mandatoryNumber) {
|
||||
return Collections.singletonList(this.jobConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void post() {
|
||||
try {
|
||||
MountUtil.umount(this.mountPoint);
|
||||
new File(this.mountPoint).deleteOnExit();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Task extends Writer.Task {
|
||||
private Configuration jobConfig;
|
||||
private String mountPoint;
|
||||
private String destPath;
|
||||
private List<String> files;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.jobConfig = super.getPluginJobConf();
|
||||
this.destPath = this.jobConfig.getString("destPath");
|
||||
this.mountPoint = this.jobConfig.getString("mountPoint");
|
||||
this.files = this.jobConfig.getList("files", Collections.emptyList(), String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startWrite(RecordReceiver lineReceiver) {
|
||||
try {
|
||||
Record record;
|
||||
while ((record = lineReceiver.getFromReader()) != null) {
|
||||
String fileName = record.getColumn(0).asString();
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
continue;
|
||||
}
|
||||
if (!files.isEmpty() && !files.contains(fileName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String filePath = this.mountPoint + "/" + fileName;
|
||||
ShellUtil.runCommand("rsync", Arrays.asList("--no-links", "--chmod=750", "--", filePath,
|
||||
this.destPath + "/" + fileName));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw DataXException.asDataXException(CommonErrorCode.RUNTIME_ERROR, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.modelengine.edatamate.plugin.writer.nfswriter;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ShellUtil {
|
||||
/**
|
||||
* 执行 rsync 命令
|
||||
*
|
||||
* @param cmd 命令
|
||||
* @param extraArgs 额外参数,可为空
|
||||
* @return 命令完整输出(stdout + stderr)
|
||||
* @throws Exception 如果 rsync 返回非 0 或发生 IO 异常
|
||||
*/
|
||||
public static String runCommand(String cmd, List<String> extraArgs) throws Exception {
|
||||
List<String> commands = new ArrayList<>();
|
||||
commands.add(cmd);
|
||||
if (extraArgs != null && !extraArgs.isEmpty()) {
|
||||
commands.addAll(extraArgs);
|
||||
}
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(commands);
|
||||
pb.redirectErrorStream(true); // 合并 stdout & stderr
|
||||
Process p = pb.start();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(p.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line).append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
|
||||
int exit = p.waitFor();
|
||||
if (exit != 0) {
|
||||
throw new RuntimeException("rsync exited with code " + exit + System.lineSeparator() + sb);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
6
runtime/datax/nfswriter/src/main/resources/plugin.json
Normal file
6
runtime/datax/nfswriter/src/main/resources/plugin.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "nfswriter",
|
||||
"class": "com.modelengine.edatamate.plugin.writer.nfswriter.NfsWriter",
|
||||
"description": "write to local",
|
||||
"developer": "modelengine"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "nfswriter",
|
||||
"parameter": {
|
||||
"ip": "127.0.0.1",
|
||||
"path": "/test",
|
||||
"destPath": ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user