该问题来自社区帖: https://bbs.csdn.net/topics/615484669.为符合问答规范, 该问题经过ChatGPT优化
以下是代码:
public class Test {
public static void main(String[] args) throws IOException {
GalaxyBaseClient client = new GalaxyBaseClient("localhost", 8180);
String query = "g.V().hasLabel('person').has('age', gt(20)).values('name', 'age');";
GremlinResultSet result = client.submit(query);
FileWriter fw = null;
try {
fw = new FileWriter("test.csv");
String[] columnNames = {"name", "age"};
for (int i = 0; i < columnNames.length; i++) {
fw.write(columnNames[i]);
if (i < columnNames.length - 1) {
fw.write(",");
}
}
fw.write("\n");
while (result.hasNext()) {
Object[] values = result.next();
for (int i = 0; i < values.length; i++) {
Object value = values[i];
fw.write(value.toString());
if (i < values.length - 1) {
fw.write(",");
}
}
fw.write("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
fw.close();
}
result.close();
client.close();
}
}
}