﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-hengheng123456789-随笔分类-JAVA-DB</title><link>http://www.blogjava.net/hengheng123456789/category/17306.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 24 Dec 2010 17:49:29 GMT</lastBuildDate><pubDate>Fri, 24 Dec 2010 17:49:29 GMT</pubDate><ttl>60</ttl><item><title>HBASE松散数据存储设计初识（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341449.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 24 Dec 2010 03:46:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341449.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/341449.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341449.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/341449.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/341449.html</trackback:ping><description><![CDATA[转自：http://hi.baidu.com/webcell/blog/item/f179ac0f0ab6f3e7aa645749.html
<div>
<p style="line-height: normal; text-indent: 21pt; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">最近关注</span>Hadoop<span style="line-height: normal; ">，因此也顺便关注了一下</span>Hadoop<span style="line-height: normal; ">相关的项目。</span>HBASE<span style="line-height: normal; ">就是基于</span>Hadoop<span style="line-height: normal; ">的一个开源项目，也是对</span>Google<span style="line-height: normal; ">的</span>BigTable<span style="line-height: normal; ">的一种实现。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BigTable<span style="line-height: normal; ">是什么？</span>Google<span style="line-height: normal; ">的</span>Paper<span style="line-height: normal; ">对其作了充分的说明。字面上看就是一张大表，其实和我们想象的传统数据库的表还是有些差别的。松散数据可以说是介于</span>Map Entry<span style="line-height: normal; ">（</span>key &amp; value<span style="line-height: normal; ">）和</span>DB Row<span style="line-height: normal; ">之间的一种数据。在我使用</span>Memcache<span style="line-height: normal; ">的时候，有时候的需求是需要存储的不仅仅是简单的一个</span>key<span style="line-height: normal; ">对应一个</span>value<span style="line-height: normal; ">，可能我需要类似于数据库表结构中多属性的存储，但是又不会有传统数据库表结构中那么多关联关系的需求，其实这类数据就是所谓的松散数据。</span>BigTable<span style="line-height: normal; ">最浅显来看就是一张很大的表，表的属性可以根据需求去动态增加，但是又没有表与表之间关联查询的需求。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: normal; ">互联网应用有一个最大的特点，就是速度，功能再强大，速度慢，还是会被舍弃。因此在大访问量的网站都采取前后的缓存来提升性能和响应时间。对于</span>Map Entry<span style="line-height: normal; ">类型的数据，集中式分布式</span>Cache<span style="line-height: normal; ">都有很多选择，对于传统的关系型数据，从</span>MySQL<span style="line-height: normal; ">到</span>Oracle<span style="line-height: normal; ">都给了很好的支持，唯有松散数据这类数据，采用前后两种解决方案都不能最大化它的处理能力。因此</span>BigTable<span style="line-height: normal; ">才有了它用武之地。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HBASE<span style="line-height: normal; ">作为</span>Apache<span style="line-height: normal; ">的开源项目，也是出于起步阶段，因为其实它所依赖的</span>Hadoop<span style="line-height: normal; ">也不能说已经到了成熟阶段，所以都有很大的发展空间，这也为我们这些开源爱好者提供了更多空间去贡献。这里主要会谈到</span>HBASE<span style="line-height: normal; ">的框架设计方面的知识和它的一些特点，不论是否采用</span>HBASE<span style="line-height: normal; ">去解决工作中的问题，一种好的流程设计总会给开发者和架构设计者带来一些思想上的火花。</span></p>
<h2 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: 32px; font-size: 14pt; ">HBASE</span><span style="line-height: 32px; font-size: 14pt; ">设计介绍</span></h2>
<h3 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: 27px; font-size: 12pt; ">数据模型</span></h3>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HBASE<span style="line-height: normal; ">中的每一张表，就是所谓的</span>BigTable<span style="line-height: normal; ">。</span>BigTable<span style="line-height: normal; ">会存储一系列的行记录，行记录有三个基本类型的定义：</span>Row Key,Time Stamp,Column<span style="line-height: normal; ">。</span>Row Key<span style="line-height: normal; ">是行在</span>BigTable<span style="line-height: normal; ">中的唯一标识，</span>Time Stamp<span style="line-height: normal; ">是每次数据操作对应关联的时间戳，可以看作类似于</span>SVN<span style="line-height: normal; ">的版本，</span>Column<span style="line-height: normal; ">定义为：</span>&lt;family&gt;:&lt;label&gt;<span style="line-height: normal; ">，通过这两部分可以唯一的指定一个数据的存储列，</span>family<span style="line-height: normal; ">的定义和修改需要对</span>HBASE<span style="line-height: normal; ">作类似于</span>DB<span style="line-height: normal; ">的</span>DDL<span style="line-height: normal; ">操作，而对于</span>label<span style="line-height: normal; ">的使用，则不需要定义直接可以使用，这也为动态定制列提供了一种手段。</span>family<span style="line-height: normal; ">另一个作用其实在于物理存储优化读写操作，同</span>family<span style="line-height: normal; ">的数据物理上保存的会比较临近，因此在业务设计的过程中可以利用这个特性。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">看一下逻辑数据模型：</span></p>
<table class="FCK__ShowTableBorders" cellspacing="0" cellpadding="0" border="0" style="table-layout: auto; line-height: normal; border-collapse: collapse; color: #555b6e; font-family: Arial; font-size: 12px; ">
    <tbody style="line-height: normal; ">
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Row Key</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Time Stamp</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Column</span></strong><span style="line-height: normal; font-size: 9pt; ">&nbsp;<em style="line-height: normal; ">"contents:"</em></span></p>
            </td>
            <td colspan="2" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Column</span></strong><span style="line-height: normal; font-size: 9pt; ">&nbsp;<em style="line-height: normal; ">"anchor:"</em></span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Column</span></strong><span style="line-height: normal; font-size: 9pt; ">&nbsp;<em style="line-height: normal; ">"mime:"</em></span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td valign="top" rowspan="5" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"com.cnn.www"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t9</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="right" style="line-height: normal; text-align: right; "><span style="line-height: normal; font-size: 9pt; ">"anchor:cnnsi.com"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"CNN"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t8</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="right" style="line-height: normal; text-align: right; "><span style="line-height: normal; font-size: 9pt; ">"anchor:my.look.ca"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"CNN.com"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t6</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"&lt;html&gt;..."</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"text/html"</span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t5</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"&lt;html&gt;..."</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t3</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"&lt;html&gt;..."</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; "></td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 0.75pt; padding-left: 0.75pt; padding-bottom: 0.75pt; padding-top: 0.75pt; "></td>
        </tr>
    </tbody>
</table>
<p style="line-height: normal; text-indent: 21pt; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">上表中有一列，列的唯一标识为</span>com.cnn.www<span style="line-height: normal; ">，每一次逻辑修改都有一个</span>timestamp<span style="line-height: normal; ">关联对应，一共有四个列定义：</span>&lt;contents:&gt;,&lt;anchor:cnnsi.com&gt;,&lt;anchor:my.look.ca&gt;,&lt;mime:&gt;<span style="line-height: normal; ">。如果用传统的概念来将</span>BigTable<span style="line-height: normal; ">作解释，那么</span>BigTable<span style="line-height: normal; ">可以看作一个</span>DB Schema<span style="line-height: normal; ">，每一个</span>Row<span style="line-height: normal; ">就是一个表，</span>Row key<span style="line-height: normal; ">就是表名，这个表根据列的不同可以划分为多个版本，同时每个版本的操作都会有时间戳关联到操作的行。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">再看一下</span>HBASE<span style="line-height: normal; ">的物理数据模型：</span></p>
<table class="FCK__ShowTableBorders" cellspacing="0" cellpadding="0" border="0" style="table-layout: auto; line-height: normal; border-collapse: collapse; color: #555b6e; font-family: Arial; font-size: 12px; ">
    <tbody style="line-height: normal; ">
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Row Key</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Time Stamp</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Column</span></strong><span style="line-height: normal; font-size: 9pt; ">&nbsp;<em style="line-height: normal; ">"contents:"</em></span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td valign="top" rowspan="3" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"com.cnn.www"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t6</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"&lt;html&gt;..."</span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t5</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"&lt;html&gt;..."</span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t3</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"&lt;html&gt;..."</span></p>
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" cellspacing="0" cellpadding="0" border="0" style="table-layout: auto; line-height: normal; border-collapse: collapse; color: #555b6e; font-family: Arial; font-size: 12px; ">
    <tbody style="line-height: normal; ">
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Row Key</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Time Stamp</span></strong></p>
            </td>
            <td colspan="2" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Column</span></strong><span style="line-height: normal; font-size: 9pt; ">&nbsp;<em style="line-height: normal; ">"anchor:"</em></span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td valign="top" rowspan="2" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"com.cnn.www"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t9</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="right" style="line-height: normal; text-align: right; "><span style="line-height: normal; font-size: 9pt; ">"anchor:cnnsi.com"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"CNN"</span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t8</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="right" style="line-height: normal; text-align: right; "><span style="line-height: normal; font-size: 9pt; ">"anchor:my.look.ca"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"CNN.com"</span></p>
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" cellspacing="0" cellpadding="0" border="0" style="table-layout: auto; line-height: normal; border-collapse: collapse; color: #555b6e; font-family: Arial; font-size: 12px; ">
    <tbody style="line-height: normal; ">
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Row Key</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Time Stamp</span></strong></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><strong style="line-height: normal; "><span style="line-height: normal; font-size: 9pt; ">Column</span></strong><span style="line-height: normal; font-size: 9pt; ">&nbsp;<em style="line-height: normal; ">"mime:"</em></span></p>
            </td>
        </tr>
        <tr style="line-height: normal; ">
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="left" style="line-height: normal; text-align: left; "><span style="line-height: normal; font-size: 9pt; ">"com.cnn.www"</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">t6</span></p>
            </td>
            <td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; padding-right: 3pt; padding-left: 3pt; padding-bottom: 3pt; padding-top: 3pt; ">
            <p align="center" style="line-height: normal; text-align: center; "><span style="line-height: normal; font-size: 9pt; ">"text/html"</span></p>
            </td>
        </tr>
    </tbody>
</table>
<p style="line-height: normal; text-indent: 21pt; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">物理数据模型其实就是将逻辑模型中的一个</span>Row<span style="line-height: normal; ">分割成为根据</span>Column family<span style="line-height: normal; ">存储的物理模型。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">对于</span>BigTable<span style="line-height: normal; ">的数据模型操作的时候，会锁定</span>Row<span style="line-height: normal; ">，并保证</span>Row<span style="line-height: normal; ">的原子操作。</span></p>
<h3 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: 27px; font-size: 12pt; ">框架结构及流程</span></h3>
<p align="center" style="line-height: normal; text-align: center; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 9pt; "><img height="587" src="http://www.blogjava.net/images/blogjava_net/cenwenchu/HBASE.GIF" width="593" border="0" style="line-height: normal; "  alt="" /><br style="line-height: normal; " />
图</span><span style="line-height: normal; font-size: 9pt; ">1&nbsp;</span><span style="line-height: normal; font-size: 9pt; ">框架结构图</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HBASE<span style="line-height: normal; ">依托于</span>Hadoop<span style="line-height: normal; ">的</span>HDFS<span style="line-height: normal; ">作为存储基础，因此结构也很类似于</span>Hadoop<span style="line-height: normal; ">的</span>Master-Slave<span style="line-height: normal; ">模式，</span>Hbase Master Server&nbsp;<span style="line-height: normal; ">负责管理所有的</span>HRegion Server<span style="line-height: normal; ">，但</span>Hbase Master Server<span style="line-height: normal; ">本身并不存储</span>HBASE<span style="line-height: normal; ">中的任何数据。</span>HBASE<span style="line-height: normal; ">逻辑上的</span>Table<span style="line-height: normal; ">被定义成为一个</span>Region<span style="line-height: normal; ">存储在某一台</span>HRegion Server<span style="line-height: normal; ">上，</span>HRegion Server<span style="line-height: normal; ">与</span>Region<span style="line-height: normal; ">的对应关系是一对多的关系。每一个</span>HRegion<span style="line-height: normal; ">在物理上会被分为三个部分：</span>Hmemcache<span style="line-height: normal; ">、</span>Hlog<span style="line-height: normal; ">、</span>HStore<span style="line-height: normal; ">，分别代表了缓存，日志，持久层。通过一次更新流程来看一下这三部分的作用：<br style="line-height: normal; " />
</span></p>
<p align="center" style="line-height: normal; text-align: center; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 9pt; "><img height="483" src="http://www.blogjava.net/images/blogjava_net/cenwenchu/HbaseCommit.GIF" width="723" border="0" style="line-height: normal; "  alt="" /><br style="line-height: normal; " />
图</span><span style="line-height: normal; font-size: 9pt; ">2&nbsp;</span><span style="line-height: normal; font-size: 9pt; ">提交更新以及刷新</span><span style="line-height: normal; font-size: 9pt; ">Cache</span><span style="line-height: normal; font-size: 9pt; ">流程</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: normal; ">由流程可以看出，提交更新操作将会写入到两部分实体中，</span>HMemcache<span style="line-height: normal; ">和</span>Hlog<span style="line-height: normal; ">中，</span>HMemcache<span style="line-height: normal; ">就是为了提高效率在内存中建立缓存，保证了部分最近操作过的数据能够快速的被读取和修改，</span>Hlog<span style="line-height: normal; ">是作为同步</span>Hmemcache<span style="line-height: normal; ">和</span>Hstore<span style="line-height: normal; ">的事务日志，在</span>HRegion Server<span style="line-height: normal; ">周期性的发起</span>Flush Cache<span style="line-height: normal; ">命令的时候，就会将</span>Hmemcache<span style="line-height: normal; ">中的数据持久化到</span>Hstore<span style="line-height: normal; ">中，同时会清空</span>Hmemecache<span style="line-height: normal; ">中的数据，这里采用的是比较简单的策略来做数据缓存和同步，复杂一些其实可以参照</span>java<span style="line-height: normal; ">的垃圾收集机制来做。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: normal; ">在读取</span>Region<span style="line-height: normal; ">信息的时候，优先读取</span>HMemcache<span style="line-height: normal; ">中的内容，如果未取到再去读取</span>Hstore<span style="line-height: normal; ">中的数据。</span></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: normal; ">几个细节：</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">1．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; ">由于每一次</span>Flash Cache<span style="line-height: normal; ">，就会产生一个</span>Hstore File<span style="line-height: normal; ">，在</span>Hstore<span style="line-height: normal; ">中存储的文件会越来越多，对性能也会产生一定影响，因此达到设置文件数量阀值的时候就会</span>Merge<span style="line-height: normal; ">这些文件为一个大文件。</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">2．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Cache<span style="line-height: normal; ">大小的设置以及</span>flush<span style="line-height: normal; ">的时间间隔设置需要考虑内存消耗以及对性能的影响。</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">3．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>HRegion Server<span style="line-height: normal; ">每次重新启动的时候会将</span>Hlog<span style="line-height: normal; ">中没有被</span>Flush<span style="line-height: normal; ">到</span>Hstore<span style="line-height: normal; ">中的数据再次载入到</span>Hmemcache<span style="line-height: normal; ">，因此</span>Hmemcache<span style="line-height: normal; ">过大对于启动的速度也有直接影响。</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">4．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Hstore File<span style="line-height: normal; ">中存储数据采用</span>B-tree<span style="line-height: normal; ">的算法，因此也支持了前面提到对于</span>Column<span style="line-height: normal; ">同</span>Family<span style="line-height: normal; ">数据操作的快速定位获取。</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">5．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>HRegion<span style="line-height: normal; ">可以</span>Merge<span style="line-height: normal; ">也可以被</span>Split<span style="line-height: normal; ">，根据</span>HRegion<span style="line-height: normal; ">的大小决定。不过在做这些操作的时候</span>HRegion<span style="line-height: normal; ">都会被锁定不可使用。</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">6．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Hbase Master Server<span style="line-height: normal; ">通过</span>Meta-info Table<span style="line-height: normal; ">来获取</span>HRegion Server<span style="line-height: normal; ">的信息以及</span>Region<span style="line-height: normal; ">的信息，</span>Meta<span style="line-height: normal; ">最顶部的一个</span>Region<span style="line-height: normal; ">是虚拟的一个叫做</span>Root Region<span style="line-height: normal; ">，通过</span>Root Region<span style="line-height: normal; ">可以找到下面各个实际的</span>Region<span style="line-height: normal; ">。</span></p>
<p style="line-height: normal; margin-left: 57.75pt; text-indent: -36.75pt; color: #555b6e; font-family: Arial; font-size: 12px; ">7．<span style="line-height: normal; font: normal normal normal 7pt/normal 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; ">客户端通过</span>Hbase Master Server<span style="line-height: normal; ">获得了</span>Region<span style="line-height: normal; ">所在的</span>Region Server<span style="line-height: normal; ">，然后就直接和</span>Region Server<span style="line-height: normal; ">进行交互，而对于</span>Region Server<span style="line-height: normal; ">相互之间不通信，只和</span>Hbase Master Server<span style="line-height: normal; ">交互，受到</span>Master Server<span style="line-height: normal; ">的监控和管理。</span></p>
<h3 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span style="line-height: 27px; font-size: 12pt; ">后话</span></h3>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: normal; ">对</span>HBase<span style="line-height: normal; ">还没有怎么使用，仅仅只是看了</span>wiki<span style="line-height: normal; ">去了解了一下结构和作用，暂时还没有需要使用的场景，不过对于各种开源项目的设计有所了解，对自己的框架结构设计也会有很多帮助，因此分享一下。</span></p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/341449.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-24 11:46 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341449.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>NoSQL数据库笔谈v0.2（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341430.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 24 Dec 2010 01:36:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341430.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/341430.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341430.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/341430.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/341430.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://www.yankay.com/nosql数据库笔谈v0-2/最近研究NOSQL，发现此文章，共享之。日前国内没有一套比较完整的NoSQL数据库资料，有很多先驱整理发表了很多，但不是很系统。不材尝试着将各家的资料整合一下，并书写了一些自己的见解。本书写了一些目前的NoSql的一些主要技术，算法和思想。同时列举了大量的现有的数据库实例。读完全篇，相信读者会对NoSQ...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341430.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/341430.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-24 09:36 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341430.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>NoSQL（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/20/341144.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Mon, 20 Dec 2010 03:35:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/20/341144.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/341144.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/20/341144.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/341144.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/341144.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://baike.baidu.com/view/2677528.htm简介　　NoSQL，意即反SQL运动，是一项全新的数据库革命性运动，早期就有人提出，发展至2009年趋势越发高涨。NoSQL的拥护者们提倡运用非关系型的数据存储，相对于目前铺天盖地的关系型数据库运用，这一概念无疑是一种全新的思维的注入。编辑本段现今状况　　现今的计算机体系结构在数据存储方面要...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2010/12/20/341144.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/341144.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-20 11:35 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/20/341144.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Agile Database Refactoring with Hibernate</title><link>http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142403.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Mon, 03 Sep 2007 09:47:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142403.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/142403.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142403.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/142403.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/142403.html</trackback:ping><description><![CDATA[by <a href="http://www.onjava.com/pub/au/3079">Gilad Buzi</a>, <a href="http://www.onjava.com/pub/au/3080">Kelley Glenn</a>, <a href="http://www.onjava.com/pub/au/3081">Jonathan Novich</a> <br />
06/11/2007
<p>Your data model was near perfect when your application was first written. Since then, it has evolved. You've hacked, you've denormalized, and, as a result, you've spent countless hours in meetings ranting about the fixes you need to put in place.</p>
<p>Yet, you're ambivalent. Despite your cogent arguments, you're loath to putting together the "change-all-your-data-all-at-once" plan. It's just too risky. There are countless applications that directly read from and write to your database--you can't change all of them at once! If only you could only fix your data model one piece at a time, and one application at a time.</p>
<p>It's a typical scenario, really. Over time, IT organizations at small, medium, and large enterprises create disparate applications that access vital data stored in a centralized database. And slowly, moderately ill-designed data models start dragging down performance, scalability, and the overall efficiency of an organization.</p>
<p>In this article, we will show readers how to upgrade their faulty schemas and data models without affecting existing applications or processes. By using the latest technology from Hibernate (version 3.0 and up)--along with a combination of database views, stored procedures, and standard design patterns--application developers and data architects can repair a faulty data model, one piece at a time.</p>
<h3>Steps to Follow</h3>
<p>Here's how we'll do it:</p>
<ol>
    <li><em><strong>Dream up an improved data model</strong></em>: Agree on what's wrong with the current model and how you could fix it
    <li><em><strong>Develop database views</strong></em>: Based on the current (faulty) model, these views reflect how you would like your data model to be
    <li><em><strong>Develop stored procedures or "instead of" triggers</strong></em>: These will replace a standard table insert
    <li><em><strong>Develop POJOs, Hibernate mappings, and DAOs</strong></em>: Represent your new data model and tie it to your views
    <li><em><strong>Test, test, test</strong></em>: Prove your data model right </li>
</ol>
<p>Now let's dive into the details of each one of these steps. But first, let's present the example at hand.</p>
<p>Our example is an overly denormalized order system. Instead of dividing the orders into an ORDER table and an ORDER_ITEM table, the original data designer decided to put all order information into one table, CUST_ORDER. We'd love to split this table into two, but how?</p>
<p>Figure 1 shows the original design.</p>
<p><img alt="figure" src="http://www.onjava.com/onjava/2007/06/07/graphics/dma_datamodel_before.png" /><br />
<em>Figure 1. Our data model before the DMA solution</em></p>
<p>Ok, let's get fixin'!</p>
<h3>Dream Up an Improved Data Model</h3>
<p>We decided that we could really split this up fairly easily. It'd be great to achieve something like in Figure 2.</p>
<p><img alt="figure" src="http://www.onjava.com/onjava/2007/06/07/graphics/dma_datamodel_after.png" /><br />
<em>Figure 2. Our data model including the views that improve the overall design</em></p>
<p>By dividing the order data into two tables, we avoid data repetition and have a generally more sustainable data model. But how can we arrive at this model given our existing data structure?</p>
<div style="border-right: #777777 1px solid; padding-right: 8px; border-top: #777777 1px solid; padding-left: 8px; padding-bottom: 8px; margin: 5px; border-left: #777777 1px solid; width: 95%; padding-top: 8px; border-bottom: #777777 1px solid; background-color: #f0f0f0">Communication is key. Although this has little to do with coding and testing, it is an important point. Make sure to involve all stakeholders in the new design of your data model. This includes developers working on other applications that access this data, database administrators who will have to maintain the new data model, and finally technical managers and technical business analysts who may have their own ideas about where the data model should go. We really can't say enough about how important communication is. </div>
<h3>Develop Database Views</h3>
<p>To achieve our desired structure, we can define database views on top of our existing schema that use the current data in our overly denormalized table(s). Our views, however, will present this data in a normalized way. The ORDER_V view is really just a grouped and simplified version of the CUST_ORDER table (removing specific order item information and grouping by the <code>order_id</code>). Here's the definition:</p>
<pre><code>CREATE VIEW dma_example.order_v
AS select
dma_example.cust_order.order_id AS order_id,
dma_example.cust_order.order_cust AS order_cust,
max(dma_example.cust_order.order_date) AS order_date
from dma_example.cust_order
group by dma_example.cust_order.order_id;</code></pre>
<p>The <code>ORDER_ITEM_V</code> view captures only the order item details, ignoring the customer id and the date (information that can be obtained from the <code>ORDER_V</code> view). Here's the <code>ORDER_ITEM_V</code>'s definition:</p>
<pre><code>CREATE VIEW dma_example.order_item_v
AS select
dma_example.cust_order.order_id AS oi_order,
dma_example.cust_order.order_item AS oi_item,
dma_example.cust_order.order_item_price AS oi_price,
dma_example.cust_order.order_item_qty AS oi_qty
from dma_example.cust_order
where (dma_example.cust_order.order_item is not null);</code></pre>
<p>So what we've basically done is split one table into two. <br />
<br />
</p>
<h3>Stored Procedures or INSTEAD OF Triggers</h3>
<p>We now want to be able to treat our new views as if they were tables--inserting, updating, and deleting to our hearts' content without actually worrying about what is going on behind the scenes. Although some views may be directly updatable without any further intervention on the part of the database designer, our views are a little more complex, and we want to make sure we control exactly how the database will affect the underlying (CUST_ORDER) table. The best way to do this is to define code on the database that will execute every time we try to execute one of these CUD operations against our views.</p>
<br />
<p>In most databases (MS SQL Server, Sybase, Oracle, DB2), we can define INSTEAD OF triggers (PostreSQL uses "rules" that behave similarly) that will be responsible for inserting, updating, and deleting records from the underlying table from which the view is defined. MySQL, however, does not currently support INSTEAD OF triggers. In their place, we can create stored procedures and, through careful configuration of Hibernate mapping files, call these stored procedures every time a CUD operation is triggered in our code (and persisted by Hibernate). Be it stored procedures or <em>instead of</em> triggers, the code is very similar.</p>
<p>Since our example uses MySQL, we will demonstrate our solution using stored procedures.</p>
<h4>The code</h4>
<p>Our stored procedures for inserting, updating, and deleting into our denormalized table must take into account all aspects of the denormalization: repetitive rows, additional fields, superfluous values, etc. When we use these stored procedures, the data model we created with the definition of nice, normalized views is turned back into the flawed, denormalized structure. Why? Because the rest of our applications are expecting the data to be presented in this way. In addition, our view definitions rely on the data to exist in the current structure.</p>
<p>So what does one of these procedures look like? Here's an example of inserting an item to the order:</p>
<pre><code>create procedure insert_order_item
(in itemprice FLOAT, in itemqty INT, in orderid INT, in itemid INT)
LANGUAGE SQL
BEGIN
DECLARE p_order_id INT;
DECLARE p_cust_id INT;
DECLARE max_order_id INT;
DECLARE p_itemprice FLOAT;
-- apply the current price to the line item
if itemprice is null then
select prod_price into p_itemprice from product where prod_id=itemid;
else
set p_itemprice = itemprice;
end if;
-- get the customer id.
select order_cust into p_cust_id
from cust_order where order_id=orderid limit 1;
insert into cust_order
(order_id, order_cust, order_date,
order_item, order_item_price, order_item_qty)
values
(orderid, p_cust_id, now(), itemid, p_itemprice, itemqty);
END</code></pre>
<p>Notice that whatever data is usually missing from the <code>ORDER_ITEM_V</code> view has to be sought out and inserted in the underlying <code>CUST_ORDER</code> table. This procedure, if successful in inserting into the <code>CUST_ORDER</code> table, will return the number of rows affected as 1. It is important to note that Hibernate expects either 1 or 0 as a result of these stored procedures, since it treats them as single rows in tables (even though they are really views). To ensure that this happens, we might have to throw little tricks into our stored procedures. For instance, the stored procedure to update an order may affect various rows in the <code>CUST_ORDER</code> table (one row for every order item). If we were to simply update all the rows with the given order ID, the rows' affected value returned would be greater than 1. Since this would present a problem for Hibernate, we use a small table and update it after the update to the <code>CUST_ORDER</code> table. This causes the stored procedure to return 1 as the number of affected rows (since the update we executed only affects one row). Here is what the stored procedure looks like:</p>
<pre><code>create procedure update_order
(in ordercust INT, in orderdate DATETIME, in orderid INT)
LANGUAGE SQL
BEGIN
update cust_order set order_cust=ordercust,
order_date=orderdate
where order_id=orderid;
if row_count() &gt; 0 then
update help_table set i=i+1;
end if;
END</code></pre>
<h3>POJOs, Hibernate Mappings, and DAOs</h3>
<p>Creating the POJOs and Hibernate mappings for your new, view-based data model is fairly straightforward. There are, however, a couple of gotchas to keep in mind here.</p>
<h4>Virtual foreign and primary keys</h4>
<p>Although database views do not have foreign and primary keys, you should still map these in your solution's mapping file. This allows other developers to treat this new data model as if it were a true physical model. Furthermore, mapping these elements will ensure an almost seamless transition when you move on to a final solution based on real tables.</p>
<h4>Overriding insert, update, and delete</h4>
<p>When using stored procedures (you do not need to do this if your solution is implementing instead of triggers), you must override the insert, update, and delete calls with calls to your stored procedures. This is done by adding <code>&lt;sql-insert&gt;</code>, <code>&lt;sql-update&gt;</code> and <code>&lt;sql-delete&gt;</code> elements to the mapping. These elements tell Hibernate to call the given procedures instead of inserting, updating, and deleting directly to the database. Here is the <code>ORDER_V</code> mapping:</p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
&lt;hibernate-mapping&gt;
&lt;class name="org.onjava.shared.vo.Order" table="order_v" catalog="dma_example"&gt;
&lt;id name="orderId" type="int" column="order_id" /&gt;
&lt;property name="orderCust" type="int" column="order_cust" /&gt;
&lt;property name="orderDate" type="timestamp" column="order_date" length="19"/&gt;
&lt;set name="items" inverse="true" cascade="all-delete-orphan" lazy="true"&gt;
&lt;key column="oi_order"/&gt;
&lt;one-to-many class="org.onjava.shared.vo.OrderItem" /&gt;
&lt;/set&gt;
&lt;sql-insert callable="true"&gt;{call insert_order(?, ?, ?)}&lt;/sql-insert&gt;
&lt;sql-update callable="true"&gt;{call update_order(?, ?, ?)}&lt;/sql-update&gt;
&lt;sql-delete callable="true"&gt;{call delete_order(?)}&lt;/sql-delete&gt;
&lt;/class&gt;
&lt;/hibernate-mapping&gt;</code></pre>
<p>The parameter order is important here. Refer to the <a href="http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#querysql-cud">custom SQL reference</a> of the Hibernate manual to determine the parameter order in your stored procedures.</p>
<h4>Data access objects</h4>
<p>Once the right mapping is in place, the data access objects for the view-based data model are identical to table-based models. Hibernate takes care of executing the stored procedures and treats the views much like tables. See this article's sample DMA solution for complete data access classes for the <code>ORDER_V</code> and <code>ORDER_ITEM_V</code> views.</p>
<br />
<h3>Test, Test, Test</h3>
<p>Extensive testing is one of the most important activities during the creation of a DMA solution. Only thorough testing can ensure a correctly functioning view-based (logical) data model. All aspects of the new data model must be explored in tests. And, of course, it is imperative to test both working cases and failing cases.</p>
<br />
<p>A great aid in automating testing is DBUnit. Although we won't go into great detail on how DBUnit works (a great <a href="http://www.onjava.com/pub/a/onjava/2004/01/21/dbunit.html">OnJava article</a> by Andrew Glover already does that) a couple of important pointers should be noted:</p>
<ul>
    <li><em><strong>Data diversity</strong></em>: Make sure your test data clearly reflects all sorts of different data scenarios, including foreign key relationships and null values.
    <li><em><strong>Dataset size</strong></em>: Although it is important to maintain a large enough data set to support all of your tests, keep in mind that DBUnit empties and reloads your data during every test method. Large data sets may lead to slow testing. </li>
</ul>
<p>As far as the tests themselves, look at them as a way to exercise your DAOs and value objects. Here are some of the kinds of tests we recommend. For a closer look at the implementation, look at the sample code included with this article.</p>
<ul>
    <li><em><strong>Find All</strong></em>: Make sure the DAO returns the expected number of rows.
    <li><em><strong>Find one</strong></em>: Look up a record using the virtual primary key and make sure the correct record is returned with all the expected (column) values in place.
    <li><em><strong>Insert</strong></em>: Insert a record and verify it has been inserted.
    <li><em><strong>Insert multiple records</strong></em>: Make sure inserting is working for more than one skill at a time.
    <li><em><strong>Insert duplicates</strong></em>: Attempt to violate the virtual primary key constraint.
    <li><em><strong>Delete</strong></em>: Delete a record and verify that it has really been deleted.
    <li><em><strong>Delete multiples</strong></em>: Do so only if your DAO design supports doing this.
    <li><em><strong>Update</strong></em>: Update and make sure the update has been persisted to the DB.
    <li><em><strong>Violate constraints</strong></em>: Make sure all possible virtual constraint conditions are tested.
    <li><em><strong>Optimistic locking</strong></em>: There are several ways to generate optimistic locking exceptions. All of these should be attempted in order to verify the correct functioning of the optimistic locking exception mechanism. Four different kinds of optimistic locking conditions should be tested:
    <ul>
        <li>Delete a record when a record has already been deleted.
        <li>Delete a record when it has been updated since the last fetch (only possible if you are maintaining a version column).
        <li>Update a record that has been deleted.
        <li>Update a record that has been updated since the last fetch (only possible if you are maintaining a version column). </li>
    </ul>
    </li>
</ul>
<p>Once you have completed all of these tests, you can be confident that your new data model is fairly bomb-proof.</p>
<!-- sidebar begins --><!-- don't move sidebars --><!-- sidebar ends -->
<h3>Peaceful Coexistence</h3>
<p>Now that you have upgraded your application to use a sane data model, keep in mind that other applications will be accessing the same data using slightly different points of contact. This shouldn't worry you, it's just something to keep in mind. Figure 3 demonstrates how your new and improved application lives peacefully alongside the existing legacy applications.</p>
<p><img alt="figure" src="http://www.onjava.com/onjava/2007/06/07/graphics/dma_side_by_side.png" /><br />
<em>Figure 3. A legacy application and the DMA solution coexisting peacefully and manipulating the same data set albeit through different models</em></p>
<h3>Now What? Migration to a Permanent Data Model</h3>
<p>So you've implemented this fancy solution to fix your data model. As the months go by, developers update their applications and begin to use this new view-based data model. But the underlying denormalization (or whatever faulty design exists) is still there, and you want to get rid of it. But how? It's actually simpler than you might think. Here is the step-by-step guide:</p>
<ol>
    <li><em><strong>Develop tables</strong></em>: These will look much like your views, but will have real foreign keys and indexes. Make sure to maintain the same column names as the views themselves.
    <li><em><strong>Load tables</strong></em>: You will load the tables with data from your already existing views. Select from view into table. It's really that simple.
    <li><em><strong>Modify your mappings</strong></em>: Change your Hibernate mappings to reflect the table names instead of the views and, if you used stored procedures, get rid of the &lt;sql-insert&gt;, &lt;sql-update&gt; and &lt;sql-delete&gt; elements (since now you won't need them).
    <li><em><strong>Test, test, test</strong></em>: Your original tests should work with absolutely no modification. Run them over and over again to prove that. </li>
</ol>
<p>Voila! If you've done things right, not a single line of Java code needs to be modified, and your applications will behave exactly the same. This is where the true beauty of this kind of solution is evident. By abstracting the data model through Hibernate and database procedures, you can achieve an impressive change with little effort. Of course, this doesn't mean that you shouldn't retest everything thoroughly--the good news is that your tests are still totally valid as well (if you are using XML data sets, make sure to replace the view name with the table name).</p>
<h3>Conclusion</h3>
<p>Using some of the latest and greatest Hibernate technologies, Java testing methodologies, and smart use of your databases resources, we have shown you that iterative change is possible. What is magical about solving data model problems this way is that the solution is mutually inclusive. This means that while you have solved the problem for yourself (in your application), other applications accessing the same data can continue to operate fine until they wise up and jump on your corrected bandwagon. It's a really friendly approach to data model migration.</p>
<p>As a final note, we would like to remind you to keep the following in mind when implementing your solution:</p>
<ul>
    <li><strong><em>Zero impact</em></strong>: DMA solutions must not affect current applications or processes. This means that while records are modified in the new (virtual) data model, the existing set of data (in the faulty data model) must reflect the change.
    <li><strong><em>Maintain functionality</em></strong>: While a new data model may eliminate the importance of certain data columns, it is important to simultaneously maintain the old behavior. Calculated fields are a perfect example of this. If a legacy application calculates a field before insertion, but the new data model doesn't even include the field, the DMA solution must respect the old behavior and calculate (and insert) the field.
    <li><strong><em>Testing for complete confidence</em></strong>: DMA solutions that are created must be thoroughly tested in order to provide confidence that the new schema is as solid as a direct database schema is. The DMA pattern may not be a straightforward mapping of tables and columns, and is therefore vulnerable to bugs. The challenge then, is in testing the solution and making sure it adheres to complete <a href="http://en.wikipedia.org/wiki/ACID">ACID principles.</a> </li>
</ul>
<h3><a id="resources" name="resources">Resources</a> </h3>
<ul>
    <li><a href="http://www.onjava.com/onjava/2007/06/07/examples/dmasample.zip">Sample code</a> for this article
    <li><a href="http://www.hibernate.org/hib_docs/v3/reference/en/html_single">Hibernate documentation</a> with further information about Hibernate
    <li><a href="http://dev.mysql.com/doc/refman/5.0/en/index.html">MySQL documentation</a> to guide you in writing stored procedures for MySQL </li>
</ul>
<p>
<p><em><a href="http://www.onjava.com/pub/au/3079">Gilad Buzi</a> has been involved in data driven application development for over ten years. He is currently a Principal Software Engineer with The Code Works Inc.</em></p>
<p><em><a href="http://www.onjava.com/pub/au/3080">Kelley Glenn</a> has worked in the software development industry for more than 10 years, with experience in telephony billing and enterprise application integration.</em></p>
<p><em><a href="http://www.onjava.com/pub/au/3081">Jonathan Novich</a> is co-founder and partner at The Code Works, Inc. with more than 10 years of experience in software consulting and development.</em></p>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/142403.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2007-09-03 17:47 <a href="http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142403.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ibatis 开发指南</title><link>http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142321.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Mon, 03 Sep 2007 07:33:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142321.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/142321.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142321.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/142321.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/142321.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;ibatis 开发指南http://www.360doc.com/showWeb/0/0/262045.aspx相对Hibernate和Apache OJB 等&#8220;一站式&#8221;ORM解决方案而言，ibatis 是一种&#8220;半自动化&#8221;的ORM实现。所谓&#8220;半自动&#8221;，可能理解上有点生涩。纵观目前主流的ORM，无论H...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142321.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/142321.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2007-09-03 15:33 <a href="http://www.blogjava.net/hengheng123456789/archive/2007/09/03/142321.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>osql批处理ms sqlserver 脚本的语句</title><link>http://www.blogjava.net/hengheng123456789/archive/2007/02/07/98571.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 07 Feb 2007 05:10:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2007/02/07/98571.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/98571.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2007/02/07/98571.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/98571.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/98571.html</trackback:ping><description><![CDATA[   我们在实际项目应用中经常会遇到执行很多SQL脚本语句的情况，但是如果SQL脚本文件过大，MS SqlServer 的查询分析器会拒绝打开，这时候我们就可以使用osql 命令来处理。<br /><br />1、假设脚本文件为create-sql-server.sql，并且在目录C:\Program Files\Microsoft SQL Server\80\Tools\Binn中。<br /><br />2、进入目录C:\Program Files\Microsoft SQL Server\80\Tools\Binn。<br /><br />3、执行如下命令：<br /><br />C:\Program Files\Microsoft SQL Server\80\Tools\Binn&gt;osql -U sa -P sa -S 127.0.0.1 -d NorthWind -i create-sql-server.sql<br /><br />-U 用户名<br />-P 密码<br />-S 机器IP<br />-d 数据库名称<br />-i 脚本文件名称<br /><img src ="http://www.blogjava.net/hengheng123456789/aggbug/98571.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2007-02-07 13:10 <a href="http://www.blogjava.net/hengheng123456789/archive/2007/02/07/98571.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySql之5.0使用心得</title><link>http://www.blogjava.net/hengheng123456789/archive/2007/01/10/92967.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 10 Jan 2007 09:07:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2007/01/10/92967.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/92967.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2007/01/10/92967.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/92967.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/92967.html</trackback:ping><description><![CDATA[
		<p>   前两天很是使用了一把MySql，版本是5.0.27，对字符集的设置、存储过程的建立、触发器的建立颇有心得，现与大家分享实例。<br /><br />1、字符集的设置<br /><br />   a、首先安装MySql5.0.27，注意！是setup安装，不是免安装版。（如何设置免安装版的字符集俺还不会）<br /><br />   b、运行MySql Server Instance Configuration Wizard，在please select the default character set页面选择Menual Selected Default Character Set / Collation，并在Character Set:下拉框中选择gb2312（经测试，从MS SQL Server中导入中文字符是成功的。）<br /><br />   c、然后启动MySql服务即可！<br /><br />2、存储过程的建立<br /><br />   a、为了方便执行sql语句，我建议安装MySQL GUI Tools 5.0的可视化工具，十分方便！<br /><br />   b、打开MySQL GUI Tools 5.0的MySql Query Browser工具，连接localhost，用户名为root，密码为空。<br /><br />   c、选择File-〉New Script Tab<br /><br />   d、在Script 1的Tab页中输入以下语句，选择Execute按钮执行即可。<br /><br />-- 建立数据库 sample ，并使用它<br />create database sample;<br />use sample;<br /><br />-- 建立一个表<br /><br />create table Msg (<br />  MsgServiceId     integer,<br />  MsgTypeId        integer,<br />  MsgQueuedAt    datetime,<br />  MsgParam1        varchar(60)  NULL,<br />  MsgId     int NOT NULL auto_increment,         -- 自增<br />  PRIMARY KEY  (`MsgId`)                              -- 主键<br />)DEFAULT CHARSET=gb2312;                        -- 设置表的字符集<br /><br />-- 建立索引<br /><br />create unique index BCG_Msg_idx2 on Msg ( MsgTypeId,  MsgId );<br /><br />-- 建立存储过程<br /><br /><font color="#ff1493">DELIMITER |  --必须有此句</font></p>
		<p>CREATE PROCEDURE BCG_QueueMsg<br />   (<br />     in v_MsgServiceName varchar(30),<br />     in v_MsgParam1      varchar(60)<br />   )<br />BEGIN<br />      DECLARE v_MsgTypeId integer;<br />      DECLARE v_QueuedAt  timestamp;<br />      set v_QueuedAt = now();<br />      SELECT MsgServiceId INTO v_MsgServiceId FROM BCG_MsgService where MsgServiceName=v_MsgServiceName;<br />      INSERT INTO Msg (<br />        MsgTypeId, <br />        MsgQueuedAt, <br />        MsgParam1<br />      )<br />      VALUES (<br />        v_MsgTypeId,<br />        v_QueuedAt, <br />        v_MsgParam1<br />      );<br />END;<br /><font color="#ff1493">|<br />DELIMITER ;</font><br /><br />3、触发器的建立<br /><br />   a、选择File-〉New Script Tab<br /><br />   b、在Script 2的Tab页中输入以下语句，选择Execute按钮执行即可。<br /><br />-- 使用sample数据库<br />use sample;<br /><br />-- 建立测试表<br /><br />CREATE TABLE Orders (<br /> OrderID int NOT NULL auto_increment ,<br /> CustomerID nchar (5)  NULL ,<br /> EmployeeID int NULL ,<br /> OrderDate datetime NULL ,<br /> RequiredDate datetime NULL ,<br /> ShippedDate datetime NULL ,<br /> ShipVia int NULL ,<br /> Freight float NULL ,<br /> ShipName nvarchar (40) NULL ,<br /> ShipAddress nvarchar (60)  NULL ,<br /> ShipCity nvarchar (15)  NULL ,<br /> ShipRegion nvarchar (15)  NULL ,<br /> ShipPostalCode nvarchar (10)   NULL ,<br /> ShipCountry nvarchar (15)   NULL,<br />  PRIMARY KEY  (`OrderID`)<br />)DEFAULT CHARSET = gb2312;<br /><br />-- 建立触发器 TESTDBINSERT  ，当有记录插入表时，同时插入Msg表一条记录<br /><br />DELIMITER |<br />CREATE TRIGGER TESTDBINSERT  AFTER  INSERT ON ORDERS<br />FOR EACH ROW<br />BEGIN<br />    -- 把新插入orders表中新记录的OrderID字段的值赋给ID，<font color="#ff1493">NEW表示当前插入的记录项内容<br /></font>    SET @ID = <font color="#ff1493">NEW</font>.OrderID;  </p>
		<p>    -- 获得当前时间<br />    SET @v_QueuedAt = NOW();<br /><br />    -- 把以上得到的值插入Msg表中<br />    INSERT INTO Msg (<br />        MsgTypeId,<br />        MsgQueuedAt, <br />        MsgParam1<br />    ) VALUES (<br />        1,<br />        @v_QueuedAt, <br />        @ID<br />    );<br />END;<br />|<br />DELIMITER ;<br /><br />   c、这时在表orders中插入一条记录，则Msg表中也增加一条记录。<br /><br />最后说明MySQL GUI Tools 5.0是一个十分好用的工具，还有可以从其他数据库中导入数据的GUI工具，希望大家多多研究。</p>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/92967.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2007-01-10 17:07 <a href="http://www.blogjava.net/hengheng123456789/archive/2007/01/10/92967.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>