HanLP分詞,如README中所說,若是沒有特殊需求,能夠經過maven配置,若是要添加自定義詞典,須要下載「依賴jar包和用戶字典".java
分享某大神的示例經驗:服務器
是直接"java xf hanlp-1.6.8-sources.jar" 解壓源碼,把源碼加入工程(依賴本地jar包,有些麻煩,有時候到服務器有找不到jar包的狀況)maven
按照文檔操做,在Spark中分詞,默認找的是本地目錄,因此若是是在driver中分詞是沒有問題的。可是若是要分佈式分詞,是要把詞典目錄放在HDFS上面,由於這樣每臺機器才能夠訪問到 【參考代碼】分佈式
最好把新增詞典放在首位(沒有放在首位好像沒有生效).第一次使用時,HanLP會把新增txt文件,生成bin文件,這個過程比較慢。可是隻須要跑一次,它會把bin文件寫到HDFS路徑上面,第二次之後速度就快一些了。ide
注意到issue中說,只能夠在mapPartition中使用oop
參考scala代碼spa
class HadoopFileIoAdapter extends IIOAdapter {scala
override def create(path: String): java.io.OutputStream = {文檔
val conf: Configuration = new Configuration()get
val fs: FileSystem = FileSystem.get(URI.create(path), conf)
fs.create(new Path(path))
}
override def open(path: String): java.io.InputStream = {
val conf: Configuration = new Configuration()
val fs: FileSystem = FileSystem.get(URI.create(path), conf)
fs.open(new Path(path))
}
}
def myfuncPerPartition_ ( iter : Iterator [String] ) : Iterator[(Int, mutable.Buffer[String])] = {
println("run in partition")
val keyWordNum = 6
HanLP.Config.IOAdapter = new HadoopFileIoAdapter
val ret = iter.filter(_.split(",",2).length==2)
.map(line=>(line.split(",",2)(1).trim.hashCode, HanLP.extractKeyword(line.split(",",2)(0),keyWordNum)
.map(str=>str.filterNot(stopChar.contains(_))).filter(w=>(w.length>1 || ( w.length==1 && white_single_word.contains(w(0))) ))
.filterNot(stopWords.contains(_)).take(keyWordNum).distinct))
ret
}
//調用
raw_data.repartition(100).mapPartitions(myfuncPerPartition_)