使用 Hibernate Shards 对数据进行切分

当一个大型应用试图在一个单一的数据库表增加TB的数据时,性能经常会下降,对所有数据编索引对数据的读写都会很耗时,显然,如果我们根据某种策略将单一的表数据存储到不同的数据库表中,将会大大提高性能,切分将是一个令人振奋的可选方法。

切分分为纵向切分和横向切分,纵向切分就是将数据库表为单元进行切分,不同的数据存储到不同的数据库中,例如:有数据库表A,B,其中A存储在数据库A中,B存储在数据库中。
横向切分,将同一个表的数据进行切分,表中的数据分别存储到不同的数据库中,例如:同一个数据库表,亚洲的数据存储在数据库A中,其它洲的存储在数据库B中。

hibernate Shards 就是一个让应用横向切分的分布式数据库解决方案,它可以让一个 Hibernate应用很简单的加入横向切分功能,以下是我在通过参考一些文摘后应用JAVA写的一个测试例子,本例子就以存储 天气预报 信息来进行,我们将要做的是将天气预报信息按洲的方式存储到对应的数据库,来达到性能的提升。

在继续本例程开始之前你需要下载:
Hibernate3.2 应用开发包:http://sourceforge.net/projects/hibernate/files/hibernate3/
Hibernate3.0 Shards 应用开发包:http://sourceforge.net/projects/hibernate/files/hibernate-shards/
Spring2.5 应用开发包:http://www.springsource.org/download

开发包准备好后我们按以下过程来开始Hibernate Shards 旅程:
1、创建数据库与数据库表:
在本示例我将使用SQL SERVER 200 来做测试

create database ASIA --亚洲相关信息数据库
create database AFRICA --非洲相关信息数据库
GO
--在ASIA与AFRICA 数据库中分别创建天气预报数据表
--创建天气序报数据表
IF EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME='WEATHER_REPORT')
DROP TABLE WEATHER_REPORT
GO
CREATE TABLE WEATHER_REPORT (
    REPORT_ID VARCHAR(50) PRIMARY KEY DEFAULT 0 NOT NULL, --预报ID
    CONTINENT varchar(20) not null, --所属的洲
    LATITUDE FLOAT,  --纬度
    LONGITUDE FLOAT, --经度
    TEMPERATURE INT, --温度
    REPORT_TIME datetime --预报日期
)

 

2、创建工程
为了更切实意,我使用MYECLIPSE创建一个名为:hibernateShards JAVA 工程并将下载的相关应用包添加引用,开发可根据自身使用的工具选择

2.1、创建hibernate 相关切分配置文件shard0.hibernate.cfg.xml 和 shard1.hibernate.cfg.xml
shard0.hibernate.cfg.xml:

<!-- 切分的 hibernate 配置文件,每个数据库都应有这样一个描述文件 -->
<hibernate-configuration>
      <session-factory name="HibernateSessionFactory0">
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ASIA</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">sa</property>
        
        <!-- 切分 ID,需保持一致 -->
        <property name="hibernate.connection.shard_id">0</property>
        
        <!-- 对跨切分关系是否进行检查,在生产环境尽量设为 false,因为检查会非常花时间 -->
        <property name="hibernate.shard.enable_cross_shard_relationship_checks">true</property>
        
      </session-factory>
</hibernate-configuration>

 shard1.hibernate.cfg.xml:

<hibernate-configuration>
      <session-factory name="HibernateSessionFactory0">
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=AFRICA</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">sa</property>
        
        <!-- 切分 ID,需保持一致 -->
        <property name="hibernate.connection.shard_id">1</property>
        
        <!-- 对跨切分关系是否进行检查,在生产环境尽量设为 false,因为检查会非常花时间 -->
        <property name="hibernate.shard.enable_cross_shard_relationship_checks">true</property>
        
      </session-factory>
</hibernate-configuration>

 我们看到以上二个文件内容非常类似,唯一不同的是数据库名和切分ID不同

2.2、建立相关hibernate对象关系映射文件和相关持久对象
weather.hbm.xml

<hibernate-mapping package="com.hibernateshards.po">
	<class name="WeatherReport" table="weather_report" dynamic-update="true"
           dynamic-insert="true">
		<id name="reportId" column="REPORT_ID" unsaved-value="-1">
		
			<!-- 在这里使用 UUID 来生成主键,以防止ID在插入不同的数据库中ID值重复 -->
			<generator class="org.hibernate.shards.id.ShardedUUIDGenerator"></generator>
			
		</id>
		<property name="continent" column="CONTINENT" type="string"></property>
		<property name="latitude" column="LATITUDE" type="float"></property>
		<property name="longitude" column="LONGITUDE" type="float"></property>
		<property name="tempErature" column="TEMPERATURE" type="int"></property>
		<property name="reportTime" column="REPORT_TIME" type="date"></property>
	</class>
</hibernate-mapping>
 

持久对象:WeatherReport.java

/**
 * 天气预报相关信息对象
 *
 * @date 2010-10-23下午06:38:21
 * 
 * @author ChenTao
 */
public class WeatherReport {

	private BigInteger reportId;
	private String continent;
	private Float latitude;
	private Float longitude;
	private Integer tempErature;
	private Date reportTime;

	// getter 和 setter 方法略.....
}

 2.3、定义切分选策略和创建SessionFactory 对象
WeatherShardSelectionStrategy.java

/**
 * 
 * 切分选择策略,根据自已定义
 *
 * @date 2010-10-23下午06:58:04
 *
 * @author ChenTao
 */
public class WeatherShardSelectionStrategy implements ShardSelectionStrategy{
	
	/**
	 * 选择策略:
	 * 如果持久化对象是一个 WeatherReport,那么其所在的洲被确定,因此选择了一个切分。在这种情况下,
	 * 有两个切分:0 和 1,其中切分 0 中包含 亚洲 相关天气预报,切分 1 中包含除亚洲之外所有的洲天气预报。
	 * 当然在这里可以将每个洲都定义,但这里只做一个测试应用,只描述其操作流程
	 * 
	 */
	public ShardId selectShardIdForNewObject(Object obj) {
		if(obj instanceof WeatherReport){
			WeatherReport weatherReport = (WeatherReport) obj;
			return this.continentShardId(weatherReport.getContinent());
		}
		throw new IllegalArgumentException();
	}

	/**
	 * 根据洲来获取切分
	 *
	 * @param continent
	 * @return
	 */
	private ShardId continentShardId(String continent){
		if(Constants.CONTINENT_ASIA.equals(continent)){
			return new ShardId(0);
		} else {
			return new ShardId(1);
		}
	}
	
}

 // 创建SessionFactory
ShardedSessionFactoryBuilder.java

public class ShardedSessionFactoryBuilder {

	private List<String> hibernateConfigurations;
	
	private List<String> resourceConfigurations;

	public void setHibernateConfigurations(List<String> hibernateConfigurations) {
		this.hibernateConfigurations = hibernateConfigurations;
	}

	public void setResourceConfigurations(List<String> resourceConfigurations) {
		this.resourceConfigurations = resourceConfigurations;
	}
	
	public SessionFactory createSessionFactory(){
		Configuration propotypeConfig = this.getPrototypeConfig(this.hibernateConfigurations.get(0), this.resourceConfigurations);
		
		List<ShardConfiguration> shardConfigs = new ArrayList<ShardConfiguration>();
		for(String hibernateConfig:this.hibernateConfigurations){
			shardConfigs.add(this.buildShardConfig(hibernateConfig));
		}
		
		ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
		
		ShardedConfiguration shardedConfig = new ShardedConfiguration(propotypeConfig,shardConfigs,shardStrategyFactory);
		
		return shardedConfig.buildShardedSessionFactory();
	}
	
	/**
	 * 创建一个 Hibernate 配置 
	 *
	 * @param hibernateFile
	 * 					hibernate 切分配置文件
	 * @param resourceFiles
	 * 					资源文件
	 * 
	 * @return
	 */
	public Configuration getPrototypeConfig(String hibernateFile,List<String> resourceFiles){
		Configuration prototypeConfig = this.getConfiguration(hibernateFile);
		for(String res:resourceFiles){
			prototypeConfig.addResource(res);
		}
		return prototypeConfig;
	}
	
	public Configuration getConfiguration(String hibernateFile){
		return new Configuration().configure(hibernateFile);
	}
	
	private ShardStrategyFactory buildShardStrategyFactory(){
		ShardStrategyFactory shardStrategyFactory = new ShardStrategyFactory(){
			public ShardStrategy newShardStrategy(List<ShardId> shardIds){
				ShardSelectionStrategy pss = new WeatherShardSelectionStrategy();
				ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(shardIds);
				ShardAccessStrategy pas = new SequentialShardAccessStrategy();
				return new ShardStrategyImpl(pss,prs,pas);
			}
		};
		return shardStrategyFactory;
	}
	
	private ShardConfiguration buildShardConfig(String hibernateConfig){
		return new ConfigurationToShardConfigurationAdapter(this.getConfiguration(hibernateConfig));
	}
	
}

 2.4、定义数据访问接口和实现对象
接口定义
WeatherReportDao.java

/**
 * 天气预报数据操作相关接口定义
 *
 * @date 2010-10-23下午06:38:50
 * 
 * @author ChenTao
 */
public interface WeatherReportDao {
	
	/**
	 * 查询所有天气预报相关数据
	 *
	 * @return
	 * @throws HibernateException
	 */
    List<WeatherReport> findAll() throws HibernateException;

    /**
     * 根据天气预报ID进行查询
     *
     * @param id  java.lang.Integer
     * 			
     * @return
     * @throws HibernateException
     */
    WeatherReport findById(Integer id) throws HibernateException;

    /**
     * 查据天气预报所在的洲进行查询
     *
     * @param continent  java.lang.String
     * 
     * @return
     * @throws HibernateException
     */
    List<WeatherReport> findByContinent(String continent) throws HibernateException;

    /**
     * 保存天气预报数据
     *
     * @param weather com.hibernateshards.po.WeatherReport
     * 
     * @throws HibernateException
     */
    void save(WeatherReport weather) throws HibernateException;

    /**
     * 更改天气预报数据
     *
     * @param weather com.hibernateshards.po.WeatherReport
     * 
     * @throws HibernateException
     */
    void update(WeatherReport weather) throws HibernateException;

    /**
     * 删除天气预报数据
     *
     * @param weather com.hibernateshards.po.WeatherReport
     * 
     * @throws HibernateException
     */
    void delete(WeatherReport weather) throws HibernateException;
}

 
真实的数据操作对象
WeatherReportDaoImpl.java

/**
 * 天气预报数据层交互对象,用于处理数据的查找与CURD操作
 *
 * @date 2010-10-23下午06:28:25
 * 
 * @author ChenTao
 */
public class WeatherReportDaoImpl implements WeatherReportDao {

	private SessionFactory sessionFactory;

	public WeatherReportDaoImpl() {
		super();
	}

	@SuppressWarnings("unchecked")
	public List<WeatherReport> findAll() throws HibernateException {
		return (List<WeatherReport>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						return session.createQuery("from WeatherReport w")
								.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	public List<WeatherReport> findByContinent(final String continent)
			throws HibernateException {
		return (List<WeatherReport>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						return session
								.createQuery(
										"from WeatherReport as weather where weather.continent=?")
								.setString(0, continent.trim()).list();
					}
				});
	}

	public WeatherReport findById(final Integer id) throws HibernateException {
		return (WeatherReport) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						return session
								.createQuery(
										"from WeatherReport as weather where weather.reportId=?")
								.setInteger(0, id).uniqueResult();
					}
				});
	}

	public void save(final WeatherReport weather) throws HibernateException {
		this.getHibernateTemplate().save(weather);
	}

	public void update(final WeatherReport weather) throws HibernateException {
		this.getHibernateTemplate().saveOrUpdate(weather);
	}

	public void delete(final WeatherReport weather) throws HibernateException {
		this.getHibernateTemplate().delete(weather);
	}

	public void setSessionFactory(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private HibernateTemplate getHibernateTemplate() {
		return new HibernateTemplate(sessionFactory);
	}

}

 2.5、配置Spring
因为前面我们以经下载了 spring 相关开发包,我将要使用spring来加载相关配置和托管相应Bean创建和SessionFactory对象的创建
weatherSpring-config.xml

<beans>
	<bean id="shardedSessionFactoryBuilder"
		class="com.hibernateshards.shardsupport.ShardedSessionFactoryBuilder">
		<property name="hibernateConfigurations">
			<list>
				<value>shard0.hibernate.cfg.xml</value>
				<value>shard1.hibernate.cfg.xml</value>
			</list>
		</property>
		<property name="resourceConfigurations">
			<list>
				<value>com/hibernateshards/po/weather.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<!-- 连接域对象 -->
	<bean id="sessionFactory" factory-bean="shardedSessionFactoryBuilder"
		factory-method="createSessionFactory"></bean>
	
	<bean id="weatherReportDao" class="com.hibernateshards.domain.WeatherReportDaoImpl">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
</beans>

 2.6、测试
通过以上的流程下来所有需要实现的代码和配置都已完成,虽然写了这么多,也不知道是否真的可以实现跨数据进行数据库的CRUD操作,为了验证真实性,我写了一个测试对象进

行测试,包含对数据的CRUD 都通过测试,来看下面测试方法:
WeatherReportTest.java

/**
 * 对 Shards 进行测试
 * 
 * @date 2010-10-23下午06:37:56
 * @author ChenTao
 */
public class WeatherReportTest {

	/**
	 * Spring 的上下文,
	 */
	private static ApplicationContext ac;

	/**
	 * 数据操作对象
	 */
	private static WeatherReportDao weatherReportDao;

	public static void main(String[] args) {

		try {
			/* 加载 spring 配置 */
			loadConfig();

			weatherReportDao = (WeatherReportDao) getAc().getBean("weatherReportDao");
			
			/* 亚洲,以下数据将会增加到 ASIA 数据库中 */
//			save(Constants.CONTINENT_ASIA);

			/* 非洲,以下数据将会增加到 AFRICA 数据库中 */
//			save(Constants.CONTINENT_AFRICA);

			/* 对数据进行修改 */
//			update();
			
			/* 对数据进行删除 */
//			delete();
			
			List<WeatherReport> weatherList = null;
			
			/* 查询所有天气预报数据,这将会扫描所有的数据库 */
			weatherList = finaAll();
			print(weatherList);
			
//			// 按洲查询,指定某个数据库查询,其体看配置
//			weatherList = findByContinent(Constants.CONTINENT_ASIA);
//			print(weatherList);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static List<WeatherReport> finaAll() throws Exception {
		if (null != weatherReportDao) {
			return weatherReportDao.findAll();
		}
		throw new Exception("No Data!");
	}

	private static List<WeatherReport> findByContinent(String continent)
			throws Exception {
		if (null != weatherReportDao) {
			return weatherReportDao.findByContinent(continent);
		}
		throw new Exception("No Data!");
	}
	
	private static void save(String continent) throws Exception {
		if (null != weatherReportDao) {
			WeatherReport weather = new WeatherReport();
			weather.setContinent(continent);
			weather.setLatitude(30.20F);
			weather.setLongitude(60.10F);
			weather.setTempErature(28);
			weather.setReportTime(new java.sql.Date(new java.util.Date()
					.getTime()));
			weatherReportDao.save(weather);
			System.out.println("数据保存成功");
		}
	}
	
	private static void update() throws Exception {
		WeatherReport weather = new WeatherReport();
		weather.setReportId(BigInteger.valueOf((long)100));
		weather.setContinent(Constants.CONTINENT_ASIA);
		weather.setLatitude(100.20F);
		weather.setLongitude(100.10F);
		weather.setTempErature(34);
		weather.setReportTime(new java.sql.Date(new java.util.Date()
				.getTime()));
		weatherReportDao.update(weather);
		System.out.println("数据修改成功");
	}
	
	private static void delete() throws Exception {
		WeatherReport weather = new WeatherReport();
		weather.setReportId(BigInteger.valueOf((long)100));
		weather.setContinent(Constants.CONTINENT_ASIA);
		weather.setLatitude(50.20F);
		weather.setLongitude(60.10F);
		weather.setTempErature(28);
		weather.setReportTime(new java.sql.Date(new java.util.Date()
				.getTime()));
		weatherReportDao.delete(weather);
		System.out.println("数据删除成功");
	}

	private static void loadConfig() throws Exception {
		loadConfig("weatherSpring-config.xml");
	}

	private static void loadConfig(String configFile) throws Exception {
		ac = (new ClassPathXmlApplicationContext(configFile));
	}

	public static ApplicationContext getAc() {
		return ac;
	}

	private static void print(List<WeatherReport> weatherList) {
		if (null != weatherList && weatherList.size() > 0) {
			for (WeatherReport w : weatherList) {
				System.out.println("预报ID:" + w.getReportId() + "=洲"
						+ w.getContinent());
			}
		}
	}

}

 
通过以上测试我们发现,当 Continent 为 ASIA 数据插入到了 ASIA 数据库中,当Continent 为 AFRICA 数据插入到了AFRICA数据库中,同时我们在查询的时候可以查询所有的数

据,查询所有的数据在这里不建议使用,我在这里提供了findByContinent按洲查询,这样就只会读取相关洲的数据库,而避免了在读取相应洲的数据时排描所有洲的数据,增加了

应用程序的读写效率,这样对于一个大型应中,数据量大的应用是一个非常适用的一个选择。
但使用切分不足的时一定要确定应用程序的规模和数据增长量,对于一般中小型应用不建议使用横向切分,因为横向切分会带来应用的开发成本和资源成本,当应用需要改变会增加维护成本,并且需要设定应用的存储和检索的特定逻辑

 

需要有朋友可以下载本例程的实例代码,由于上传文件大小有限制,所以本人只上传了相应的源代码,相应的开发工具包需要读者自身下载并导入。本例程的代码本人都经过测试并成功运行,如有问题可加:63267818 群进行讨论


本例程参考文摘:
Hibernate Shards:http://docs.jboss.org/hibernate/shards/3.0/reference/en/html_single/
Hibernate Shards API Documentation
http://docs.jboss.org/hibernate/stable/shards/api/

http://www.cnblogs.com/RicCC/archive/2010/04/14/hibernate-shards-3-architecture.html

 

 

淘宝男人频道

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值