Spark上用saveAsHadoopDataset向hbase保存数据,长时间等待!
先说一下环境,
3台server:master, slave01, slave02
hadoop cluster、hbase cluster、spark cluster 同样的部署在这3台机器上。
首先是代码:
object SparkOnHBase {
def convertScanToString(scan: Scan) = {
val proto = ProtobufUtil.toScan(scan)
Base64.encodeBytes(proto.toByteArray)
}
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("FileAna").setMaster("spark://master:7077").
set("spark.driver.host", "192.168.1.139").
setJars(List("/home/pang/woozoomws/spark-service.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-common-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-client-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-protocol-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/htrace-core-3.1.0-incubating.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-server-1.2.2.jar"))
val sc = new SparkContext(conf)
val hbaseConf = HBaseConfiguration.create()
val jobConf = new JobConf(hbaseConf, this.getClass)
jobConf.setOutputFormat(classOf[TableOutputFormat])
jobConf.set(TableOutputFormat.OUTPUT_TABLE, "MissionItem")
def convert(triple: (Int, String, Int)) = {
val p = new Put(Bytes.toBytes(triple._1))
p.addColumn(Bytes.toBytes("data"), Bytes.toBytes("name"), Bytes.toBytes(triple._2))
p.addColumn(Bytes.toBytes("data"), Bytes.toBytes("age"), Bytes.toBytes(triple._3))
(new ImmutableBytesWritable, p)
}
// step 3: read RDD data from somewhere and convert
val rawData = List((1, "lilei", 14), (2, "hanmei", 18), (3, "someone", 38))
val localData = sc.parallelize(rawData).map(convert)
//step 4: use `saveAsHadoopDataset` to save RDD to HBase
localData.saveAsHadoopDataset(jobConf)
}
}
在scala sdk当中直接运行,在下面几行日志之后,会出现长时间等待:
16/08/25 00:02:00 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on 192.168.1.247:55383 (size: 28.2 KB, free: 366.3 MB)
从web site可以看到大量的下面这样的信息:
16/08/25 22:50:23 INFO client.RpcRetryingCaller: Call exception, tries=10, retries=35, started=38411 ms ago, cancelled=false, msg=row 'MissionItem,,99999999999999' on table 'hbase:meta' at region=hbase:meta,,1.1588230740, hostname=master,16020,1472135364343, seqNum=0
但是如果是下面这样的代码,就没有问题:
val hbaseConf = HBaseConfiguration.create()
val table = new HTable(hbaseConf, TableName.valueOf("MissionItem"))
for (i <- 0 until 100) {
val put = new Put(Bytes.toBytes(String.valueOf(i)))
put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("x"),
Bytes.toBytes(String.valueOf(i)));
table.put(put);
}
谁能告诉我是怎么回事?