Siena 数据持久层框架试用记

小编辑 发布于 2010/02/09 00:49
阅读 1K+
收藏 2

安装

http://siena.googlecode.com/svn/trunk/将 源码checkout出来,发现虽然是用maven管的项目,但是要编译起来还真不容易(如果准备直接从http://code.google.com/p/siena/downloads/list下载binary的同志可以直接跳过安装这步啦)

1. 首先去GAE下载Google App Engine的包:http://code.google.com/intl/zh-CN/appengine/downloads.html, 解压后将如下文件存为GAE解压目录下的mvn-install.bat:

call mvn install:install-file -Dfile=lib/appengine-tools-api.jar -DgroupId=com.google -DartifactId=appengine-tools -Dversion=1.2.0 -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/shared/appengine-local-runtime-shared.jar -DgroupId=com.google -DartifactId=appengine-local-runtime-shared -Dversion=1.2.0 -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/user/appengine-api-1.0-sdk-1.2.0.jar -DgroupId=com.google -DartifactId=appengine-sdk-1.2.0-api -Dversion=1.2.0 -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/user/orm/datanucleus-appengine-1.0.0.final.jar -DgroupId=org.datanucleus -DartifactId=datanucleus-appengine -Dversion=1.0.0.final -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/impl/appengine-local-runtime.jar -DgroupId=com.google -DartifactId=appengine-local-runtime -Dversion=1.2.0 -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/impl/appengine-api-stubs.jar -DgroupId=com.google -DartifactId=appengine-api-stubs -Dversion=1.2.0 -Dpackaging=jar -DgeneratePom=true

运行mvn-install.bat安装所有GAE依赖

2. 去Hadoop的HBase下载:http://hadoop.apache.org/hbase/releases.html, 我下载的是0.20.1,如果是其他版本注意修改bat文件的相应内容,解压后在解压目录下创建mvn-install.bat文件:

call mvn install:install-file -Dfile=hbase-0.20.1.jar -DpomFile=hbase.pom
call mvn install:install-file -Dfile=lib/hadoop-0.20.1-hdfs127-core.jar -DgroupId=org.apache -DartifactId=hadoop -Dversion=0.20.1 -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/libthrift-r771587.jar -DgroupId=com.facebook -DartifactId=thrift -Dversion=r771587 -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -Dfile=lib/commons-cli-2.0-SNAPSHOT.jar -DgroupId=commons-cli -DartifactId=commons-cli -Dversion=2.0-SNAPSHOT -Dpackaging=jar -DgeneratePom=true

然后将如下内容存为同目录的hbase.pom:

<?xml version="1.0" encoding="UTF-8"?>

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hbase</artifactId>
  <packaging>jar</packaging>
  <version>0.20.1</version>

  <name>Hadoop HBase</name>

  <dependencies>
    <dependency>
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
      <version>2.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2</version>
    </dependency>
    <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.0.4</version>
    </dependency>
    <dependency>
      <groupId>commons-math</groupId>
      <artifactId>commons-math</artifactId>
      <version>1.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache</groupId>
      <artifactId>hadoop</artifactId>
      <version>0.20.1</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.13</version>
    </dependency>

    <dependency>
      <groupId>jetty</groupId>
      <artifactId>org.mortbay.jetty</artifactId>
      <version>5.1.4</version>
    </dependency>
    <dependency>
      <groupId>jline</groupId>
      <artifactId>jline</artifactId>
      <version>0.9.91</version>
    </dependency>
    <dependency>
      <groupId>com.facebook</groupId>
      <artifactId>thrift</artifactId>
      <version>r771587</version>
    </dependency>
    <dependency>
      <groupId>org.apache.lucene</groupId>
      <artifactId>lucene-core</artifactId>
      <version>2.2.0</version>
    </dependency>
    <!--
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.15</version>
    </dependency>
    -->
    <dependency>
      <groupId>xmlenc</groupId>
      <artifactId>xmlenc</artifactId>
      <version>0.52</version>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-j2ee_1.4_spec</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

  </dependencies>

    <repositories>
        <repository>
            <id>virolab.cyfronet.pl</id>
            <name>virolab.cyfronet.pl (used for commons-cli-2.0)</name>
            <url>http://virolab.cyfronet.pl/maven2</url>
        </repository>
    </repositories>

</project>

然后执行mvn-install.bat完成HBase依赖安装

3. 修改siena项目中的pom.xml文件
将所有的GAE依赖版本由1.2.1改为1.2.0
将hbase版本改为 0.20.1

4. 在siena项目根目录执行mvn package -Dmaven.test.skip=true
成功即完 成,target/siena.jar即为我们所要
如果自己也是用maven,也可以直接安装到本地库

mvn source:jar install -Dmaven.test.skip=true 

试用

随便用maven建个项目,pom.xml中加入以下三个依赖:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.7</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>siena</groupId>
  <artifactId>siena</artifactId>
  <version>0.4</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
</dependency> 

然后创建如下类:

package com.baidu.siena.domain;

import siena.Generator;
import siena.Id;
import siena.Max;
import siena.Model;
import siena.Query;
import siena.Table;

@Table("user")
public class User extends Model {

    @Id(Generator.AUTO_INCREMENT)
    private Long id;
   
    @Max(32)
    private String username;
   
    @Max(32)
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
    public static Query<User> all() {
        return Model.all(User.class);
    }
   
}

 

同时在此类同包中(即com.baidu.siena.domain包中)创建一个siena.properties文件:

implementation = siena.jdbc.JdbcPersistenceManager
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost/gt?useUnicode=true&characterEncoding=UTF8
user = guolin
password = guolin

搭建即算完成,接下来写测试代码:

public class UserTest {

    @BeforeClass
    public static void beforeClass() throws Exception {
        DdlGenerator generator = new DdlGenerator();

        // 需要生成的类们
        generator.addTable(User.class);

        Database database = generator.getDatabase();
        Platform platform = PlatformFactory.createNewPlatformInstance("mysql");
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/gt", "guolin", "guolin");

        // 输出生成的SQL
        String sql = platform.getAlterTablesSql(connection, database);
        System.out.println(sql);

        // 执行SQL
        platform.alterTables(connection, database, true);

        connection.close();
    }

    @Test
    public void sample() {
        // 新建
        User user = new User();
        user.setName("");
        user.setUsername("fallen_lord");
        user.insert();
        Assert.assertNotNull(user.getId());

        // 批量取出
        List<User> users = User.all().filter("username", "fallen_lord").fetch();
        Assert.assertEquals(user.getId(), users.get(0).getId());

        // 修改
        user.setName("Lion");
        user.update();

        User result = User.all().filter("id", user.getId()).get();
        Assert.assertEquals("Lion", result.getName());

        // 删除
        for (User u : users) {
            u.delete();
        }
    }
}

运行mvn test,pass

加载中
返回顶部
顶部