Skip to content

Instantly share code, notes, and snippets.

@adutra
Created September 8, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adutra/bfa102cddcda51e10f1f75903d420e7d to your computer and use it in GitHub Desktop.
Save adutra/bfa102cddcda51e10f1f75903d420e7d to your computer and use it in GitHub Desktop.
import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import java.util.UUID;
@Table(name = "article_updates")
public class ArticleUpdate {
@PartitionKey
@Column(name = "change_request_id")
private UUID changeRequestId;
private String name;
public ArticleUpdate() {
}
public ArticleUpdate(UUID changeRequestId, String name) {
this.changeRequestId = changeRequestId;
this.name = name;
}
public UUID getChangeRequestId() {
return changeRequestId;
}
public ArticleUpdate setChangeRequestId(UUID changeRequestId) {
this.changeRequestId = changeRequestId;
return this;
}
public String getName() {
return name;
}
public ArticleUpdate setName(String name) {
this.name = name;
return this;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ArticleUpdate{");
sb.append("changeRequestId=").append(changeRequestId);
sb.append(", name='").append(name).append('\'');
sb.append('}');
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArticleUpdate that = (ArticleUpdate) o;
if (!changeRequestId.equals(that.changeRequestId)) return false;
return name != null ? name.equals(that.name) : that.name == null;
}
@Override
public int hashCode() {
int result = changeRequestId.hashCode();
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.utils.UUIDs;
public class Test {
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder()
.addContactPoints("127.0.0.1").withPort(9042)
.build();
Session session = cluster.connect();
session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};");
session.execute("USE test");
session.execute("CREATE TABLE IF NOT EXISTS article_updates (change_request_id uuid PRIMARY KEY, name text)");
MappingManager manager = new MappingManager(session);
Mapper<ArticleUpdate> mapper = manager.mapper(ArticleUpdate.class);
ArticleUpdate foo = new ArticleUpdate(UUIDs.random(), "foo");
mapper.save(foo);
ArticleUpdate foo2 = mapper.get(foo.getChangeRequestId());
System.out.println(foo2);
} finally {
if (cluster != null)
cluster.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment