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。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合GeoMesa Cassandra是为了利用GeoMesa库的强大地理空间处理能力,与Cassandra NoSQL数据库结合,以便在Spring应用中方便地执行地理数据的CRUD操。GeoMesa是一个高度可扩展的开源框架,它抽象了底层的数据存储,使得开发者能够更专注于业务逻辑。 以下是整合步骤: 1. 添加依赖: 在你的`pom.xml`文件中添加GeoMesa和Spring Data Cassandra的依赖: ```xml <dependency> <groupId>org.locationtech.geomesa</groupId> <artifactId>geomesa-spring-boot-starter-cassandra</artifactId> <version>XX.YY.ZZ</version> <!-- 用最新版本替换 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency> ``` 记得替换版本号为最新的稳定版。 2. 配置GeoMesa Cassandra:在`application.properties`或`application.yml`中配置Cassandra连接信息,包括主机、端口、用户名、密码等。 3. 创建数据源:在Spring Boot应用中,使用Spring Data Cassandra的Repository接口来定义地理空间数据源。例如,假设你有一个`Feature`实体映射到GeoMesa中的一个表,你可以创建一个类似这样的Repository接口: ```java public interface FeatureRepository extends GeoMesaFeatureDao<Feature, UUID> { // CRUD operations List<Feature> findByBbox(BoundingBox bbox); Feature findById(UUID id); void save(Feature feature); void deleteById(UUID id); } ``` 4. 使用Repository:注入`FeatureRepository`并进行CRUD操: ```java @Autowired private FeatureRepository featureRepository; public void createFeature(Feature feature) { featureRepository.save(feature); } public Feature getFeatureById(UUID id) { return featureRepository.findById(id); } public void updateFeature(Feature updatedFeature) { featureRepository.save(updatedFeature); } public void deleteFeature(UUID id) { featureRepository.deleteById(id); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值