hadoop中文wordcount无结果输出

废话不多说,最近在学hadoop,然后利用docker搭建的集群,网上有利用docker搭建hadoop的博客教程。

但是需要提醒一下,我看的两篇博客都没设置将mapreduce函数放到yarn平台跑,都是自己利用本地机器跑的。

自己边搭边踩坑,终于遇到一个百度也没直接找到解决方法的问题。

英文的wordcount跑成功之后,寻思利用分词做个中文的wordcount。

我是按照视频来的,就是网上下载的别人的培训视频。分词器用的IK分词器。

对了,在此,因为maven貌似对IK分词器不更新了,得把配置文件写一下

在csdn上下载了jar包,然后在maven中配置了本地jar包

<dependency>
			<groupId>org.wltea</groupId>
			<artifactId>ik-analyzer</artifactId>
			<version>3.2.8</version>
			<scope>system</scope>
			<systemPath>/home/username/workspace/hadoop1/lib/IKAnalyzer3.2.8.jar</systemPath>
</dependency>

项目打包时要将IK分词器一起打包进去

<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>cn.edu.bjut.hadoop.WcAndIk</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
              ------------------------------------------------------------------------------------------------- 
              <resources>
			<resource>
				<targetPath>lib/</targetPath>
				<directory>lib/</directory>
				<includes>
					<include>**/IKAnalyzer3.2.8.jar</include>
				</includes>
			</resource>
		</resources>
           -------------------------------------------------------------------------------------------------------
 </build>
map函数的写法如下:

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			// StringTokenizer itr = new StringTokenizer(value.toString());
			// while (itr.hasMoreTokens()) {
			// word.set(itr.nextToken());
			// context.write(word, one);
			// }
			byte[] bytes = value.getBytes();
			InputStream is = new ByteArrayInputStream(bytes);
			Reader reader = new InputStreamReader(is);
			IKSegmentation ikSegmentation = new IKSegmentation(reader);
			Lexeme t = null;
			while ((t = ikSegmentation.next()) != null) {
				context.write(new Text(t.getLexemeText()), new IntWritable(1));
			}
		}
reduce函数因为和英文的wordcount一致就不贴了。

-------------------------------------------------------------------------------------

结果jar包上传到集群,利用hadoop命令跑的时候,愣是没有结果,后来在map函数里加入了System.out.println之类的输出语句

发现了输入语句是乱码。然后上网搜原因,发现大家都说hadoop的编译是基于java的,是UTF-8的,然后我就想,难道是我的待

分词文件不是UTF-8的?然后就各种搜索ubuntu下更改文件编码的方式,结果永远是乱码,乱码,乱码。

关键是网上别人做中文wordcount顶多是遇到输出是乱码,可是我这压根没输出阿。

------------------------------------------------------------------------------------------

后来灵光一现,会不会是我的java在编译时,不是以utf-8的编码格式编译文件的?

于是,写个含有中文String的代码放到集群节点机上编译,果然提示中文呢乱码无法识别

unmappable character for encoding ASCII

后来就直接搜索这个问题,有不少人建议在javac 命令时加参数,对我来说没用,因为我没法控制集群编译参数,

在StackOverFlow上找到一个解答(在此抱怨下学校网速,网页最起码花了20秒才打开)

stackoverflow.com/questions/26067350/unmappable-character-for-encoding-ascii-but-my-files-are-in-utf-8
于是我修改了 ~/.bashrc,加入了

export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8

为防止不生效,保存~/.bashrc之后,执行了 source ~/.bashrc
再次编译测试代码,不再不识别中文,问题也相应解决了。

----------------------------------------------------------------------------------

有点疑惑,貌似大多数人在ubuntu下装的java在编译时都是可以直接识别中文的,不知道为啥我装的这个需要如此设定。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值