欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 中Spark中将对象序列化存储到hdfs

程序员文章站 2023-12-17 17:12:22
java 中spark中将对象序列化存储到hdfs 摘要: spark应用中经常会遇到这样一个需求: 需要将java对象序列化并存储到hdfs, 尤其是利用mllib计算...

java 中spark中将对象序列化存储到hdfs

摘要: spark应用中经常会遇到这样一个需求: 需要将java对象序列化并存储到hdfs, 尤其是利用mllib计算出来的一些模型, 存储到hdfs以便模型可以反复利用. 下面的例子演示了spark环境下从hbase读取数据, 生成一个word2vec模型, 存储到hdfs.

废话不多说, 直接贴代码了. spark1.4 + hbase0.98

import org.apache.spark.storage.storagelevel
import scala.collection.javaconverters._
import java.io.file
import java.io.fileinputstream
import java.io.fileoutputstream
import java.io.objectinputstream
import java.io.objectoutputstream
import java.net.uri
import java.util.date
import org.ansj.library.userdefinelibrary
import org.ansj.splitword.analysis.nlpanalysis
import org.ansj.splitword.analysis.toanalysis
import org.apache.hadoop.fs.fsdatainputstream
import org.apache.hadoop.fs.fsdataoutputstream
import org.apache.hadoop.fs.filesystem
import org.apache.hadoop.fs.fileutil
import org.apache.hadoop.fs.path
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.hbase.{hbaseconfiguration, htabledescriptor, tablename}
import org.apache.hadoop.hbase.filter.filterlist
import org.apache.hadoop.hbase.filter.pagefilter
import org.apache.hadoop.hbase.filter.regexstringcomparator
import org.apache.hadoop.hbase.filter.singlecolumnvaluefilter
import org.apache.hadoop.hbase.filter.comparefilter.compareop
import org.apache.hadoop.hbase.mapreduce.tableinputformat
import org.apache.hadoop.hbase.protobuf.protobufutil
import org.apache.hadoop.hbase.util.{base64, bytes}
import com.feheadline.fespark.db.neo4jmanager
import com.feheadline.fespark.util.env
import org.apache.spark.sparkconf
import org.apache.spark.sparkcontext
import org.apache.spark.rdd._
import org.apache.spark.mllib.feature.{word2vec, word2vecmodel}
import scala.math.log
import scala.io.source

object word2vecdemo {

 def convertscantostring(scan: scan) = {
  val proto = protobufutil.toscan(scan)
  base64.encodebytes(proto.tobytearray)
 }

 def main(args: array[string]): unit = {
  val sparkconf = new sparkconf().setappname("word2vec demo")
  sparkconf.set("spark.serializer", "org.apache.spark.serializer.kryoserializer")
  sparkconf.set("spark.kryoserializer.buffer", "256m")
  sparkconf.set("spark.kryoserializer.buffer.max","2046m")
  sparkconf.set("spark.akka.framesize", "500")
  sparkconf.set("spark.rpc.asktimeout", "30")
  

  val sc = new sparkcontext(sparkconf)
  val hbaseconf = hbaseconfiguration.create()
  hbaseconf.set("hbase.zookeeper.quorum", "myzookeeper")

  hbaseconf.set(tableinputformat.input_table, "crawled")

  val scan = new scan()
  val filterlist:filterlist = new filterlist(filterlist.operator.must_pass_all)
  
  val comp:regexstringcomparator = new regexstringcomparator(""".{1500,}""")
  
  val articlefilter:singlecolumnvaluefilter = new singlecolumnvaluefilter(
  "data".getbytes,
  "article".getbytes,
  compareop.equal,
  comp
  )
  
  filterlist.addfilter(articlefilter)
  filterlist.addfilter(new pagefilter(100))
  
  scan.setfilter(filterlist)
  scan.setcaching(50)
  scan.setcacheblocks(false)
  hbaseconf.set(tableinputformat.scan,convertscantostring(scan))

  val crawledrdd = sc.newapihadooprdd(
   hbaseconf,
   classof[tableinputformat],
   classof[org.apache.hadoop.hbase.io.immutablebyteswritable],
   classof[org.apache.hadoop.hbase.client.result]
  )
 
  val articlesrdd = crawledrdd.filter{
   case (_,result) => {
     val content = bytes.tostring(result.getvalue("data".getbytes,"article".getbytes))
     content != null
   }
  }

  val wordsindoc = articlesrdd.map{
   case (_,result) => {
     val content = bytes.tostring(result.getvalue("data".getbytes,"article".getbytes))
     if(content!=null)toanalysis.parse(content).asscala.map(_.getname).toseq
     else seq("")
   }
  }
  
  val fitleredwordsindoc = wordsindoc.filter(_.nonempty)
  
  val word2vec = new word2vec()
  val model = word2vec.fit(fitleredwordsindoc)
  
  //---------------------------------------重点看这里-------------------------------------------------------------
  //将上面的模型存储到hdfs
  val hadoopconf = sc.hadoopconfiguration
  hadoopconf.set("fs.defaultfs", "hdfs://myhadoop:9000/")
  val filesystem = filesystem.get(hadoopconf)
  val path = new path("/user/hadoop/data/mllib/word2vec-object")
  val oos = new objectoutputstream(new fsdataoutputstream(filesystem.create(path)))
  oos.writeobject(model)
  oos.close
  
  //这里示例另外一个程序直接从hdfs读取序列化对象使用模型
  val ois = new objectinputstream(new fsdatainputstream(filesystem.open(path)))
  val sample_model = ois.readobject.asinstanceof[word2vecmodel]
  
  /*
  * //你还可以将序列化文件从hdfs放到本地, scala程序使用模型
  * import java.io._
  * import org.apache.spark.mllib.feature.{word2vec, word2vecmodel}
  * val ois = new objectinputstream(new fileinputstream("/home/cherokee/tmp/word2vec-object"))
  * val sample_model = ois.readobject.asinstanceof[word2vecmodel]
  * ois.close
  */
  //--------------------------------------------------------------------------------------------------------------
 }
}


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: