package com.feng.spring.chapter2.helloworld;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class HelloTest {
@Test
public void helloWorld(){
ExpressionParser parser = new SpelExpressionParser();//创建解析器
Expression expression = parser.parseExpression("('Hello '+'World').concat(#end)");//解析表达式
EvaluationContext context = new StandardEvaluationContext();//构造上下文
context.setVariable("end", "!");
Assert.assertEquals("Hello World!", expression.getValue(context)); //求值,根据上下文获得表达式
}
}linux内核中的文件描述符(六)--fd的分配--expand_files
Kernel version:2.6.14
CPU architecture:ARM920T
Author:ce123(http://blog.csdn.net/ce123)
我们先贴出expand_files函数的源码:
int expand_files(struct files_struct *files, int nr)
{
int err, expand = 0;
struct fdtable *fdt;
fdt = files_fdtable(files);
if (nr >= fdt->max_fdset || nr >= fdt->max_fds) { //我们在前面的文章中已经分析过,初始时max_fdset = 1024,max_fds = 32
if (fdt->max_fdset >= NR_OPEN || //#define NR_OPEN (1024*1024) /* Absolute upper limit on fd num */
fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
err = -EMFILE; //max_fdset和max_fds都不能大于 NR_OPEN,否则返回 -EMFILE,即打开太多的文件
goto out;
}
expand = 1;
if ((err = expand_fdtable(files, nr)))//真正进行扩展
goto out;
}
err = expand;
out:
return err;
}expand_files函数进行一些检查后调用expand_fdtable进行文件描述符表的扩展,下面分析expand_fdtable函数。
static int expand_fdtable(struct files_struct *files, int nr)
__releases(files->file_lock)
__acquires(files->file_lock)
{
int error = 0;
struct fdtable *fdt;
struct fdtable *nfdt = NULL;
spin_unlock(&files->file_lock);
nfdt = alloc_fdtable(nr);//根据nr重新创建一个新的fdtable
if (!nfdt) {
error = -ENOMEM;
spin_lock(&files->file_lock);
goto out;
}
spin_lock(&files->file_lock);
fdt = files_fdtable(files);
/*
* Check again since another task may have expanded the
* fd table while we dropped the lock
*/
if (nr >= fdt->max_fds || nr >= fdt->max_fdset) { //nr值必须大于max_fds和max_fdset值,这里再次进行检查是防止另一个进程进行了expand
copy_fdtable(nfdt, fdt); //将旧的fdtable中的内容拷贝至新的fdtable
} else {
/* Somebody expanded while we dropped file_lock */
spin_unlock(&files->file_lock);
__free_fdtable(nfdt);
spin_lock(&files->file_lock);
goto out;
}
rcu_assign_pointer(files->fdt, nfdt);//用新的fdtable替换旧的fdtable
free_fdtable(fdt);//释放旧的fdtable
out:
return error;
}我们再来看一下扩展文件描述符表的关键函数alloc_fdtable,其定义如下:static struct fdtable *alloc_fdtable(int nr)
{
struct fdtable *fdt = NULL;
int nfds = 0;
fd_set *new_openset = NULL, *new_execset = NULL;
struct file **new_fds;
fdt = kmalloc(sizeof(*fdt), GFP_KERNEL);
if (!fdt)
goto out;
memset(fdt, 0, sizeof(*fdt));
nfds = __FD_SETSIZE; //#define __FD_SETSIZE 1024
// #define PAGE_SHIFT 12
// #define PAGE_SIZE (1UL << PAGE_SHIFT)
/* Expand to the max in easy steps */
do {
if (nfds < (PAGE_SIZE * 8))//dfds = 1024
nfds = PAGE_SIZE * 8;
else {
nfds = nfds * 2;
if (nfds > NR_OPEN)
nfds = NR_OPEN;
}
} while (nfds <= nr);//第一次expand时,nr应该等于32
new_openset = alloc_fdset(nfds);//分配打开文件位图
new_execset = alloc_fdset(nfds);
if (!new_openset || !new_execset)
goto out;
fdt->open_fds = new_openset;
fdt->close_on_exec = new_execset;
fdt->max_fdset = nfds;//更新max_fdset值,此时这个值为32k
nfds = NR_OPEN_DEFAULT;//nfds = 32
/*
* Expand to the max in easy steps, and keep expanding it until
* we have enough for the requested fd array size.
*/
do {
#if NR_OPEN_DEFAULT < 256
if (nfds < 256)
nfds = 256;//nfds = 256(32->256->1024)
//无法超过1024,因为在最开始的就进行了检查,一定要小于current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
else
#endif
if (nfds < (PAGE_SIZE / sizeof(struct file *)))
nfds = PAGE_SIZE / sizeof(struct file *);
else {
nfds = nfds * 2;
if (nfds > NR_OPEN)
nfds = NR_OPEN;
}
} while (nfds <= nr);
new_fds = alloc_fd_array(nfds);//分配文件描述符数组
if (!new_fds)
goto out;
fdt->fd = new_fds;
fdt->max_fds = nfds;//更新max_fds
fdt->free_files = NULL;
return fdt;
out:
if (new_openset)
free_fdset(new_openset, nfds);
if (new_execset)
free_fdset(new_execset, nfds);
kfree(fdt);
return NULL;
}
alloc_fd_array和alloc_fdset采用kmalloc或者vmalloc进行内存分配。
sqoop是让hadoop技术支持的clouder公司开发的一个在关系数据库和hdfs,hive之间数据导入导出的一个工具
sqoop-1.2.0-CDH3B4依赖hadoop-core-0.20.2-CDH3B4.jar,所以你需要下载hadoop-0.20.2-CDH3B4.tar.gz,解压缩后将hadoop-0.20.2-CDH3B4/hadoop-core-0.20.2-CDH3B4.jar复制到sqoop-1.2.0-CDH3B4/lib中。我们只需要hadoop-core-0.20.2-CDH3B4.jar,hadoop的环境我们还可以使用Apache的开源hadoop。
下面是CDH3和SQOOP 1.2.0的下载地址
http://archive.cloudera.com/cdh/3/hadoop-0.20.2-CDH3B4.tar.gz
http://archive.cloudera.com/cdh/3/sqoop-1.2.0-CDH3B4.tar.gz
另外,sqoop导入mysql数据运行过程中依赖mysql-connector-java-*.jar,
2、修改SQOOP的文件configure-sqoop,注释掉hbase和zookeeper检查(除非你准备使用HABASE等HADOOP上的组件)
#if [ ! -d "${HBASE_HOME}" ]; then
# echo “Error: $HBASE_HOME does not exist!”
# echo ‘Please set $HBASE_HOME to the root of your HBase installation.’
# exit 1
#fi
#if [ ! -d "${ZOOKEEPER_HOME}" ]; then
# echo “Error: $ZOOKEEPER_HOME does not exist!”
# echo ‘Please set $ZOOKEEPER_HOME to the root of your ZooKeeper installation.’
# exit 1
#fi
还有其他HBASE和ZOOKEEPER的部分也需要注释掉。
3.在使用sqoop之前需要首先启动hadoop
4.常用的sqoop命令:
1》列出mysql数据库中的所有数据库
sqoop list-databases --connect jdbc:mysql://localhost:3306/ --username root --password 123456
2)连接mysql并列出数据库中的表
sqoop list-tables --connect jdbc:mysql://localhost:3306/test --username root --password 123456
命令中的test为mysql数据库中的test数据库名称 username password分别为mysql数据库的用户密码
3)将关系型数据的表结构复制到hive中,只是复制表的结构,表中的内容没有复制过去。
sqoop create-hive-table --connect jdbc:mysql://localhost:3306/test --table sqoop_test --username root --password 123456 --hive-table test
其中 --table sqoop_test为mysql中的数据库test中的表 --hive-table test 为hive中新建的表名称
4)从关系数据库导入文件到hive中
sqoop import --connect jdbc:mysql://localhost:3306/zxtest --username root --password 123456 --table sqoop_test --hive-import --hive-table s_test -m 1
5)将hive中的表数据导入到mysql中,在进行导入之前,mysql中的表hive_test必须已经提起创建好了。
sqoop export --connect jdbc:mysql://localhost:3306/zxtest --username root --password root --table hive_test --export-dir /user/hive/warehouse/new_test_partition/dt=2012-03-05
6》从数据库导出表的数据到HDFS上文件
sqoop import --connect jdbc:mysql://localhost:3306/zxtest --username root --password 123456 --table hive_test -m 1