【发布时间】:2017-05-22 11:42:36
【问题描述】:
我有一个数据框,我想将它插入到 hbase 中。我关注这个documenation。
这就是我的数据框的样子:
--------------------
|id | name | address |
|--------------------|
|23 |marry |france |
|--------------------|
|87 |zied |italie |
--------------------
我使用以下代码创建了一个 hbase 表:
val tableName = "two"
val conf = HBaseConfiguration.create()
if(!admin.isTableAvailable(tableName)) {
print("-----------------------------------------------------------------------------------------------------------")
val tableDesc = new HTableDescriptor(tableName)
tableDesc.addFamily(new HColumnDescriptor("z1".getBytes()))
admin.createTable(tableDesc)
}else{
print("Table already exists!!--------------------------------------------------------------------------------------")
}
现在如何将这个数据帧插入到 hbase 中?
在另一个示例中,我使用以下代码成功插入 hbase:
val myTable = new HTable(conf, tableName)
for (i <- 0 to 1000) {
var p = new Put(Bytes.toBytes(""+i))
p.add("z1".getBytes(), "name".getBytes(), Bytes.toBytes(""+(i*5)))
p.add("z1".getBytes(), "age".getBytes(), Bytes.toBytes("2017-04-20"))
p.add("z2".getBytes(), "job".getBytes(), Bytes.toBytes(""+i))
p.add("z2".getBytes(), "salary".getBytes(), Bytes.toBytes(""+i))
myTable.put(p)
}
myTable.flushCommits()
但是现在我被卡住了,如何将我的数据帧的每条记录插入到我的 hbase 表中。
感谢您的时间和关注
【问题讨论】:
-
问题不清楚。你在做别的事情。 hbase.apache.org/book.html#_sparksql_dataframes 告诉您定义目录并在 sc.parallelize(data).toDF.write.options 中使用以将 DF 保存到 HBase。
-
是的,并提到我正在使用该文档。我被困在这里
val data = (0 to 255).map { i => HBaseRecord(i, "extra")}如何插入我的数据帧的 foreach 记录,而不是从 0 到 255
标签: scala apache-spark dataframe hbase rdd