欢迎投稿

今日深度:

NoSQL数据库概览及其与SQL语法的比较(1)(2)

六、NoSQL和SQL语法的简单比较

前面介绍了NoSQL的基本情况,下面以HBase和ORACLE为例,对NoSQL和SQL的语法进行简单的比较。HBase数据库被认为是安全特性最完善的NoSQL数据库产品之一,它被证实是一个强大的工具,尤其是在已经使用Hadoop的场合。如今,它已经是Apache顶级项目,有着众多的开发人员和兴旺的用户社区。

1.创建表

如果要创建一个表“mytable”,其中包含了一个“info”字段,那么:

(1)ORACLE中的语法为:

create table mytable

(

info varchar(30) not null

);

(2)HBase中的语法为

create 'mytable', 'cf'

该命令创建了一个有一个列族(“cf”)的表“mytable”。

2.写数据

如果要向表中写入数据“hello hbase”,那么:

(1)ORACLE中的语法为:

insert into mytable(info) values('hello hbase');

(2)HBase中的语法为:

put 'mytable', 'first', 'cf:info', 'hello hbase'

该命令在“mytable”表的“first”行中的“cf:info”列对应的数据单元中插入“hello hbase”。

3.读(查)数据

如果要从表中读出单条数据,那么:

(1)ORACLE中的语法为:

select * from mytable where info = 'hello hbase';

(2)HBase中的语法为:

get 'mytable', 'first'

该命令输出了该行的数据单元。

如果要从表中读出所有数据,那么:

(1)ORACLE中的语法为:

select * from mytable;

(2)HBase中的语法为:

scan 'mytable'

该命令输出了所有数据。

4.删数据

如果要从表中删除数据,那么:

(1)ORACLE中的语法为:

delete from mytable where info = 'hello hbase';

(2)HBase中的语法为:

put 'mytable', 'first', 'cf:info', 'hello hbase1'

该命令用最新的值覆盖了旧的值,就相当于将原数据删除了。

5.修改数据

如果要在表中修改数据,那么:

(1)ORACLE中的语法为:

update mytable set info = 'hello hbase1' where info = 'hellohbase';

(2)HBase中的语法为:

put 'mytable', 'first', 'cf:info', 'hello hbase1'

该命令用最新的值覆盖了旧的值,就相当于修改了原数据。

6.删表

如果要删除表,那么:

(1)ORACLE中的语法为:

drop table mytable;

(2)HBase中的语法为:

disable 'mytable'

drop 'mytable'

该命令先将表“disable”掉,然后再“drop”掉。

我们可以看到,HBase的语法比较的简单,因此完全可以将上述所有命令放到一个shell脚本中,让命令批量执行。下面,我们来具体操作一下:

第一步,编写名为“command.sh”的脚本,其内容如下:

  1. exec /root/zhouzx/hbase-1.0.1/bin/hbase shell <<EOF 
  2.  
  3. create 'mytable''cf' 
  4.  
  5. put 'mytable''first''cf:info''hello hbase' 
  6.  
  7. get 'mytable''first' 
  8.  
  9. scan 'mytable' 
  10.  
  11. put 'mytable''first''cf:info''hello hbase1' 
  12.  
  13. disable 'mytable' 
  14.  
  15. drop 'mytable' 
  16.  
  17. EOF 

第二步,将该脚本上传到Linux机器的安装HBase的用户下,依次执行“dos2unix command.sh”和“chmod 777command.sh”命令来转换文件格式和对文件赋权限。

第三步,执行“./command.sh”命令,在Linux界面上,我们可以看到如下输出信息:

  1. HBase Shell; enter 'help<RETURN>' for list of supportedcommands. 
  2.  
  3. Type "exit<RETURN>" to leave the HBase Shell 
  4.  
  5. Version 1.0.1, r66a93c09df3b12ff7b86c39bc8475c60e15af82d, Fri Apr17 22:14:06 PDT 2015 
  6.  
  7.   
  8.  
  9. create 'mytable''cf' 
  10.  
  11. 0 row(s) in 0.6660 seconds 
  12.  
  13.   
  14.  
  15. Hbase::Table - mytable 
  16.  
  17. put 'mytable''first''cf:info''hello hbase' 
  18.  
  19. 0 row(s) in 0.1140 seconds 
  20.  
  21.   
  22.  
  23. get 'mytable''first' 
  24.  
  25. COLUMN                           CELL                                                                                           
  26.  
  27.  cf:info                         timestamp=1435807200326, value=hello hbase                                                     
  28.  
  29. 1 row(s) in 0.0440 seconds 
  30.  
  31.   
  32.  
  33. scan 'mytable' 
  34.  
  35. ROW                  COLUMN+CELL                                                                                     
  36.  
  37.  first                   column=cf:info,timestamp=1435807200326, value=hello hbase                                     
  38.  
  39. 1 row(s) in 0.0210 seconds 
  40.  
  41.   
  42.  
  43. put 'mytable''first''cf:info''hello hbase1' 
  44.  
  45. 0 row(s) in 0.0040 seconds 
  46.  
  47.   
  48.  
  49. disable 'mytable' 
  50.  
  51. 0 row(s) in 1.1930 seconds 
  52.  
  53.   
  54.  
  55. drop 'mytable' 
  56.  
  57. 0 row(s) in 0.1940 seconds 

整个脚本执行过程不过几秒钟,但我们之前提到的所有HBase命令都包括其中了,由此可见批处理的威力。大家一定要好好体会一下。




www.htsjk.Com true http://www.htsjk.com/shujukujc/19392.html NewsArticle 六、NoSQL和SQL语法的简单比较 前面介绍了NoSQL的基本情况,下面以HBase和ORACLE为例,对NoSQL和SQL的语法进行简单的比较。HBase数据库被认为是安全特性最完善...
评论暂时关闭