使用java processbuilder删除cassandra键空间

使用java processbuilder删除cassandra键空间,java,windows,bash,cassandra,Java,Windows,Bash,Cassandra,我有一个清理shell脚本,用于删除除系统键空间之外的所有cassandra键空间。我可以使用java processbuilder执行shell脚本,但是我的脚本指向了错误的cqlsh.py位置(自动添加D:驱动器号) 注意-我的项目工作区位于Windows操作系统的D:驱动器中 cleanup.sh文件-- 这是我的项目中执行shell文件的java类- URL resource = ClassLoader.getSystemResource("cleanup.sh"); File reso

我有一个清理shell脚本,用于删除除系统键空间之外的所有cassandra键空间。我可以使用java processbuilder执行shell脚本,但是我的脚本指向了错误的cqlsh.py位置(自动添加D:驱动器号)

注意-我的项目工作区位于Windows操作系统的D:驱动器中

cleanup.sh文件--

这是我的项目中执行shell文件的java类-

URL resource = ClassLoader.getSystemResource("cleanup.sh");
File resourceFile = Paths.get(resource.toURI()).toFile();
String resourceFilePath = resourceFile.getAbsolutePath();

String cmd[] = {"sh", resourceFilePath,"127.0.0.1"};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);

Process proc = pb.start();
但是当我执行java程序时,我得到以下错误-

---------------------------
   Clearing Cassandra with keyspaces
---------------------------

Get a list of all keyspaces that doesn't contain system prefix
C:\Python27\python.exe: can't open file 'D:\c\Users\Dell\tools\apache-cassandra-2.2.6\bin/cqlsh.py': [Errno 2] No such file or directory

Dropping all keyspaces that doesn't contain system prefix...
DONE

正如您看到的,上面写着无法打开文件“D:\c\Users\Dell\tools\apache-cassandra-2.2.6\bin/cqlsh.py”:[Errno 2]没有这样的文件或目录,如果您注意到文件路径以D:drive开头,我想这是因为我的项目workpspace正在D:drive中运行。那么我该怎么做呢。

也许你可以试试这个:
“D:\c\Users\Dell\tools\apache-cassandra-2.2.6\bin\cqlsh.py”
“D:\\c\\Users\\Dell\\tools\\apache-cassandra-2.2.6\\bin\\cqlsh.py”
而不是:
“D:\c\Users\Dell\tools\apache-cassandra-2.2.2.6\bin/cqlsh.py”
。表达式中有一个斜杠而不是反斜杠。

不用依赖
cqlsh
而是通过Cassandra Java驱动程序更容易和更方便地执行此操作-只需从
元数据
类中获取一个,并删除所有非系统(如果您使用的是DSE,请小心,因为有些系统键空间的名称类似于
DSE\…
)。代码可能如下所示(未测试):

Metadata md=cluster.getMetadata();
List keyspace=md.getkeyspace();
for(键空间元数据ks:键空间){
if(ks.getName().startsWith(“系统”))
继续;
session.execute(“drop keyspace”+ks.getName());
}

谢谢你的回答。是的,我知道这是将shell脚本的代码转换为Java代码的方法。但是我还有很多shell脚本,它们都处理一些与cassandra交互的逻辑,基本上我正在寻找脚本方法。只是出于好奇-你喜欢哪种脚本需要通过
cqlsh
运行,而Java驱动程序无法直接运行?
---------------------------
   Clearing Cassandra with keyspaces
---------------------------

Get a list of all keyspaces that doesn't contain system prefix
C:\Python27\python.exe: can't open file 'D:\c\Users\Dell\tools\apache-cassandra-2.2.6\bin/cqlsh.py': [Errno 2] No such file or directory

Dropping all keyspaces that doesn't contain system prefix...
DONE
Metadata md = cluster.getMetadata();
List<KeyspaceMetadata> keyspaces = md.getKeyspaces();
for (KeyspaceMetadata ks: keyspaces) {
   if (ks.getName().startsWith("system"))
      continue;
   session.execute("drop keyspace " + ks.getName());
}