<< 七月 2013 | 首页 | 九月 2013 >>

同时入选IMDB和豆瓣电影100强的经典电影

导演 : 弗兰克·德拉邦特 Frank Darabont 
主演 : 蒂姆·罗宾斯 Tim Robbins/摩根·弗里曼 Morgan Freeman/鲍勃·冈顿 Bob Gunton/威廉姆·赛德勒 William Sadler/克兰西·布朗 Clancy Brown

 

 

2009-05-19 添加2. 教父 The Godfather

导演 : 弗朗西斯·福特·科波拉 Francis Ford Coppola 
主演 : 马龙·白兰度 Marlon Brando/阿尔·帕西诺 Al Pacino/詹姆斯·凯恩 James Caan/罗伯特·杜瓦尔 Robert Duvall/黛安·基顿 Diane Keaton

 

 

2009-05-19 添加3. 教父2 The Godfather: Part Ⅱ

导演 : Francis Ford Coppola 
主演 : Al Pacino/Robert Duvall/Robert De Niro/Diane Keaton/John Cazale

 

 

2009-05-19 添加4. 低俗小说 Pulp Fiction

导演 : 昆汀·塔伦蒂诺 Quentin Tarantino 
主演 : 乌玛·瑟曼 Uma Thurman/塞缪尔·杰克逊 Samuel L. Jackson/约翰·特拉沃塔 John Travolta/蒂姆·罗斯 Tim Roth/布鲁斯·威利斯 Bruce Willis

 

 

导演 : Steven Spielberg 
主演 : Liam Neeson/Ben Kingsley/Ralph Fiennes/Caroline Goodall/Jonathan Sagall

 

 

导演 : 米洛斯·福尔曼 Miloš Forman 
主演 : 杰克·尼科尔森 Jack Nicholson/丹尼·德维托 Danny DeVito/克里斯托弗·洛伊德 Christopher Lloyd/路易丝·弗莱彻 Louise Fletcher/Ted Markland/布拉德·道里夫 Brad Dourif

 

 

2009-05-19 添加7. 七武士 七人の侍

导演 : 黑泽明 
主演 : 三船敏郎 Toshirô Mifune/志村乔 Takashi Shimura/稻叶义男 Yoshio Inaba/宫口精二 Seiji Miyaguchi/千秋实 Minoru Chiaki/加东大介 Daisuke Katô/木村功 Isao Kimura

 

 

导演 : 彼得·杰克逊 Peter Jackson 
主演 : 维果·莫腾森 Viggo Mortensen/伊利亚·伍德 Elijah Wood/西恩·奥斯汀 Sean Astin/丽芙·泰勒 Liv Tyler/伊恩·麦克莱恩 Ian McKellen/奥兰多·布鲁姆 Orlando Bloom/凯特·布兰切特 Cate Blanchett/米兰达·奥图 Miranda Otto/安迪·瑟金斯 Andy Serkis/雨果·维文 Hugo Weaving/多米尼克·莫纳汉 Dominic Monaghan/比利·博伊德 Billy Boyd/马尔顿·索克斯 Marton Csokas/卡尔·厄本 Karl Urban/克里斯托弗·李 Christopher Lee/约翰·瑞斯-戴维斯 John Rhys-Davies

 

 

2009-05-19 添加9. 上帝之城 Cidade de Deus

导演 : Fernando Meirelles/Kátia Lund 
主演 : Alexandre Rodrigues/Matheus Nachtergaele/Douglas Silva/Leandro Firmino/Jonathan Haagensen

 

 

导演 : 彼得·杰克逊 Peter Jackson 
主演 : 伊利亚·伍德 Elijah Wood/西恩·奥斯汀 Sean Astin/伊恩·麦克莱恩 Ian McKellen/维果·莫腾森 Viggo Mortensen/奥兰多·布鲁姆 Orlando Bloom/凯特·布兰切特 Cate Blanchett/肖恩·宾 Sean Bean/克里斯托弗·李 Christopher Lee/雨果·维文 Hugo Weaving/丽芙·泰勒 Liv Tyler/安迪·瑟金斯 Andy Serkis/伊安·霍姆 Ian Holm/多米尼克·莫纳汉 Dominic Monaghan/萨拉·贝克 Sala Baker/约翰·瑞斯-戴维斯 John Rhys-Davies

 

 

2009-05-19 添加11. 搏击俱乐部 Fight Club

导演 : 大卫·芬奇 David Fincher 
主演 : 爱德华·诺顿 Edward Norton/布拉德·皮特 Brad Pitt/海伦娜·邦汉·卡特 Helena Bonham Carter/杰瑞德·莱托 Jared Leto/艾恩·贝利 Eion Bailey

 

 

2009-05-19 添加12. 这个杀手不太冷 Léon

导演 : 吕克·贝松 Luc Besson 
主演 : 让·雷诺 Jean Reno/娜塔丽·波特曼 Natalie Portman/加里·奥德曼 Gary Oldman/丹尼·爱罗 Danny Aiello/麦温·勒·贝斯柯 Maïwenn Le Besco

 

 

2009-05-19 添加13. 美丽人生 La vita è bella

导演 : 罗伯托·贝尼尼 Roberto Benigni 
主演 : 罗伯托·贝尼尼 Roberto Benigni/尼可莱塔·布拉斯基 Nicoletta Braschi/乔治·坎塔里尼 Giorgio Cantarini/Giustino Durano/Sergio Bini Bustric/玛丽莎·佩雷德斯 Marisa Paredes/豪斯特·巴奇霍兹 Horst Buchholz/Lidia Alfonsi/Giuliana Lojodice

 

 

导演 : 朱塞佩·托纳多雷 Giuseppe Tornatore 
主演 : 萨瓦特利·卡西欧 Salvatore Cascio/Enzo Cannavale/马克·莱昂纳蒂 Marco Leonardi/菲利浦·诺瓦雷 Philippe Noiret/安东娜拉 塔莉 Antonella Attili/雅克·贝汉 Jacques Perrin

 

 

导演 : 宫崎骏 Hayao Miyazaki 
主演 : 柊瑠美 Rumi Hîragi/入野自由 Miyu Irino/夏木真理 Mari Natsuki/菅原文太 Bunta Sugawara/内藤刚志 Takashi Naitô/神木隆之介 Ryûnosuke Kamiki

 

 

2009-06-07 添加16. 钢琴家 The Pianist

导演 : Roman Polanski 
主演 : Adrien Brody/Thomas Kretschmann/Emilia Fox/Julia Rayner/Jessica Kate Meyer

 

 

导演 : 彼得·杰克逊 Peter Jackson 
主演 : 伊利亚·伍德 Elijah Wood/西恩·奥斯汀 Sean Astin/伊恩·麦克莱恩 Ian McKellen/维果·莫腾森 Viggo Mortensen/奥兰多·布鲁姆 Orlando Bloom/克里斯托弗·李 Christopher Lee/丽芙·泰勒 Liv Tyler/安迪·瑟金斯 Andy Serkis/雨果·维文 Hugo Weaving/卡尔·厄本 Karl Urban/凯特·布兰切特 Cate Blanchett/多米尼克·莫纳汉 Dominic Monaghan/大卫·文翰 David Wenham/比利·博伊德 Billy Boyd/布拉德·道里夫 Brad Dourif/伯纳德·希尔 Bernard Hill/约翰·瑞斯-戴维斯 John Rhys-Davies

 

 

导演 : Sergio Leone 
主演 : 罗伯特·德尼罗 Robert De Niro/詹姆斯·伍兹 James Woods/詹妮弗·康纳利 Jennifer Connelly/伊丽莎白·麦戈文 Elizabeth McGovern/乔·佩西 Joe Pesci/塔斯黛·韦尔德 Tuesday Weld/波特·杨 Burt Young/特里特·威廉斯 Treat Williams/丹尼·爱罗 Danny Aiello/Richard Bright

 

 

2009-06-07 添加19. 阿甘正传 Forrest Gump

导演 : Robert Zemeckis 
主演 : Tom Hanks/Robin Wright Penn/Gary Sinise/Mykelti Williamson/Sally Field/Michael Conner Humphreys/Haley Joel Osment

 

 

导演 : Jean-Pierre Jeunet 
主演 : 奥黛丽·塔图 Audrey Tautou/马修·卡索维茨 Mathieu Kassovitz/贾梅尔·杜布兹 Jamel Debbouze/Lorella Cravotta/多米尼克·皮诺 Dominique Pinon/Isabelle Nanty/友兰达·梦露 Yolande Moreau/Rufus/克莱尔·莫里耶 Claire Maurier/艾特·迪·彭吉云 Artus de Penguern/Maurice Bénichou/米切尔·罗宾 Michel Robin/Serge Merlin/Clotilde Mollet/Urbain Cancelier/克劳德·佩龙 Claude Perron

 

 

导演 : 达伦·阿伦诺夫斯基 Darren Aronofsky 
主演 : 艾伦·伯斯汀 Ellen Burstyn/杰瑞德·莱托 Jared Leto/詹妮弗·康纳利 Jennifer Connelly/马龙·韦恩斯 Marlon Wayans/Christopher McDonald

 

 

2010-02-21 添加22. 机器人总动员 WALL·E

导演 : 安德鲁·斯坦顿 Andrew Stanton 
主演 : 本·贝尔特 Ben Burtt/艾丽莎·奈特 Elissa Knight/杰夫·格尔林 Jeff Garlin/佛莱德·威拉特 Fred Willard

 

 

导演 : 弗洛里安·亨克尔·冯·多纳斯马 Florian Henckel von Donnersmarck 
主演 : 乌尔里希·穆埃 Ulrich Mühe/马蒂娜·戈黛特 Martina Gedeck/塞巴斯蒂安·考奇 Sebastian Koch/乌尔里奇·图克尔 Ulrich Tukur/托马斯·席尔梅 Thomas Thieme

 

 

2010-02-21 添加24. 飞屋环游记 Up

导演 : 彼特·道格特 Pete Docter 
主演 : 克里斯托弗·普卢默 Christopher Plummer/约翰·拉岑贝格 John Ratzenberger/爱德华·阿斯纳 Edward Asner/Paul Eiding/乔丹·长井 Jordan Nagai

 

 

2010-02-21 添加25. 雨中曲 Singin' in the Rain

导演 : Stanley Donen/Gene Kelly 
主演 : Gene Kelly/Donald O'Connor/Debbie Reynolds/Cyd Charisse/Jean Hagen

阅读全文……

标签 : ,

Dynamic DataSource Routing | SpringSource Team Blog

Anyways, here's the code for my Catalog:

package blog.datasource;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
 
public class Catalog extends SimpleJdbcDaoSupport {
 
   public List<Item> getItems() {
      String query = "select name, price from item";
      return getSimpleJdbcTemplate().query(query, new ParameterizedRowMapper<Item>() {
            public Item mapRow(ResultSet rs, int row) throws SQLException {
               String name = rs.getString(1);
               double price = rs.getDouble(2);
               return new Item(name, price);
            }
      });
   }
}

 

As you can see, the Catalog simply returns a list of Item objects. The Item just contains name and price properties:

package blog.datasource;
 
public class Item {
 
   private String name;
   private double price;
 
   public Item(String name, double price) {
      this.name = name;
      this.price = price;
   }
 
   public String getName() {
      return name;
   }
 
   public double getPrice() {
      return price;
   }
 
   public String toString() {
      return name + " (" + price + ")";
   }
 
}

 

Now, in order to demonstrate multiple DataSources, I created an enum for different Customer types (representing "levels" of membership I guess), and I created three different databases – so that each type of customer would get a distinct item list (I did mention that this would be a contrived example didn't I?). The important thing is that each of the databases are equivalent in terms of the schema. That way the Catalog's query will work against any of them – just returning different results. In this case, it's just the "item" table with 2 columns: name and price. And… here is the enum:

public enum CustomerType {
   BRONZE,
   SILVER,
   GOLD
}

 

It's time to create some bean definitions. Since I have 3 datasources where everything is the same except for the port number, I created a parent bean so that the shared properties can be inherited. Then, I added the 3 bean definitions to represent the per-CustomerType DataSources:

<bean id="parentDataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource"
         abstract="true">
   <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
   <property name="username" value="sa"/>
</bean>
 
<bean id="goldDataSource" parent="parentDataSource">
   <property name="url" value="jdbc:hsqldb:hsql://localhost:${db.port.gold}/blog"/>
</bean>
 
<bean id="silverDataSource" parent="parentDataSource">
   <property name="url" value="jdbc:hsqldb:hsql://localhost:${db.port.silver}/blog"/>
</bean>
 
<bean id="bronzeDataSource" parent="parentDataSource">
   <property name="url" value="jdbc:hsqldb:hsql://localhost:${db.port.bronze}/blog"/>
</bean>
 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="classpath:/blog/datasource/db.properties"/>
</bean>

 

Notice that I added a PropertyPlaceholderConfigurer so that I could externalize the port numbers in a "db.properties" file, like so:

db.port.gold=9001
db.port.silver=9002
db.port.bronze=9003

 

Now things start to get interesting. I need to supply the "routing" DataSource to my Catalogso that it can dynamically get connections from the 3 different databases at runtime based on the current customer's type. As I mentioned, the AbstractRoutingDataSource can be rather simple to implement. Here is my implementation:

package blog.datasource;
 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 
public class CustomerRoutingDataSource extends AbstractRoutingDataSource {
 
   @Override
   protected Object determineCurrentLookupKey() {
      return CustomerContextHolder.getCustomerType();
   }
}

…and the CustomerContextHolder simply provides access to a thread-bound CustomerType. In reality, the 'context' would likely hold more information about the customer. Also note that if you are using Acegi, then you could retrieve some information from the userDetails. For this example, it's just the customer "type":

public class CustomerContextHolder {
 
   private static final ThreadLocal<CustomerType> contextHolder =
            new ThreadLocal<CustomerType>();
 
   public static void setCustomerType(CustomerType customerType) {
      Assert.notNull(customerType, "customerType cannot be null");
      contextHolder.set(customerType);
   }
 
   public static CustomerType getCustomerType() {
      return (CustomerType) contextHolder.get();
   }
 
   public static void clearCustomerType() {
      contextHolder.remove();
   }
}

 

Finally, I just need to configure the catalog and routing DataSource beans. As you can see, the "real" DataSource references are provided in a Map. If you provide Strings, they can be resolved as JNDI names (or any custom resolution strategy can be provided – see the JavaDoc). Also, I've simply set the 'bronzeDataSource' as the default:

<bean id="catalog" class="blog.datasource.Catalog">
   <property name="dataSource" ref="dataSource"/>
</bean>
 
<bean id="dataSource" class="blog.datasource.CustomerRoutingDataSource">
   <property name="targetDataSources">
      <map key-type="blog.datasource.CustomerType">
         <entry key="GOLD" value-ref="goldDataSource"/>
         <entry key="SILVER" value-ref="silverDataSource"/>
      </map>
   </property>
   <property name="defaultTargetDataSource" ref="bronzeDataSource"/>
</bean>

 

Of course I'd like to see this working, so I've created a simple test (extending one of Spring's integration test support classes). I added 3 items to the "gold" database, 2 items to the "silver" database, and only 1 item to the "bronze" database. This is the test:

public class CatalogTests extends AbstractDependencyInjectionSpringContextTests {
 
   private Catalog catalog;
 
   public void setCatalog(Catalog catalog) {
      this.catalog = catalog;
   }
 
   public void testDataSourceRouting() {
      CustomerContextHolder.setCustomerType(CustomerType.GOLD);
      List<Item> goldItems = catalog.getItems();
      assertEquals(3, goldItems.size());
      System.out.println("gold items: " + goldItems);
 
      CustomerContextHolder.setCustomerType(CustomerType.SILVER);
      List<Item> silverItems = catalog.getItems();
      assertEquals(2, silverItems.size());
      System.out.println("silver items: " + silverItems);
 
      CustomerContextHolder.clearCustomerType();
      List<Item> bronzeItems = catalog.getItems();
      assertEquals(1, bronzeItems.size());
      System.out.println("bronze items: " + bronzeItems);
   }
 
   protected String[] getConfigLocations() {
      return new String[] {"/blog/datasource/beans.xml"};
   }
}

…and rather than simply taking a screenshot of the green bar, you'll notice I've provided some console output – the results!:

gold items: [gold item #1 (250.0), gold item #2 (325.45), gold item #3 (55.6)]
silver items: [silver item #1 (25.0), silver item #2 (15.3)]
bronze items: [bronze item #1 (23.75)]

 

As you can see, the configuration is simple. Better still, the data-access code is not concerned with looking up different DataSources. For more information, consult the JavaDoc for AbstractRoutingDataSource.

阅读全文……

标签 : ,

动态切换数据源(spring+hibernate) - liheping - 博客园

思路:动态切换数据源确切的来说是在同一类型数据库的情况下的。意思就是说 , 在系统中的使用的数据库分布在多台数据库服务器或者在同台服务器上的多个数据库. 在运行时期间根据某种标识符来动态的选择当前操作的数据库.
     1.数据源是相同类型的数据库: 一个SessionFactory+动态数据源+一个事务管理器
     2.数据源是不同类型的数据库: 根据类型 配置多套SessionFactory
 
模拟:两个mysql数据源+一个Access数据源

实现

1.切换数据源需要标识符,标识符是Object类型
package lhp.example.context;
public enum DBType {
dataSource1, dataSource2;
}

2.然后创建一个用于切换数据源(设置或者获得上下文)的工具类
复制代码
package lhp.example.context;

public class ContextHolder {
private static final ThreadLocal<Object> holder = new ThreadLocal<Object>();

public static void setDbType(DBType dbType) {
holder.set(dbType);
}

public static DBType getDbType() {
return (DBType) holder.get();
}

public static void clearDbType() {
holder.remove();
}
}
复制代码

3.创建动态数据源类,继承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource这个类.
复制代码
package lhp.example.context;

import java.util.logging.Logger;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {
public static final Logger logger = Logger.getLogger(DynamicDataSource.class.toString());

@Override
protected Object determineCurrentLookupKey() {
DBType key = ContextHolder.getDbType();//获得当前数据源标识符
//logger.info("当前数据源 :" + key);
return key;
}

}
复制代码

4.然后配置多个数据源
复制代码
<!-- 数据源1 : mysql -->
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/dec" />
<property name="user" value="root" />
<property name="password" value="" />
</bean>
<!-- 数据源2 : mysql -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/lms" />
<property name="user" value="root" />
<property name="password" value="" />
</bean>

<!-- 数据源3 : access -->
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="sun.jdbc.odbc.JdbcOdbcDriver" />
<property name="jdbcUrl" value="jdbc:odbc:accessTest" />
<property name="user" value="administrator" />
<property name="password" value="XLZX0309" />
</bean>

<!-- mysql 动态数据源设置-->
<bean id="mysqlDynamicDataSource" class="lhp.example.context.DynamicDataSource">
<property name="targetDataSources">
<!-- 标识符类型 -->
<map key-type="lhp.example.context.DBType">
<entry key="dataSource1" value-ref="dataSource1" />
<entry key="dataSource2" value-ref="dataSource2" />
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource1" />
</bean>
复制代码

5.配置sessionFactory
复制代码
<!-- mysql sessionFactory -->
<bean id="mysqlSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mysqlDynamicDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!--create validate -->
<prop key="hibernate.query.substitutions">true 1, false 0</prop>
</props>
</property>
</bean>

<!-- access sessionFactory -->
<bean id="aceessSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource3" />
<property name="hibernateProperties">
<props>
<!-- access 语法和MSSQL相似 所以用的MSSQL方言,或者可以使用第三方方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!--create validate -->
<prop key="hibernate.query.substitutions">true 1, false 0</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<!-- <prop key="hibernate.cache.use_second_level_cache">true</prop> -->
<!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
<!-- <prop key="hibernate.cache.use_query_cache">true</prop> -->
<!-- <prop key="hibernate.generate_statistics">true</prop> -->
<!-- <prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop> -->
</props>
</property>
</bean>
复制代码

6.测试用例
复制代码
package lhp.example.junit;

import static org.junit.Assert.*;
import java.sql.DatabaseMetaData;
import lhp.example.context.ContextHolder;
import lhp.example.context.DBType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServiceTest {
private ApplicationContext context;
//三个数据源的URL
private String dataSource1_URL = "jdbc:mysql://127.0.0.1:3306/dec";
private String dataSource2_URL = "jdbc:mysql://127.0.0.1:3306/lms";
private String dataSource3_URL = "jdbc:odbc:accessTest";
private SessionFactory mysqlSessionFactory;
private SessionFactory aceessSessionFactory;

@Before
public void setUp() throws Exception {
// 选择数据源初始化spring
ContextHolder.setDbType(DBType.dataSource1);
//
String[] xmlFiles = new String[] {
"applicationContext-dataSource.xml",
"applicationContext-hibernate.xml",
"applicationContext-spring.xml" };
//
context = new ClassPathXmlApplicationContext(xmlFiles);
//
mysqlSessionFactory = (SessionFactory) context.getBean("mysqlSessionFactory");
aceessSessionFactory = (SessionFactory) context.getBean("aceessSessionFactory");
}

@SuppressWarnings("deprecation")
@Test
public void mysqlDataSourceTest() {
try {

Session mysqlSession = mysqlSessionFactory.openSession();
// 获得数据库元数据
DatabaseMetaData meatData = mysqlSession.connection().getMetaData();

// 默认启动数据源 dataSource1
//断言当前数据源URL是否是dataSource1的URL
assertEquals(dataSource1_URL, meatData.getURL());

// 切换到数据源 dataSource2
ContextHolder.setDbType(DBType.dataSource2);
mysqlSession = mysqlSessionFactory.openSession();
meatData = mysqlSession.connection().getMetaData();
//断言当前数据源URL是否是dataSource2的URL
assertEquals(dataSource2_URL, meatData.getURL());

} catch (Exception e) {
e.printStackTrace();
}
}

@SuppressWarnings("deprecation")
@Test
public void accessDataSourceTest() {
try {
Session accessSession = aceessSessionFactory.openSession();
// 获得数据库元数据
DatabaseMetaData meatData = accessSession.connection().getMetaData();
//断言当前数据源URL是否是dataSource3的URL
assertEquals(dataSource3_URL, meatData.getURL());


} catch (Exception e) {
e.printStackTrace();
}
}

}
复制代码

阅读全文……

标签 : ,

Mahout学习笔记-分类算法之Decision Forest - 潘的博客 - 博客园

根据最近的Mahout Wiki,Mahout实现的分类算法有:随机梯度下降(SGD),贝叶斯分类,Online Passive Aggressive,HMM和决策森林(随机森林)。随机梯度下降当前不能并行处理,贝叶斯分类更适合处理文本数据,所以这两个算法都不太适合我的应用场景(并行处理,特征类型为数字),OPA和HMM不太熟悉,所以就选用了决策森林(随机森林)。

  决策森林,顾名思义,就是由多个决策树组成森林,然后用这个森林进行分类,非常适合用MapReduce实现,进行并行处理。决策森林又称为随机森林,这是因为不同于常规的决策树(ID3,C4.5),决策森林中每个每棵树的每个节点在选择该点的分类特征时并不是从所有的输入特征里选择一个最好的,而是从所有的M个输入特征里随机的选择m个特征,然后从这m个特征里选择一个最好的(这样比较适合那种输入特征数量特别多的应用场景,在输入特征数量不多的情况下,我们可以取m=M)。然后针对目标特征类型的不同,取多个决策树的平均值(目标特征类型为数字类型(numeric))或大多数投票(目标特征类型为类别(category))。

  在Mahout的example中有一个Decision Tree的例子,可以直接在命令行运行:

  1. 准备数据:

  数据为Breiman提供的glass:http://archive.ics.uci.edu/ml/datasets/Glass+Identification

  2. 生成数据的说明文件:

  在Mahout目录下执行:bin/mahout org.apache.mahout.df.tools.Describe -p testdata/glass.data -f testdata/glass.info -d I 9 N L

  数据格式为CSV,最后的I 9 N L说明各特征的属性:

  I表示忽略第一个特征值(该特征值一般用来标示每一条训练样例,亦即可以作为ID)。

  9 N表示接下来的9个特征是输入特征,类型为数字类型。

  L 表示该特征是目标特征,亦即Label。

  以glass文件的前几行为例为例:

1,1.52101,13.64,4.49,1.10,71.78,0.06,8.75,0.00,0.00,1
2,1.51761,13.89,3.60,1.36,72.73,0.48,7.83,0.00,0.00,1
3,1.51618,13.53,3.55,1.54,72.99,0.39,7.78,0.00,0.00,1
4,1.51766,13.21,3.69,1.29,72.61,0.57,8.22,0.00,0.00,1
5,1.51742,13.27,3.62,1.24,73.08,0.55,8.07,0.00,0.00,1

  第一个特征被忽略,因为这个特征是作为ID用来表示每个样例的,2-10是9个输入特征,用来训练分类器,类型为数字(Numeric),最后一个特征是目标特征,代表每个样例所属的类别,这里所有样例都属于"1"类。

  3. 进行分类和测试

  在Mahout目录下执行:bin/mahout org.apache.mahout.df.BreimanExample -d testdata/glass.data -ds testdata/glass.info -i 10 -t 100

  -i表示迭代的次数

  -t表示每棵决策树的节点的个数

  BreimanExample默认会构造两个森林,一个取m=1,一个取m=log(M+1)。之所以这么做是为了说明即使m值很小,整个森林的分类结果也会挺好。

阅读全文……

推荐引擎:使用Mahout协同过滤

Mahout is a collection of machine learning algorithms intended to perform the following operations as recommendation (Collaborative Filtering), Clustering and Classification. Initially to implement recommendation we need an input data file where every line contains one record each. Each record should have the user ID, Item ID and preference value in order separated by comma.
 
Input File – input.txt
501,1002,5
501,1012,3
510,1002,2
515,1002,5
501,1020,1
                The point to be considered here that we need the User Id and Item ID to be integers, alpha numeric characters won’t serve our purpose. Also the larger the input files better the quality of recommendations produced
 
Recommenders
                Recommenders are broadly classified into two categories based on the method or approach they use in generating recommendations
1.       User Based Recommendations
                Recommendations are derived from how similar items are to items, ie based on the items a user has already more similar items are recommended
 
2.       Item Based Recommendations
                Recommendations are derived on how similar users to users are. ie to make recommendations for a user(User1) we take into account an user/users who shares similar tastes and based on the items they possess we recommend items to User1
 
 
                When we make mahout recommendations the key components involved are
Data Model
                It is an encapsulation used by Mahout to hold input data. It helps efficient access to data by various recommender algorithms.
Similarity Algorithm
                There are various kind of Similarity algorithms available and mahout has implementations of all the popular ones like Person Correlation, Cosine Measure, Euclidean Distance, Log Likelihood, Tanimoto coefficient etc
User Neighborhood
                This is applicable for user based recommendations, user based recommendations are made based on user to user similarity. We form a neighborhood of most similar users that share almost same tastes so that we get better recommendations. And the algorithms thet we use to select user neighborhood are
1.       Nearest N User Neighborhood
                Here we specify the neighborhood size, ie exactly the number of most similar uses to be considered for generating recommendations say 100,500 etc
2.       Threshold User Neighborhood
                We don’t specify the neighborhood size, rather we specify a similarity measure which is a value between -1 and +1. If we specify a value .7 then only the users that share a similarity greater than ).7 would be considered in neighborhood. Higher the value more similar the users are
Recommender
                It is the final computing object which couples together the datamodel, similarity algorithm and neighborhood to generate recommendations based on the same
 
                Samples code snippets to generate user and item based recommendations are given below
 
User Based Recommender
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
importorg.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
importorg.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
importorg.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
 
public class UserRecommender {
     
      public static void main(String args[])
      {
            // specifying the user id to which the recommendations have to be generated for
            int userId=510;
           
            //specifying the number of recommendations to be generated
            int noOfRecommendations=5;
           
            try
            {
                  // Data model created to accept the input file
                  FileDataModel dataModel = new FileDataModel(newFile("D://input.txt"));
                 
                  /*Specifies the Similarity algorithm*/
                  UserSimilarity userSimilarity = newPearsonCorrelationSimilarity(dataModel);
                 
                  /*NearestNUserNeighborhood is preferred in situations where we need to have control on the exact no of neighbors*/
                  UserNeighborhood neighborhood =newNearestNUserNeighborhood(100, userSimilarity, dataModel);
                 
                  /*Initalizing the recommender */
                  Recommender recommender =newGenericUserBasedRecommender(dataModel, neighborhood, userSimilarity);
                 
                  //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                  //
                  for (RecommendedItem recommendedItem : recommendations)
                        System.out.println(recommendedItem.getItemID());
            }
            catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TasteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
                 
      }
 
}
 
 
Item Based Recommender
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
importorg.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
importorg.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
importorg.apache.mahout.cf.taste.recommender.ItemBasedRecommender;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
 
public class ItemRecommender {
     
      public static void main(String args[])
      {
            // specifying the user id to which the recommendations have to be generated for
            int userId=510;
           
            //specifying the number of recommendations to be generated
            int noOfRecommendations=5;
           
            try
            {
                  // Data model created to accept the input file
                  FileDataModel dataModel = new FileDataModel(newFile("D://input.txt"));
                 
                  /*Specifies the Similarity algorithm*/
                  ItemSimilarity itemSimilarity = newPearsonCorrelationSimilarity(dataModel);
                 
                  /*Initalizing the recommender */
                  ItemBasedRecommender recommender =newGenericItemBasedRecommender(dataModel, itemSimilarity);
                 
                  //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                  //
                  for (RecommendedItem recommendedItem : recommendations)
                        System.out.println(recommendedItem.getItemID());
            }
            catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TasteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
      }
}
 
Note: To get some recommendations you a sufficiently large input file. A few lines of input won’t gain you any recommendations

 

参考:基于 Apache Mahout 构建社会化推荐引擎:http://www.ibm.com/developerworks/cn/java/j-lo-mahout/

使用Mahout为布尔型数据生成推荐内容

Generating Recommendations with mahout for Boolean data sets (data sets with no preference value)

关于在Spark MLlib中基于布尔型数据集推荐可参考:Spark MLlib中的协同过滤

通过指定alpha:是一个针对于隐性反馈 ALS 版本的参数,这个参数决定了偏好行为强度的基准。

val alpha = 0.01

val model = ALS.trainImplicit(ratings, rank, numIterations, 0.01, alpha)

 

参考:http://spark.apache.org/docs/latest/mllib-collaborative-filtering.html

 

Boolean data Sets
                Input data set that doesn’t have a preference value, ie input data set would be of the format UserId1,ItemId1
UserId2,ItemId2
Here it’d based on some data where an user either likes an item or he doesn’t, there is no preference value associated with this.
 
                When we use Boolean data sets we need to appropriately choose the Similarity algorithms and Recommenders
 
Similarity Algorithms
                For Boolean data sets we can either go in for Tanimoto Coefficient Similarity or Log Likelihood Similarity
 
Recommender
                We need to use Generic Boolean Pref User Based Recommender or Generic Boolean Pref Item Based Recommender
 
                Sample codes for generating User based and Item Based recommendations are given below
 
Used Based Recommender for Boolean Data Sets
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericBooleanPrefUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
 
public class UserRecommender {
     
      public static void main(String args[])
      {
            // specifying the user id to which the recommendations have to be generated for
            int userId=510;
           
            //specifying the number of recommendations to be generated
            int noOfRecommendations=5;
           
            //specifying theNeighborhood size
            double thresholdValue=0.7;
           
            try
            {
                  // Data model created to accept the input file
                  FileDataModel dataModel = new FileDataModel(newFile("D://input.txt"));
                 
                  /*TanimotoCoefficientSimilarity is intended for "binary" data sets
                  where a user either expresses a generic "yes" preference for an item or has no preference.*/
                  UserSimilarity userSimilarity = new TanimotoCoefficientSimilarity(dataModel);
                 
                  /*ThresholdUserNeighborhood is preferred in situations where we go in for a
                   similarity measure between neighbors and not any number*/
                  UserNeighborhood neighborhood =new ThresholdUserNeighborhood(thresholdValue, userSimilarity, dataModel);
                 
                  /*GenericBooleanPrefUserBasedRecommender is appropriate for use when no notion
                  of preference value exists in the data. */
                  Recommender recommender =new GenericBooleanPrefUserBasedRecommender(dataModel, neighborhood, userSimilarity);
                 
                  //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                  //
                  for (RecommendedItem recommendedItem : recommendations)
                        System.out.println(recommendedItem.getItemID());
            }
            catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TasteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
                 
      }
 
}
 
Item Based Recommender for Boolean Data Sets
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.recommender.ItemBasedRecommender;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
 
public class ItemRecommender {
     
      public static void main(String args[])
      {
            // specifying the user id to which the recommendations have to be generated for
            int userId=510;
           
            //specifying the number of recommendations to be generated
            int noOfRecommendations=5;
           
            try
            {
                  // Data model created to accept the input file
                  FileDataModel dataModel = new FileDataModel(newFile("D://input.txt"));
                 
                  /*Specifies the Similarity algorithm*/
                  ItemSimilarity itemSimilarity = new LogLikelihoodSimilarity(dataModel);
                 
                  /*Initalizing the recommender */
                  ItemBasedRecommender recommender =new GenericItemBasedRecommender(dataModel, itemSimilarity);
                 
                  //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                  //
                  for (RecommendedItem recommendedItem : recommendations)
                        System.out.println(recommendedItem.getItemID());
            }
            catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TasteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
      }
}

 

Craig Andrews » Best way to use HttpClient in Android

Google不鼓励使用HttpClient,更喜欢使用HttpURLConnection和HttpsURLConnection。

Many Android applications access the Internet resources over HTTP (and my projects are no exception). There are 2 common ways to do that: use Apache HttpClient 4.x (which is included in Android) or use HttpURLConnection (from Java). Google stated in a September 29, 2011 blog post that they prefer you use HttpURLConnection, but many apps and a large number of Java libraries already use HttpClient and won’t be changing soon (if ever). So HttpClient is here to stay.

With that in mind, the performance and footprint of HttpClient can vary widely based on how its set up. Here are my recommendations:

  • Always use one HttpClient instance for your entire application. HttpClient is not free to instantiate – each additional instance takes time to create and uses more memory. However, more importantly, using one instance allows HttpClient to pool and reuse connections along with other optimizations that can make big differences in how your application performs.
  • Use a thread safe connection manager. If you’re using one global HttpClient, it will be accessed by multiple threads concurrently – so if you don’t use a thread safe connection manager, Bad Things will happen.
  • Use Android’s android.net.SSLCertificateSocketFactory and android.net.SSLSessionCacheif they’re available. Using these instead of the base HttpClient SSLSocketFactorywill reduce round trips when connecting to the same https site multiple times, making your application feel faster.
  • Set the user agent to something useful. That way, the server’s logs will be far more useful, which may save you (or someone else) a lot of time later if (when) a problem occurs.

With all that said, here’s how I get my global HttpClient instance. This code should work on all Android versions (it should even work all the way back to 1.0 – if anyone cares). I use Google Guice‘s Provider interface and injection to get the application, but you can easily adopt this to a form that doesn’t use Guice.

阅读全文……

标签 : ,

[HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种) - 追竹 - 博客频道 - CSDN.NET

一、在调用PostMethod方法时设置字符编码:

 

  1. PostMethod postMethod = new PostMethod(  
  2.         "http://127.0.0.1:8080/HttpClientServer/login.do");  
  3. postMethod.getParams(). setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");  

 

 

 

二、重载PostMethod的getRequestCharSet()方法, 返回我们需要的编码(字符集)名称, 就可以解决 UTF-8 或者其它非默认编码提交 POST 请求时的乱码问题了.

 

 

  1. //Inner class for UTF-8 support     
  2.    public static class UTF8PostMethod extends PostMethod{     
  3.        public UTF8PostMethod(String url){     
  4.            super(url);     
  5.        }     
  6.        @Override    
  7.        public String getRequestCharSet() {     
  8.            //return super.getRequestCharSet();     
  9.            return "utf-8";     
  10.        }     
  11.    }       

 

 

  1. PostMethod postMethod = new UTF8PostMethod(  
  2.                 "http://127.0.0.1:8080/HttpClientServer/login.do");  

 

 

三、最后服务器端要配合客户端,设置下编码字符集:

 

  1. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  2.         throws ServletException, IOException {  
  3.     //解决中文乱码问题,此步不可少  
  4.     request.setCharacterEncoding("UTF-8");  
  5.     response.setContentType("text/html");  
  6.     response.setCharacterEncoding("UTF-8");  
  7.     PrintWriter out = response.getWriter();  
  8.     String username = (String) request.getParameter("username");  
  9.     String password = (String) request.getParameter("password");  
  10.     System.out.println("******************** doPost被执行了 ********************");  
  11.     System.out.println("您的请求参数为:/tusername:" + username + "/tpassword:"  
  12.             + password);  
  13.     out.print("您的请求参数为:/tusername:" + username + "/tpassword:" + password);  
  14.     out.flush();  
  15.     out.close();  
  16. }  

 

 

阅读全文……

也说TOMCAT中文乱码问题 request的setCharacterEncoding必须在getParameter之前执行_深圳漂者_百度空间

setCharacterEncoding
public void setCharacterEncoding(java.lang.String env) throws java.io.UnsupportedEncodingException
Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().

 

request的setCharacterEncoding必须在getParameter之前执行,也就是说传说中的GBK编码下Ajax乱码问题,可以使用Servlet Filter避免乱码,在Filter里通过判断是否是Ajax请求来通过request.setCharacterEncoding设置相应的编码。

 

应用程序在Servlet Filter 或Controller程序中通过setCharacterEncoding设置先后两次不同的字符集如setCharacterEncoding("UTF-8")和setCharacterEncoding("GBK"),在不同的应用服务器下会有不同的效果,在Tomcat是以第一次设置的字符集为准,该案例中是以"UTF-8"为准;而Weblogic则以最后一次为准,以"GBK"为准。

 

阅读全文……

标签 :

Java性能调优工具BEA JRockit Mission Control - kakatian1 - 51CTO技术博客

WebLogic使用

修改commEnv.cmd为-Xmanagement -Dcom.sun.management.jmxremote.port=7091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false 
 

运行rmi的机器如果有多个IP就会有这个问题。也可以通过设置java.rmi.server.hostname=本机IP(非127.0.0.1)来解决。
我使用的方法是在启动参数上加入 -Djava.rmi.server.hostname=10.5.31.56 
这样不用改hosts,也可以解决这个问题。

使用

启动代理开始连接

要使用JRockit Management Console来监控JRockit JVM,首先必须启动要监控的JVM中的管理代理。也就是说应用系统必须是用JRockit JDK进行启动的,并且启动了希望监控的JRockit的JMX管理代理服务。可以通过使用JRCMD或Ctrl-Break Handler来启动和关闭管理代理,最简单的方法是直接借助于-Xmanagement标志来启动它。如java -Xmanagement [your application]。但是通过JRCMD或Ctrl-Break Handler来进行管理更加方便与无侵入,连启动的命令都不用个性。有关JRCMD或Ctrl-Break Handler以及-Xmanagment选项的更多信息,请参见Management Console文档和Sun的JMX监控与管理的文档。

Java性能调优工具BEA  JRockit Mission Control图-3

阅读全文……

JRockit heap dump? | Oracle Forums

Problem: an application I look at experiences an OOM in production not yet reproduced anywhere else. 
Is there any way to get a heap dump in JRockit when OOM occurs similar to -XX:+HeapDumpOnOutOfMemoryError option in Sun VM?
Is there a way to do it through command line?

Try -Djrockit.oomdiagnostics=true. It will print a lot of information about the current state of the heap to stderr when an OutOfMemoryError occurs.

-Djrockit.oomdiagnostics.filename=<path to file>

阅读全文……

使用ffmpeg视频格式转换、视频截图、视频采集、屏幕录制

ffmpeg非常强大,轻松几条命令就可以完成你的工作。

把darkdoor.[001-100].jpg序列帧和001.mp3音频文件利用mpeg4编码方式合成视频文件darkdoor.avi:
$ ffmpeg -i 001.mp3 -i darkdoor.%3d.jpg -s 1024x768 -author skypp -vcodec mpeg4 darkdoor.avi

ffmpeg还支持mov格式:
$ ffmpeg -i darkdoor.%3d.jpg darkdoor.mov

要查看你的ffmpeg支持哪些格式,可以用如下命令:
$ ffmpeg -formats | less

还可以把视频文件导出成jpg序列帧:
$ ffmpeg -i bc-cinematic-en.avi example.%d.jpg

debian下安装ffmpeg很简单:
#apt-get install ffmpeg

 

视频文件截图

  • 截取一张352x240尺寸大小的,格式为jpg的图片
ffmpeg -i test.asf -y -f image2 -t 0.001 -s 352x240 a.jpg
  • 把视频的前30帧转换成一个Animated Gif
ffmpeg -i test.asf -vframes 30 -y -f gif a.gif
  • 截取指定时间的缩微图
ffmpeg -i test.avi -y -f image2 -ss 8 -t 0.001 -s 350x240 test.jpg
-ss后跟的时间单位为秒
  • 转换文件为3GP格式
ffmpeg -y -i test.mpeg -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 
-acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp
ffmpeg -y -i test.wmv -ac 1 -acodec libamr_nb -ar 8000 -ab 12200 -s 176x144 -b 128 -r 15 test.3gp

视频格式转换

  • 如何使用 ffmpeg 编码得到高质量的视频
ffmpeg.exe -i "D:\Video\Fearless\Fearless.avi" -target film-dvd -s 720x352
-padtop 64 -padbottom 64 -maxrate 7350000 -b 3700000 -sc_threshold 1000000000
-trellis -cgop -g 12 -bf 2 -qblur 0.3 -qcomp 0.7 -me full -dc 10 -mbd 2
-aspect 16:9 -pass 2 -passlogfile "D:\Video\ffmpegencode" -an -f mpeg2video "D:\Fearless.m2v"
  • 转换指定格式文件到FLV格式
ffmpeg.exe -i test.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv 
ffmpeg.exe -i test.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
  • 转码解密的VOB
ffmpeg -i snatch_1.vob -f avi -vcodec mpeg4 -b 800 -g 300 -bf 2 -acodec mp3 -ab 128 snatch.avi
上面的命令行将vob的文件转化成avi文件,mpeg4的视频和mp3的音频。注意命令中使用了B帧,所以mpeg4流是divx5兼容的。GOP大小是300意味着29.97帧频下每10秒就有INTRA帧。该映射在音频语言的DVD转码时候尤其有用。
  • 同时编码到几种格式并且在输入流和输出流之间建立映射
ffmpeg -i /tmp/a.wav -ab 64 /tmp/a.mp2 -ab 128 /tmp/b.mp2 -map 0:0 -map 0:0
上面的命令行转换一个64Kbits 的a.wav到128kbits的a.mp2 ‘-map file:index’在输出流的顺序上定义了哪一路输入流是用于每一个输出流的。
  • 转换文件为3GP格式
ffmpeg -i test.avi -y -b 20 -s sqcif -r 10 -acodec amr_wb -ab 23.85 -ac 1 -ar 16000 test.3gp
注:如果要转换为3GP格式,则ffmpeg在编译时必须加上–enable-amr_nb –enable-amr_wb,详细内容可参考:转换视频为3GPP格式

屏幕录制

  • 使用ffmpeg录像屏幕
ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -vd x11:0,0 -s 1024x768 ~/test.avi

:其中,-vd x11:0,0 指录制所使用的偏移为 x=0 和 y=0,-s 1024×768 指录制视频的大小为 1024×768。录制的视频文件为 test.avi,将保存到用户主目录中

如果你只想录制一个应用程序窗口或者桌面上的一个固定区域,那么可以指定偏移位置和区域大小。使用xwininfo -frame命令可以完成查找上述参数。
  • 重新调整视频尺寸大小
ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -i ~/test.avi -s 800×600 ~/test-800-600.avi
注:ffmpeg的屏幕录制功能只能在Linux环境下有效。

视频采集

  • 把摄像头的实时视频录制下来,存储为文件
ffmpeg  -f video4linux -s 320*240 -r 10 -i /dev/video0  test.asf
三、ffmepg使用

各种转换例子见:
FFmpeg Howto
程序下载:
Unofficial FFmpeg Win32 Builds(需要使用代理访问)
或:
FFMPEG FULL SDK V3.0

The generic syntax is:

ffmpeg [[infile options][`-i' infile]]… {[outfile options] outfile}…

As a general rule, options are applied to the next specified file. Therefore, order is important, and you can have the same option on the command line multiple times. Each occurrence is then applied to the next input or output file.

* To set the video bitrate of the output file to 64kbit/s:

ffmpeg -i input.avi -b 64k output.avi

* To force the frame rate of the input and output file to 24 fps:

ffmpeg -r 24 -i input.avi output.avi

* To force the frame rate of the output file to 24 fps:

ffmpeg -i input.avi -r 24 output.avi

* To force the frame rate of input file to 1 fps and the output file to 24 fps:

ffmpeg -r 1 -i input.avi -r 24 output.avi

The format option may be needed for raw input files.

By default, FFmpeg tries to convert as losslessly as possible: It uses the same audio and video parameters for the outputs as the one specified for the inputs.

转换文件为3GP格式
ffmpeg -y -i test.mpeg -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 -acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp

ffmpeg -y -i test.mpeg -ac 1 -acodec amr_nb -ar 8000 -s 176x144 -b 128 -r 15 test.3gp

转换指定格式文件到FLV格式

ffmpeg.exe -i test.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv 
ffmpeg.exe -i test.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
***************************************************************************
ffmpeg -i F:\tools\ffmpeg.rev10464\test.avi -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 F:\tools\ffmpeg.rev10464\test.flv
ffmpeg -i "F:\tools\ffmpeg.rev10464\test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"
 


字体变小 字体变大
ffmpeg.exe -i F:\闪客之家\闪客之歌.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\11.flv 
ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv 
使用-ss参数 作用(time_off set the start time offset),可以从指定时间点开始转换任务。如: 
转换文件格式的同时抓缩微图: 
ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 'test.jpg' 
对已有flv抓图: 
ffmpeg -i "test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 'test.jpg' 
-ss后跟的时间单位为秒 
Ffmpeg转换命令 
ffmpeg -y -i test.mpeg -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 -acodec aac -ac 2 -ar 22500 
-ab 24 -f 3gp test.3gp 
或者 
ffmpeg -y -i test.mpeg -ac 1 -acodec amr_nb -ar 8000 -s 176x144 -b 128 -r 15 test.3gp 


ffmpeg参数设定解说 
-bitexact 使用标准比特率 
-vcodec xvid 使用xvid压缩 
-s 320x240 指定分辨率 
-r 29.97 桢速率(可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97) 
画面部分,选其一 
-b <比特率> 指定压缩比特率,似乎ffmpeg是自动VBR的,指定了就大概是平均比特率,比如768,1500这样的 
就是原来默认项目中有的 
-qscale <数值> 以<数值>质量为基础的VBR,取值0.01-255,约小质量越好 
-qmin <数值> 设定最小质量,与-qmax(设定最大质量)共用,比如-qmin 10 -qmax 31 
-sameq 使用和源同样的质量 
声音部分 
-acodec aac 设定声音编码 
-ac <数值> 设定声道数,1就是单声道,2就是立体声,转换单声道的TVrip可以用1(节省一半容量),高品质 
的DVDrip就可以用2 
-ar <采样率> 设定声音采样率,PSP只认24000 
-ab <比特率> 设定声音比特率,前面-ac设为立体声时要以一半比特率来设置,比如192kbps的就设成96,转换 
君默认比特率都较小,要听到较高品质声音的话建议设到160kbps(80)以上 
-vol <百分比> 设定音量,某些DVDrip的AC3轨音量极小,转换时可以用这个提高音量,比如200就是原来的2倍 
这样,要得到一个高画质音质低容量的MP4的话,首先画面最好不要用固定比特率,而用VBR参数让程序自己去 
判断,而音质参数可以在原来的基础上提升一点,听起来要舒服很多,也不会太大(看情况调整 


例子:ffmpeg -y -i "1.avi" -title "Test" -vcodec xvid -s 368x208 -r 29.97 -b 1500 -acodec aac -ac 2 -ar 24000 -ab 128 -vol 200 -f psp -muxvb 768 "1.***" 

解释:以上命令可以在Dos命令行中输入,也可以创建到批处理文件中运行。不过,前提是:要在ffmpeg所在的目录中执行(转换君所在目录下面的cores子目录)。 
参数: 
-y(覆盖输出文件,即如果1.***文件已经存在的话,不经提示就覆盖掉了) 
-i "1.avi"(输入文件是和ffmpeg在同一目录下的1.avi文件,可以自己加路径,改名字) 
-title "Test"(在PSP中显示的影片的标题) 
-vcodec xvid(使用XVID编码压缩视频,不能改的) 
-s 368x208(输出的分辨率为368x208,注意片源一定要是16:9的不然会变形) 
-r 29.97(帧数,一般就用这个吧) 
-b 1500(视频数据流量,用-b xxxx的指令则使用固定码率,数字随便改,1500以上没效果;还可以用动态码率如:-qscale 4和-qscale 6,4的质量比6高) 
-acodec aac(音频编码用AAC) 
-ac 2(声道数1或2) 
-ar 24000(声音的采样频率,好像PSP只能支持24000Hz) 
-ab 128(音频数据流量,一般选择32、64、96、128) 
-vol 200(200%的音量,自己改) 
-f psp(输出psp专用格式) 
-muxvb 768(好像是给PSP机器识别的码率,一般选择384、512和768,我改成1500,PSP就说文件损坏了) 
"1.***"(输出文件名,也可以加路径改文件名) 

机器强劲的话,可以多开几个批处理文件,让它们并行处理。 
E:\ffmpeg.exe -i I:\1.wmv -b 360 -r 25 -s 320x240 -hq -deinterlace -ab 56 -ar 22050 -ac 1 D:\2.flv 
===========================================
ffmpeg.exe -i F:\闪客之家\闪客之歌.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\11.flv ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv 使用-ss参数 作用(time_off set the start time offset),可以从指定时间点开始转换任务。如:
转换文件格式的同时抓缩微图:
ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 'test.jpg'
对已有flv抓图:
ffmpeg -i "test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 'test.jpg'
-ss后跟的时间单位为秒 Ffmpeg转换命令
ffmpeg -y -i test.mpeg -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 -acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp
或者
ffmpeg -y -i test.mpeg -ac 1 -acodec amr_nb -ar 8000 -s 176x144 -b 128 -r 15 test.3gp ffmpeg参数设定解说 
-bitexact 使用标准比特率 
-vcodec xvid 使用xvid压缩 
-s 320x240 指定分辨率 
-r 29.97 桢速率(可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97) 


画面部分,选其一 
-b <比特率> 指定压缩比特率,似乎ffmpeg是自动VBR的,指定了就大概是平均比特率,比如768,1500这样的就是原来默认项目中有的 
-qscale <数值> 以<数值>质量为基础的VBR,取值0.01-255,约小质量越好 
-qmin <数值> 设定最小质量,与-qmax(设定最大质量)共用,比如-qmin 10 -qmax 31 
-sameq 使用和源同样的质量 声音部分 
-acodec aac 设定声音编码 
-ac <数值> 设定声道数,1就是单声道,2就是立体声,转换单声道的TVrip可以用1(节省一半容量),高品质的DVDrip就可以用2 
-ar <采样率> 设定声音采样率,PSP只认24000 
-ab <比特率> 设定声音比特率,前面-ac设为立体声时要以一半比特率来设置,比如192kbps的就设成96,转换君默认比特率都较小,要听到较高品质声音的话建议设到160kbps(80)以上 
-vol <百分比> 设定音量,某些DVDrip的AC3轨音量极小,转换时可以用这个提高音量,比如200就是原来的2倍 这样,要得到一个高画质音质低容量的MP4的话,首先画面最好不要用固定比特率,而用VBR参数让程序自己去判断,而音质参数可以在原来的基础上提升一点,听起来要舒服很多,也不会太大(看情况调整 例子:ffmpeg -y -i "1.avi" -title "Test" -vcodec xvid -s 368x208 -r 29.97 -b 1500 -acodec aac -ac 2 -ar 24000 -ab 128 -vol 200 -f psp -muxvb 768 "1.***"

解释:以上命令可以在Dos命令行中输入,也可以创建到批处理文件中运行。不过,前提是:要在ffmpeg所在的目录中执行(转换君所在目录下面的cores子目录)。
参数:
-y(覆盖输出文件,即如果1.***文件已经存在的话,不经提示就覆盖掉了)
-i "1.avi"(输入文件是和ffmpeg在同一目录下的1.avi文件,可以自己加路径,改名字)
-title "Test"(在PSP中显示的影片的标题)
-vcodec xvid(使用XVID编码压缩视频,不能改的)
-s 368x208(输出的分辨率为368x208,注意片源一定要是16:9的不然会变形)
-r 29.97(帧数,一般就用这个吧)
-b 1500(视频数据流量,用-b xxxx的指令则使用固定码率,数字随便改,1500以上没效果;还可以用动态码率如:-qscale 4和-qscale 6,4的质量比6高)
-acodec aac(音频编码用AAC)
-ac 2(声道数1或2)
-ar 24000(声音的采样频率,好像PSP只能支持24000Hz)
-ab 128(音频数据流量,一般选择32、64、96、128)
-vol 200(200%的音量,自己改)
-f psp(输出psp专用格式)
-muxvb 768(好像是给PSP机器识别的码率,一般选择384、512和768,我改成1500,PSP就说文件损坏了)
"1.***"(输出文件名,也可以加路径改文件名)

P.S. 版主机器强劲的话,可以多开几个批处理文件,让它们并行处理。 E:\ffmpeg.exe -i I:\1.wmv -b 360 -r 25 -s 320x240 -hq -deinterlace -ab 56 -ar 22050 -ac

 

gcc 4.6.0 源码安装

安装环境 

 

 

[root@vm11 ~]# lsb_release -a

LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64                                                                             :graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:prin                                                                             ting-4.0-noarch

Distributor ID: CentOS

Description:    CentOS release 5.6 (Final)

Release:        5.6

Codename:       Final

[root@vm11 ~]#

 
进入/usr/ 目录建立 build目录
cd /usr/
mkdir build
 

切换到浏览器.

在官方网站上选择gcc 4.6.0下载的ftp站点http://gcc.gnu.org/mirrors.html

我选择的是 http://mirrors-us.seosue.com/gcc/

 

 

[ICO]NameLast modifiedSizeDescription


[DIR] Parent Directory   -  
[   ] README 25-Apr-2003 01:09 765  
[DIR] infrastructure/ 01-Apr-2011 04:43 -  
[DIR] libstdc++/ 17-Dec-2007 16:00 -  
[   ] md5.sum 09-Mar-2010 16:00 41  
[DIR] old-releases/ 16-Jun-2001 17:00 -  
[DIR] releases/ 29-Jun-2011 04:34 -  
[DIR] snapshots/ 29-Jun-2011 04:34 -  
[DIR] summit/ 29-Jun-2011 04:34 -  

进入ftp的目录,选择releases->gcc-4.6.0

 

 

[   ] gcc-4.6.0.tar.bz2 25-Mar-2011 11:03 68M  
[   ] gcc-4.6.0.tar.gz 25-Mar-2011 11:04 89M

 

 

选择其中一个复制下载链接

 

 wget http://mirrors-us.seosue.com/gcc/releases/gcc-4.6.0/gcc-4.6.0.tar.bz2

 bunzip2 gcc-4.6.0.tar.bz2

 tar xf gcc-4.6.0.tar

 

编译源码安装gcc 4.6.0还需要一些依赖库

进入上面的ftp目录infrastructure/

分别 gmp mpc mpfr ppl cloog 安装包

wget http://mirrors-us.seosue.com/gcc/infrastructure/gmp-4.3.2.tar.bz2

wget http://mirrors-us.seosue.com/gcc/infrastructure/mpc-0.8.1.tar.gz

wget http://mirrors-us.seosue.com/gcc/infrastructure/mpfr-2.4.2.tar.bz2

wget http://mirrors-us.seosue.com/gcc/infrastructure/ppl-0.11.tar.gz

wget http://mirrors-us.seosue.com/gcc/infrastructure/cloog-0.16.2.tar.gz

 

如果cloog编译有问题可以在这里下载最新的http://gcc.gnu.org/wiki/Graphite_Build

 

bunzip2 gmp-4.3.2.tar.bz2

tar xf gmp-4.3.2.tar

 

gunzip mpc-0.8.1.tar.gz

tar xf mpc-0.8.1.tar

 

bunzip2 mpfr-2.4.2.tar.bz2

tar xf mpfr-2.4.2.tar

 

gunzip ppl-0.11.tar.gz

tar xf ppl-0.11.tar

 

gunzip cloog-0.16.2.tar.gz

tar xf cloog-0.16.2.tar

 

cd gmp-4.3.2

./configure --prefix=/usr

make && make install

最好指定安装目录,如果是默认是/usr/local/,有时候安装在local 时候,有可能出现在编译时候没有找到对应库

还有可以编辑/etc/ld.so.conf 文件添加/usr/local/目录,再次ldconfig

 

cd mpfr-2.4.2

./configure --prefix=/usr --with-gmp=/usr/

make && make install

 

cd mpc-0.8.1

./configure --prefix=/usr/ --with-gmp=/usr/ --with-mpfr=/usr/

make && make install

 

cd ppl-0.11

./configure --prefix=/usr/ --with-gmp=/usr/ 

make && make install

 

cd cloog-0.16.2

 

./configure --prefix=/usr/ --with-gmp=/usr/ --with-ppl=/usr/

make && make install

 

cd gcc.4.6.0

./configure --with-gmp=/usr/ --with-mpfr=/usr/ --with-mpc=/usr/ --with-cloog=/usr/ --enable-languages=c,c++ --enable-threads=posix --enable-__cxa_atexit --with-cpu=generic --disable-multilib --with-ppl=/usr/

make && make install

 

标签 : ,