背景: 为何会用到这个呢?
事情是这样的,最近在做nebula图数据库的导入,导入的时候,用的是官方推荐的插件 nebula-import ,这个插件是一个二进制文件,在linux中直接运行它的命令,并指定配置文件 xxx.yaml即可 将csv导入到 图数据库中;所以便衍生了此需求,在java程序中调用linux命令;
Process exec = Runtime.getRuntime().exec("./nebula-importer --config " + yamlFile, null, new File(importerPath));
int resutl = exec.waitFor() ; // 0 失败 1 成功
exec 方法是一个重载方法,这里采用三入参的这个
粘贴一段 Runtime.java中的代码
public Process exec(String command, String[] envp, File dir)
throws IOException {
if (command.length() == 0)
throw new IllegalArgumentException("Empty command");
StringTokenizer st = new StringTokenizer(command);
String[] cmdarray = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++)
cmdarray[i] = st.nextToken();
return exec(cmdarray, envp, dir);
}
commamd 执行的命令
envp 设置环境变量 例如此linux服务器上的java环境变量 我这里无需设置 为null
dir 将要执行命令的 linux 文件所在路径 (例如 这个配置文件在/opt/data/aa.yaml,那么此处写为 new File("/opt/data/"))
至此 其实已经完成了java中执行linux 命令,然后说下这次项目中的重点 难点
1 项目导入需要生成一个yml配置文件,需要用程序实现
2 在java中实现linux中nebula-import 的导入命令执行 上述已经实现
//客户端配置
TemplateBean templateBean = new TemplateBean();
String logPath = filePath + "/logFile";
makeDir(logPath);
templateBean.setLogPath(String.format("%s/%s.log", logPath, fileName));
TemplateBean.ClientSettings clientSettings = new TemplateBean.ClientSettings();
clientSettings.setConnection(new TemplateBean.Connection());
clientSettings.setSpace(importBean.getSpace());
templateBean.setClientSettings(clientSettings);
//文件配置
List<TemplateBean.File> fileList = new ArrayList<>();
// 点
getVertex(importBean, fileList);
//边
getEdge(importBean, fileList);
templateBean.setFiles(fileList);
String yamlFilePath = filePath + "/yamlFile";
makeDir(yamlFilePath);
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(dumperOptions);
String yamlFile = String.format("%s/%s.yaml", yamlFilePath, fileName);
yaml.dump(templateBean, new FileWriter(yamlFile));
生成yaml配置文件的实体
import lombok.Data;
import java.util.List;
/**
* @ClassName= TemplateBean
*/
@Data
public class TemplateBean {
private String version = "v3";
private String description = "web console import";
private Boolean removeTempFiles = false;
private ClientSettings clientSettings;
private String logPath;
private List<File> files;
@Data
public static class File {
private String path;
private String failDataPath;
private Integer batchSize = 60;
private String limit;
private String inOrder;
private String type = "csv";
private CSV csv;
private Schema schema;
}
@Data
public static class CSV {
private Boolean withHeader = false;
private Boolean withLabel = false;
private String delimiter;
}
@Data
public static class Schema {
private String type = "vertex";
private Vertex vertex;
private Edge edge;
}
@Data
public static class Edge {
private String name = "order";
private Boolean withRanking = false;
private List<Prop> props;
private SrcDst srcVID;
private SrcDst dstVID;
private Integer rank;
}
@Data
public static class SrcDst {
private Integer index = 1;
private String function = "";
private String type = "string";
private String prefix = "";
}
@Data
public static class Vertex {
private Vid vid;
private List<Tag> tags;
}
@Data
public static class Vid {
private Integer index = 0;
private String function;
private String type = "string";
private String prefix;
}
@Data
public static class Tag {
private String name = "item";
private List<Prop> props;
}
@Data
public static class Prop {
private String name = "id_single_item";
private String type = "string";
private Integer index = 0;
}
@Data
public static class ClientSettings {
private Integer retry = 3;
private Integer concurrency = 10;
private Integer channelBufferSize = 128;
private String space = "dataImport";
private String postStart;
private String preStop;
private Connection connection;
}
@Data
public static class Connection {
private String user = "root";
private String password = "nebula";
private String address = "127.0.0.1:9669";
}
}
生成的yaml
!!com.example.test.bean.TemplateBean
clientSettings:
channelBufferSize: 128
concurrency: 10
connection:
address: 127.0.0.1:9669
password: nebula
user: root
postStart: null
preStop: null
retry: 3
space: dataImport
description: web console import
files:
- batchSize: 60
csv:
delimiter: null
withHeader: false
withLabel: false
failDataPath: /data1/opt/itemFail.csv
inOrder: null
limit: null
path: /data1/opt/item.csv
schema:
edge: null
type: vertex
vertex:
tags:
- name: item
props:
- index: 0
name: id_single_item
type: string
- index: 0
name: id_single_item
type: string
- index: 0
name: id_single_item
type: string
vid:
function: null
index: 0
prefix: null
type: string
type: csv
logPath: /data1/opt/import.log
removeTempFiles: null
version: v3