废话少说,直接上代码。
val now: Date = new Date()
val gt = now.getTime
var time = gt + ""
val client: HttpClient = new HttpClient
val method: GetMethod = new GetMethod(url)
client.executeMethod(method)
val firstJson = method.getResponseBodyAsString
val json = JSON.parseObject(firstJson)
// println(json)
//第一层json
import scala.collection.JavaConversions._
val dataJson: JSONObject = json.getJSONObject("data")
val hasnext = json.getString("hasnext")
val lastdocid = json.getInteger("lastdocid")+""
val num = json.getString("num")
for (entry <- dataJson.entrySet) {
val dataValue = entry.getValue.toString
val mark = JSON.parseObject(dataValue)
val docid = mark.getInteger("docid")
val conf = HBaseConfiguration.create()
//设置zooKeeper集群地址,也可以通过将hbase-site.xml导入classpath,但是建议在程序里这样设置
conf.set("hbase.zookeeper.quorum","39.104.48.200")
//设置zookeeper连接端口,默认2181
conf.set("hbase.zookeeper.property.clientPort", "2181")
val tablename = "t_map_client_elm_shanghai"
val table: HTable = new HTable(conf, tablename)
val put = new Put(Bytes.toBytes(docid+"2018/7/31"))
put.add(Bytes.toBytes("data"),Bytes.toBytes("time"),Bytes.toBytes("2018/7/31"))
put.add(Bytes.toBytes("data"),Bytes.toBytes("mark"),Bytes.toBytes(dataJson.toJSONString))
table.put(put)
//释放连接
method.releaseConnection()
}