public List<String> getSchema( String sourceKeyspace, String destKeyspace, Collection<String> tables) { Cluster cluster = peer.getCluster(); Metadata meta = cluster.getMetadata(); KeyspaceMetadata kmeta = meta.getKeyspace(keyspace); Collection<TableMetadata> tmeta = kmeta.getTables(); // List<String> drops = new ArrayList<>(); List<String> created = new ArrayList<>(); for (TableMetadata tab : tmeta) { if (tables.size() != 0) { String name = tab.getName(); if (!tables.contains(name)) continue; } String s = tab.asCQLQuery(); StringBuilder sb = new StringBuilder(); sb.append("DROP TABLE IF EXISTS ") .append(destKeyspace) .append(".") .append(tab.getName()) .append(";\n"); created.add(sb.toString()); created.add(s.replaceAll("CREATE TABLE " + sourceKeyspace, "CREATE TABLE " + destKeyspace)); } return created; }
public List<String> getTables() throws ExecutionException { String query = "select columnfamily_name from system.schema_columnfamilies where keyspace_name = ?"; List<String> tables = new ArrayList<String>(); BoundStatement bs = peer.bPrepare(session, query); bs.setString(0, keyspace); ResultSet rs = session.execute(bs); for (Row row : rs) { String s = row.getString(0); tables.add(s); } return tables; }
public void close() { peer.destroy(); }