import scalaj.http.Http class SolrAdd () {
// 方法接受两个参数,dataType为数据集名称,jsonString为数据json字符串 def postToSolr (dataType:String, jsonString: String): Unit = {
// 字符串拼接 var data = "{‘add‘:{ ‘doc‘:" + jsonString + ",‘boost‘:1.0,‘overwrite‘:true,‘commitWithin‘:1000}}" val result = Http("http://IP:8985/solr/"+dataType+"/update?wt=json") .postData(data) .header("Content-Type", "application/json").asString println(result) } }
object TestDemo { def main(args: Array[String]): Unit = { var data = "{‘id‘: ‘5566‘,‘quanwen‘: ‘我是一个大好人‘}" var zty = new SolrAdd()
// 调用方法,传递数据集名称和数据 zty.postToSolr("idfscore", data) } }