我有一个清理 shell 脚本,旨在删除除系统键空间之外的所有 cassandra 键空间。我可以使用 java processbuilder 执行 shell 脚本,但我的脚本指向 cqlsh.py 的错误位置(自动添加 D: 驱动器号)
注意-我的项目工作区位于 Windows 操作系统的 D: 驱动器
cleanup.sh 文件 --
#!/bin/bash
hostname=$1
echo "---------------------------"
echo " Clearing Cassandra with keyspaces"
echo "---------------------------"
echo -e "\nGet a list of all keyspaces that doesn't contain system prefix"
keyspaces=$(echo desc keyspaces | cqlsh $hostname | xargs -n1 echo | grep -v ^system)
echo -e "\nDropping all keyspaces that doesn't contain system prefix..."
for ks in $keyspaces; do
echo "drop keyspace $ks;" | cqlsh $hostname
#echo Dropped "$ks"
done
echo -e "DONE"
sleep 2
这是我的项目中正在执行 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: 驱动器开头的文件路径,我猜这是因为我的项目工作空间在 D: 驱动器中运行。那么我该如何让它发挥作用。
最佳答案
通过 Cassandra Java 驱动程序而不是依赖 cqlsh
会更容易、更便携 - 只需获取 list of keyspaces来自 Metadata
类,并删除所有非系统(如果您使用的是 DSE,请小心,因为存在名称类似于 dse_...
的系统键空间)。代码可能如下所示(未经测试):
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());
}
关于java - 使用 java processbuilder 删除 cassandra 键空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832928/