Skip to content

Instantly share code, notes, and snippets.

@Glamdring
Created November 2, 2019 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glamdring/4a50e230680bd195c8053cf8470ec295 to your computer and use it in GitHub Desktop.
Save Glamdring/4a50e230680bd195c8053cf8470ec295 to your computer and use it in GitHub Desktop.
Convert Cassandra sstabledump JSON to CQL
package com.logsentinel.util.db;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.logsentinel.util.db.dump.Cell;
import com.logsentinel.util.db.dump.Info;
import com.logsentinel.util.db.dump.Row;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class CassandraJSONToCQL {
public static void main(String[] args) throws IOException {
String tableName = args[1];
ObjectMapper mapper = new ObjectMapper();
for (String path : args[0].split(",")) {
List<Row> dumpModel = mapper.readValue(new File(path), new TypeReference<List<Row>>(){});
for (Row row : dumpModel) {
Map<String, String> quotedFields = new LinkedHashMap<>();
Map<String, Integer> numbers = new LinkedHashMap<>();
Map<String, Boolean> booleans = new LinkedHashMap<>();
for (Info info : row.getRows()) {
for (Cell cell : info.getCells()) {
if (cell.getValue() != null && cell.getName() != null) {
try {
numbers.put(cell.getName(), Integer.parseInt(cell.getValue().toString()));
} catch (Exception ex) {
if (cell.getValue().toString().equalsIgnoreCase("true") || cell.getValue().toString().equals("false")) {
booleans.put(cell.getName(), Boolean.parseBoolean(cell.getValue().toString()));
} else {
String value = cell.getValue().toString().replace("\n", "\\n");
if (value.endsWith("Z") && value.length() == 24) {
value = value.replace(" ", "T");
// currently
} else if (cell.getPath() != null && !cell.getPath().isEmpty()) {
if (value.equals("[]")) {
continue;
}
value = "[" + value + "]";
}
quotedFields.put(cell.getName(), value.replace("'", "''"));
}
}
}
}
}
String stringFields = String.join(",", quotedFields.keySet());
if (StringUtils.isNotBlank(stringFields)) {
stringFields = stringFields + ",";
}
String numberFields = String.join(",", numbers.keySet());
if (StringUtils.isNotBlank(numberFields)) {
numberFields = numberFields + ",";
}
String booleanFields = String.join(",", booleans.keySet());
String id = row.getPartition().getKey().get(0);
String stringValues = String.join(",", quotedFields.values().stream().map(CassandraJSONToCQL::quote).collect(Collectors.toList()));
String numberValues = String.join(",", numbers.values().stream().map(String::valueOf).collect(Collectors.toList()));
String booleanValues = String.join(",", booleans.values().stream().map(String::valueOf).collect(Collectors.toList()));
String query = "INSERT INTO " + tableName + "(id, " + stringFields + numberFields + booleanFields + ") VALUES (" + id + "," + stringValues + "," + numberValues + "," + booleanValues + ");";
System.out.println(query);
}
}
}
private static String quote(String v) {
try {
// UUIDs are not quoted
UUID.fromString(v);
return v;
} catch (Exception ex) {
if (v.startsWith("[") && v.endsWith("]")) {
return "[" + Arrays.stream(v.replace("[", "").replace("]", "")
.split(",")).map(CassandraJSONToCQL::quote).collect(Collectors.joining(",")) + "]";
} else {
return "'" + v + "'";
}
}
}
}
package com.logsentinel.util.db.dump;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.joda.time.DateTime;
import java.util.ArrayList;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Cell {
String name;
Object value;
String tstamp;
ArrayList<String> path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public String getTstamp() {
return tstamp;
}
public void setTstamp(String tstamp) {
this.tstamp = tstamp;
}
public ArrayList<String> getPath() {
return path;
}
public void setPath(ArrayList<String> path) {
this.path = path;
}
}
package com.logsentinel.util.db.dump;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Info {
String type;
int position;
LivelinessInfo livenessInfo;
ArrayList<Cell> cells;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public LivelinessInfo getLivenessInfo() {
return livenessInfo;
}
public void setLivenessInfo(LivelinessInfo livenessInfo) {
this.livenessInfo = livenessInfo;
}
public ArrayList<Cell> getCells() {
return cells;
}
public void setCells(ArrayList<Cell> cells) {
this.cells = cells;
}
}
package com.logsentinel.util.db.dump;
public class LivelinessInfo {
String tstamp;
public String getTstamp() {
return tstamp;
}
public void setTstamp(String tstamp) {
this.tstamp = tstamp;
}
}
package com.logsentinel.util.db.dump;
import java.util.ArrayList;
public class Partition {
ArrayList<String> key;
int position;
public ArrayList<String> getKey() {
return key;
}
public void setKey(ArrayList<String> key) {
this.key = key;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
package com.logsentinel.util.db.dump;
import java.util.ArrayList;
public class Row {
Partition partition;
ArrayList<Info> rows;
public Partition getPartition() {
return partition;
}
public void setPartition(Partition partition) {
this.partition = partition;
}
public ArrayList<Info> getRows() {
return rows;
}
public void setRows(ArrayList<Info> rows) {
this.rows = rows;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment