Hadoop Configuration设置自定义类变量

hadoop1.0.4

有时在编写Hadoop的MR的时候,会想到如果在Configuration里面可以设置一个类变量多好呀。查看Configuration的api可以看到,一般set方法都是set一般数据类型,比如int,string或者double之类的。那有没有一个方法设置一个自定义类的变量呢,比如setClass,还真别说,还真有这个方法。

查看api:

setClass

public void setClass(String name,
                     Class<?> theClass,
                     Class<?> xface)
Set the value of the name property to the name of a theClass implementing the given interface xface. An exception is thrown if theClass does not implement the interface xface.
Parameters:
name - property name.
theClass - property value.
xface - the interface implemented by the named class.
但是,感觉不怎么对的?网上查了下,发现这个是设置类型的,用法如下:

/**
	 * 根据变量获得数据类型
	 */
	public static void testSetClass(){
		Configuration conf= new Configuration();
		
		conf.setClass("mapout", LongWritable.class	, Writable.class);
		Class<?> b=conf.getClass("mapout", Writable.class);
		System.out.println(b);
		conf.setClass("myClass", User.class, Person.class);
		b=conf.getClass("myClass", Person.class);
		System.out.println(b);
	}
这样打印出来的是(User是Person的子类):

class org.apache.hadoop.io.LongWritable
class org.fz.testconfig.User
这个其实就是设置类型用的,感觉用处不是很大。如果想在Mapper或者Reducer的setup函数中获得某个类型,那么其实也可以使用下面的方式:

/**
	 * 根据包路径获得数据类型
	 * @throws ClassNotFoundException
	 */
	public static void testGetClassByName() throws ClassNotFoundException{
		Configuration conf= new Configuration();
		Class<?> a=conf.getClassByName("org.apache.hadoop.io.Long
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值