Cassandra学习笔记


Cassandra学习笔记
2011年11月16日
          http://cassandra.apache.org/  
  http://java.net/projects/jna/downloads  
  特点:弱一致性(可调一致性),分布式(自动切片)、无中心(自动协调节点操作),高可用性(副本因子决定数据在集群中的多个节点复制,副本放置策略可以决定副本存于本数据中心、或另一数据中心和  本中心的其他机架、或其他数据中心和本中心的其他机架)。
  转变思维:使用丰富有意义的列名、以查询模型为中心建模(而不是一数据模型为中心建模,SQL)、一行之中有很多列对cassandra来说是家常便饭,在cassandra0.7中,每行可以容纳20亿列之多。每行有key、海量的column。
  应用场景:大规模部署(分布式,可单节点部署,但体现不出优势)、写密集。
  数据模型:cluster、keyspace、column family、row、column/super column。
  Cassandra分布式使用gossip协议通讯,使用Phi增量故障检测算法对节点进行检测。增量故障检测机制不是简单的判定节点离线/在线,而是给出离线“嫌疑”。通过指定 “嫌疑”度,调整故障检测的灵敏度。 
  Cassandra使用“墓碑”软删除。
  [ClusterName]
  [Keyspace][ColumnFamily][RowKey][SuperColumn][Column]
  列族选项:
  column_type Standard | Super
  clock_type Timestamp
  comparator AsciiType、BytesType、LexicalUUIDType、IntegerType、LongType、TimeUUIDType、UTF8Type
  subcomparator
  reconciler Timestamp
  comment 备注
  rows_cached 行缓存数量
  preload_row_cache 自动加载行缓存
  key_cache_size 缓存的键值数量
  read_repair_chance 0.0~1.0
  分区器,指定行键值排序和影响数据在节点的分布:
  随机分区器
  有序分区器
  配页有序分区器
  字节序风分区器
  分布式的一致性:
  顺序一致性(严格一致性)
  因果一致性
  最终一致性(弱一致性) 
  数据库大观:
  关系数据库
  对象数据库 Cache(InterSystems)
  XML数据库 Tamino(SoftwareAG)、Berkeley XML DB(Oracle)
  文档数据库 Lotus(IBM)、CouchDB(Apache)、MongoDB、Riak
  图数据库 FlockDB(Twitter)
  键值存储与分布式哈希表 Dynamo(Amazon) 、SimpleDB、Dynomite(Microsoft)、Redis(VMWare)、MemcacheD、MemcacheDB
  列数据库 Bigtable(Google)、HBase(Aapche)、Hypertable、Cassandra(Apache)
  CAP理论
  
  
  
  JAN.tar放入./lib目录
  启动
  ./bin/cassandra -f 
  客户端
  ./bin/cassandra-cli
  ] help
  ] connect /
  port:9160
  ] show cluster name
  默认:Test
  ] show keyspaces
  默认:system
  ] show api version
  ] create keyspace mykeyspace with replication_factor=1;
  ] describe mykeyspace;
  ] drop keyspace mykeyspace;
  ] use mykeyspace
  ] create column family user
  ] drop column family user;
  ] rename keyspace mykeyspace newkeyspace
  ] rename column family user newuser;
  ] set user['user1']['fname']='user1'
  ] count user['user1']
  ] get user['user1']
  ] del user['user1']['fname']
  ] del user['user1']
  按键取全部列
  按键取指定列
  按键取指定分片区间列(以列族排序为准:AsciiType、BytesType、LexicalUUIDType、IntegerType、LongType、TimeUUIDType、UTF8Type、)
  配置文件
  cassandra.yaml
  存储位置
  
  
  运行日志
  log4j.appender.R.File=/var/log/cassandra/system.log
  缓存数据文件
   
  /var/lib/cassandra/saved_caches 
    
  集群名
  Test Cluster
  种子节点
  seed:
  - 192.168.1.5
  - 192.168.1.7
  autobootstrap 节点自己作为种子节点是,不能指定true 
  listen_address:
  rpc_address:
  安全
  1、指定验证类(作为plugin需额外提供)
  authenticator: org.apache.cassandra.auth.AllowAllAuthenticator
  authority:  org.apache.cassandra.auth.AllowAllAuthority 
  #无验证
  org.apache.cassandra.auth.SimpleAuthenticator #鉴权 passwd.properties
  org.apache.cassandra.auth.SimpleAuthority #授权 access.properties
  #简单验证
  2、告知JVM文件路径
  JVM_OPTS="-Dpasswd.properties=/home/cassandra/conf/passwd.properties -access.properties= /home/cassandra/conf/access.properties"
  3、定义文件内容
  passwd.properties
  mykeyspace=user1,user2,user3
  access.properties
  user1=abc
  user2=123
  user3=xyz
  并发
  concurrent_reads 视CPU核心数定
  concurrent_writes 视并发客户端数定
  启动配置
  cassandra.in.sh(conf/cassandra-env.sh)( cassandra.in.bat)
  JVM_OPTS -Xms2048M -Xmx2048M
  日志配置
  ./conf/log4j-server.properties
  工具
  ./bin/sstablekeys sstable_file_path
  查看SSTable中的键值
  web控制台
  http://github.com/suguru/cassandra-webconsole
  管理
  ./bin/nodetool
  avro
  thrift
  需要http://thrift.apache.org
  thrift属于facebook.com技术核心框架之一,使用不同开发语言开发的系统可以通过该框架实现彼此间的通讯,开发者只需编辑一份thrift脚本,即可自动获得其它开发语言的代码(比如 c++ java python ruby c# haskell ocmal erlang cocoa php squeak).  
  需要http://www.boost.org
  编译thrift
  可能需要php5.2+
  ./bootstrap.sh 可能不需要  
  ./configure
  ./make
  ./make install
  cd ./lib/
  ./make
  which thrift
  压力测试 
  生成压力脚本
  cd /contrib/py_stress
  ant gen-thrift-py
  stress.py -h 
  Cygwin一个windows中的Bash Shell模拟器,允许执行各种linux工具。 
  编程接口 
  Cassandra提供了多种语言接口,如Java、C++、C#、Python等。正是由于使用了Thrift框架,Cassandra的开发者只需要定义客户端与服务器端之间的相关通信的结构体和接口,并提供一个服务契约文件cassandra.thrift,即可提供多种编程语言接口。在我们下载的Cassandra发行包中,只包含了Java的编程接口。假设我们已经安装好了Thrift,那么通过执行简单的命令就可以获得C#或C++的编程接口。生成C#编程接口如下:thrift -gen csharp cassandra.thrift 生成C++编程接口如下:thrift -gen cpp cassandra.thrift 在生成编程接口的代码中,thrift是我们安装的可执行文件,-gen命令后面带的参数是需要生成代码的种类,csharp代表C#,cpp代表C++,最后一个参数cassandra.thrift代表的是服务的契约文件,这个文件在Cassandra的发行包中interface目录下。只需查看cassandra.thrift即可了解编程接口定义。  
  数据类型 
  Column   
  SuperColumn  
  ColumnOrSuperColumn 
  ColumnParent 
  ColumnPath 
  SliceRange 
  SlicePredicate 
  Deletion 
  Mutation 
  KeyRange 
  KeySlice 
  TokenRange 
  AuthenticationRequest 
  ConsistencyLevel 
  NotFoundException 
  InvalidRequestException 
  UnavailableException 
  TimedOutException 

猜你喜欢

转载自weot06weot.iteye.com/blog/1359283
今日推荐