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

2015-07-24 06:32:45 · 作者: · 浏览: 7
区。

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”的脚本,其内容如下:

exec /root/zhouzx/hbase-1.0.1/bin/hbase shell <

create 'mytable', 'cf'

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

get 'mytable', 'first'

scan 'mytable'

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

disable 'mytable'

drop 'mytable'

EOF

?

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

?

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

HBase Shell; enter 'help ' for list of supportedcommands.

Type "exit " to leave the HBase Shell

Version 1.0.1, r66a93c09df3b12ff7b86c39bc8475c60e15af82d, Fri Apr17 22:14:06 PDT 2015

?

create 'mytable', 'cf'

0 row(s) in 0.6660 seconds

?

Hbase::Table - mytable

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

0 row(s) in 0.1140 seconds

?

get 'mytable', 'first'

COLUMN CELL

cf:info timestamp=1435807200326, value=hello hbase

1 row(s) in 0.0440 seconds

?

scan 'mytable'

ROW COLUMN+CELL

first column=cf:info,timestamp=1435807200326, value=hello hbase

1 row(s) in 0.0210 seconds

?

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

0 row(s) in 0.0040 seconds

?

disable 'mytable'

0 row(s) in 1.1930 seconds

?

drop 'mytable'

0 row(s) in 0.1940 seconds

?

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

?

七、总结

本文对NoSQL进行了全面的介绍,并比较了它与SQL语法之间的不同。尽管大多数NoSQL数据存储系统都已被部署到实际应用中,但仍存在以下挑战性问题需要解决:

第一,已有key-value数据库产品大多是面向特定应用自治构建的,缺乏通用性。

第二,已有产品支持的功能有限(不支持事务特性),导致其应用具有一定的局限性。

第三,已有一些研究成果和改进的NoSQL数据存储系统,但它们都是针对不同应用需求而提出的相应解决方案,很少从全局考虑系统的通用性,也没有形成系列化的研究成果。

第四,缺乏类似关系数据库所具有的强有力的理论(如armstrong公理系统)、技术(如成熟的基于启发式的优化策略、两段封锁协议等)、标准规范(如SQL语言)的支持。

第五,很多NoSQL数据库没有提供内建的安全机制。

?

随着云计算、移动互联网等技术的发展,大数据广泛存在,同时也出现了许多云环境下的新型应用,如社交网络、移动服务、协作编辑等。这些新型应用对海量数据管理或称云数据管理系统也提出了新的需求,NoSQL数据库在这些方面有大展身手的机会。我们有理由相信,NoSQL数据库的明天会更加的美好!