Two days ago, I began to study Kassandra in my internship, they gave me the opportunity to learn about Kassandra, and I found several codes from the network. There are no errors in the code, but when I run the code, I get errors such as:
InvalidRequestException (why: the Blog keyspace does not exist in this schema.) In org.apache.cassandra.thrift.Cassandra $ remove_result.read (Cassandra.java:14354) in org.apache.cassandra.thrift.Cassandra $ Client.recv_remove ( Cassandra.java:755) in org.apache.cassandra.thrift.Cassandra $ Client.remove (Cassandra.java:729) in Authors.removeAuthor (Authors.java:141) on Authors.main (Authors.java:59)
I also run cassandra from the console using the command. / cassandra -f. I think I need to create a cassandra database first, but I really could not find how to do this using java. Please help me on this topic. Thank you very much.
if it will be useful to use the code I'm trying here.
import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.cassandra.thrift.Cassandra; import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; import org.apache.cassandra.thrift.ConsistencyLevel; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.InvalidRequestException; import org.apache.cassandra.thrift.KeyRange; import org.apache.cassandra.thrift.KeySlice; import org.apache.cassandra.thrift.Mutation; import org.apache.cassandra.thrift.NotFoundException; import org.apache.cassandra.thrift.SlicePredicate; import org.apache.cassandra.thrift.SliceRange; import org.apache.cassandra.thrift.TimedOutException; import org.apache.cassandra.thrift.UnavailableException; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; public class Authors { private static final String KEYSPACE = "Blog"; private static final String COLUMN_FAMILY = "Authors"; public static final String ENCODING = "utf-8"; private static TTransport tr = null; public static void main(String[] args) throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException { Cassandra.Client client = setupConnection(); System.out.println("Remove all the authors we might have created before.\n"); removeAuthor(client, "Eric Long"); removeAuthor(client, "Ronald Mathies"); removeAuthor(client, "John Steward"); System.out.println("Create the authors.\n"); createAuthor(client, "Eric Long", "eric (at) long.com", "United Kingdom", "01/01/2002"); createAuthor(client, "Ronald Mathies", "ronald (at) sodeso.nl", "Netherlands, The", "01/01/2010"); createAuthor(client, "John Steward", "john.steward (at) somedomain.com", "Australia", "01/01/2009"); System.out.println("Select Eric Long.\n"); selectSingleAuthorWithAllColumns(client, "Eric Long"); System.out.println("Select Ronald Mathies.\n"); selectSingleAuthorWithAllColumns(client, "Ronald Mathies"); System.out.println("Select John Steward.\n"); selectSingleAuthorWithAllColumns(client, "John Steward"); System.out.println("Select all authors with all columns.\n"); selectAllAuthorsWithAllColumns(client); System.out.println("Select all authors with only the email column.\n"); selectAllAuthorsWithOnlyTheEmailColumn(client); System.out.println("Update John Steward.\n"); updateJohnStewardAuthor(client); System.out.println("Select John Steward.\n"); selectSingleAuthorWithAllColumns(client, "John Steward"); System.out.println("Remove email address and birthday from John Steward.\n"); deleteEmailAndBirthdayFromJohnSteward(client); System.out.println("Select John Steward.\n"); selectSingleAuthorWithAllColumns(client, "John Steward"); closeConnection(); } private static Cassandra.Client setupConnection() throws TTransportException { try { tr = new TSocket("localhost", 9160); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); return client; } catch (TTransportException exception) { exception.printStackTrace(); } return null; } private static void closeConnection() { try { tr.flush(); tr.close(); } catch (TTransportException exception) { exception.printStackTrace(); } } private static void removeAuthor(Cassandra.Client client, String authorKey) { try { ColumnPath columnPath = new ColumnPath(COLUMN_FAMILY); client.remove(KEYSPACE, authorKey, columnPath, System.currentTimeMillis(), ConsistencyLevel.ALL); } catch (Exception exception) { exception.printStackTrace(); } } private static void createAuthor(Cassandra.Client client, String authorKey, String email, String country, String registeredSince) { try { long timestamp = System.currentTimeMillis(); Map<String, List<ColumnOrSuperColumn>> job = new HashMap<String, List<ColumnOrSuperColumn>>(); List<ColumnOrSuperColumn> columns = new ArrayList<ColumnOrSuperColumn>(); Column column = new Column("email".getBytes(ENCODING), email.getBytes(ENCODING), timestamp); ColumnOrSuperColumn columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); columns.add(columnOrSuperColumn); column = new Column("country".getBytes(ENCODING), country.getBytes(ENCODING), timestamp); columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); columns.add(columnOrSuperColumn); column = new Column("country".getBytes(ENCODING), country.getBytes(ENCODING), timestamp); columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); columns.add(columnOrSuperColumn); column = new Column("registeredSince".getBytes(ENCODING), registeredSince.getBytes(ENCODING), timestamp); columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); columns.add(columnOrSuperColumn); job.put(COLUMN_FAMILY, columns); client.batch_insert(KEYSPACE, authorKey, job, ConsistencyLevel.ALL); } catch (Exception exception) { exception.printStackTrace(); } } private static void selectSingleAuthorWithAllColumns(Cassandra.Client client, String authorKey) { try { SlicePredicate slicePredicate = new SlicePredicate(); SliceRange sliceRange = new SliceRange(); sliceRange.setStart(new byte[] {}); sliceRange.setFinish(new byte[] {}); slicePredicate.setSlice_range(sliceRange); ColumnParent columnParent = new ColumnParent(COLUMN_FAMILY); List<ColumnOrSuperColumn> result = client.get_slice(KEYSPACE, authorKey, columnParent, slicePredicate, ConsistencyLevel.ONE); printToConsole(authorKey, result); } catch (Exception exception) { exception.printStackTrace(); } } private static void selectAllAuthorsWithAllColumns(Cassandra.Client client) { try { KeyRange keyRange = new KeyRange(3); keyRange.setStart_key(""); keyRange.setEnd_key(""); SliceRange sliceRange = new SliceRange(); sliceRange.setStart(new byte[] {}); sliceRange.setFinish(new byte[] {}); SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.setSlice_range(sliceRange); ColumnParent columnParent = new ColumnParent(COLUMN_FAMILY); List<KeySlice> keySlices = client.get_range_slices(KEYSPACE, columnParent, slicePredicate, keyRange, ConsistencyLevel.ONE); for (KeySlice keySlice : keySlices) { printToConsole(keySlice.getKey(), keySlice.getColumns()); } } catch (Exception exception) { exception.printStackTrace(); } } private static void selectAllAuthorsWithOnlyTheEmailColumn(Cassandra.Client client) { try { KeyRange keyRange = new KeyRange(3); keyRange.setStart_key(""); keyRange.setEnd_key(""); List<byte[]> columns = new ArrayList<byte[]>(); columns.add("email".getBytes(ENCODING)); SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.setColumn_names(columns); ColumnParent columnParent = new ColumnParent(COLUMN_FAMILY); List<KeySlice> keySlices = client.get_range_slices(KEYSPACE, columnParent, slicePredicate, keyRange, ConsistencyLevel.ONE); for (KeySlice keySlice : keySlices) { printToConsole(keySlice.getKey(), keySlice.getColumns()); } } catch (Exception exception) { exception.printStackTrace(); } } private static void updateJohnStewardAuthor(Cassandra.Client client) { try { long timestamp = System.currentTimeMillis(); Map<String, Map<String, List<Mutation>>> job = new HashMap<String, Map<String, List<Mutation>>>(); List<Mutation> mutations = new ArrayList<Mutation>();