cassandra入门(一):jdbc连接cassandra作增删改查

先分享一个最新的cassandra-java-driver文档,点击电子书分享里的链接,找到javaDriver21.pdf。

该文档内容比较全,包含:jdbc连接cassandra集群,执行cql增删改查,批量查询,异步查询,cql的类型和java类型的映射关系及用户自定义类型使用,ORM等。

Cassandra是一个NoSql数据库,纯java编写,apache的*项目,主页:http://cassandra.apache.org/(简介不多说网上有)。

入门步骤:(我的jdk版本是1.7.0_71,win7系统)

1.去主页下载cassandra,我下载的是apache-cassandra-2.1.9,然后bin/cassandra.bat启动数据库,如果想使用bin/cqlsh.bat则需要安装python2.7。

2.贴代码

1

2

3

4

5

<dependency>

<groupId>com.datastax.cassandra</groupId>

<artifactId>cassandra-driver-core</artifactId>

<version>2.1.5</version>

</dependency>

SimpleClient:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

package com.zoo;

import com.datastax.driver.core.Cluster;

import com.datastax.driver.core.Host;

import com.datastax.driver.core.Metadata;

import com.datastax.driver.core.ResultSet;

import com.datastax.driver.core.Row;

import com.datastax.driver.core.Session;

/**

*

* @author yankai913@gmail.com

* @date 2015-9-25

*

*/

public class SimpleClient {

private Cluster cluster;

private Session session;

public Session getSession() {

return this.session;

}

/**

* 连接集群,创建执行cql的session对象。

*

* @param node

*/

public void connect(String node) {

cluster = Cluster.builder().addContactPoint(node).build();

Metadata metadata = cluster.getMetadata();

System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());

for (Host host : metadata.getAllHosts()) {

System.out.printf("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(),

host.getAddress(), host.getRack());

}

session = cluster.connect();

System.out.println();

}

/**

* 创建schema, 创建库:simplex,表:simplex.songs,表:simplex.playlists

*/

public void createSchema() {

session.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication "

+ "= {'class':'SimpleStrategy', 'replication_factor':3};");

session.execute("CREATE TABLE IF NOT EXISTS simplex.songs (" + "id uuid PRIMARY KEY," + "title text,"

+ "album text," + "artist text," + "tags set<text>," + "data blob" + ");");

session.execute("CREATE TABLE IF NOT EXISTS simplex.playlists (" + "id uuid," + "title text,"

+ "album text, " + "artist text," + "song_id uuid,"

+ "PRIMARY KEY (id, title, album, artist)" + ");");

}

/**

* 插入数据

*/

public void loadData() {

session.execute("INSERT INTO simplex.songs (id, title, album, artist, tags) " + "VALUES ("

+ "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'La Petite Tonkinoise',"

+ "'Bye Bye Blackbird'," + "'Joséphine Baker'," + "{'jazz', '2013'})" + ";");

session.execute("INSERT INTO simplex.playlists (id, song_id, title, album,artist) " + "VALUES ("

+ "2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," + "756716f7-2e54-4715-9f00-91dcbea6cf50,"

+ "'La Petite Tonkinoise'," + "'Bye Bye Blackbird'," + "'Joséphine Baker'" + ");");

}

/**

* 查询simplex.songs

*/

public void querySchema() {

ResultSet results2 =

session.execute("SELECT * FROM simplex.songs "

+ "WHERE id = 756716f7-2e54-4715-9f00-91dcbea6cf50;");

for (Row row : results2) {

System.out.println(row.getUUID("id") + "\t" + row.getString("title") + "\t"

+ row.getString("album") + "\t" + row.getString("artist") + "\t"

+ row.getSet("tags", String.class));

}

System.out.println();

}

/**

* 修改simplex.songs

*/

public void updateSchema() {

session.execute("UPDATE simplex.songs set title = 'La Petite Tonkinoise Updated'"

+ " WHERE id = 756716f7-2e54-4715-9f00-91dcbea6cf50;");

}

/**

* 删除simplex.songs

*/

public void deleteSchema() {

session.execute("DELETE FROM simplex.songs " + " WHERE id = 756716f7-2e54-4715-9f00-91dcbea6cf50;");

}

/**

* 删除keyspace,keyspace可以理解成oracle里的schema

*/

public void dropSchema() {

session.execute("DROP KEYSPACE simplex;");

}

public void close() {

session.close();

cluster.close();

}

public static void main(String[] args) {

SimpleClient client = new SimpleClient();

try {

client.connect("127.0.0.1");

client.dropSchema();

client.createSchema();

client.loadData();

System.out.println("before update...");

client.querySchema();

System.out.println("after update...");

client.updateSchema();

client.querySchema();

System.out.println();

client.deleteSchema();

System.out.println("after delete...");

client.querySchema();

System.out.println();

}

catch (Throwable t) {

t.printStackTrace();

}

finally {

client.close();

}

}

}

打印结果:
Connected to cluster: Test Cluster
Datacenter: datacenter1; Host: /127.0.0.1; Rack: rack1

before update…
756716f7-2e54-4715-9f00-91dcbea6cf50 La Petite Tonkinoise Bye Bye Blackbird Joséphine Baker [2013, jazz]

after update…
756716f7-2e54-4715-9f00-91dcbea6cf50 La Petite Tonkinoise Updated Bye Bye Blackbird Joséphine Baker [2013, jazz]

after delete…

下一篇说cassandra的ORM。


上一篇:cassandra入门(二):自定义类型使用和ORM


下一篇:扩展webx资源装载器之HttpResourceLoader(一):简单实现