从Spark Scala Shell将列族添加到现有的hbase表中

时间:2018-10-24 18:02:08

标签: scala apache-spark hbase

我在hbase中有一个带有2个列族(cf1,cf2)的表(test),现在我想从spark shell向hbase中的现有表添加一个列族。我尝试使用以下语句,但它表示该表已存在

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.{HBaseAdmin,HTable,Put,Get}
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor}
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HColumnDescriptor
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.client.HTable


val conf = HBaseConfiguration.create()
val admin = new HBaseAdmin(conf)
conf.set("hbase.rootdir","hdfs://")
conf.set("hbase.zookeeper.quorum","")
conf.setInt("hbase.zookeeper.property.clientPort", )

val tableName = "test"
val tableDesc = new HTableDescriptor(tableName)
tableDesc.addFamily(new HColumnDescriptor("cf3"))
admin.createTable(tableDesc)

是否可以使用Scala从Spark Shell向hbase中的现有测试表添加列族(cf3)?

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要先禁用该表。然后添加columnFamily并将其启用。

val conf = HBaseConfiguration.create()
val admin = new HBaseAdmin(conf)
conf.set("hbase.rootdir","hdfs://")
conf.set("hbase.zookeeper.quorum","")
conf.setInt("hbase.zookeeper.property.clientPort", )

val tableName = "test"
val table = TableName.valueOf(tableName)
admin.disableTable(table) 
admin.addColumn(table,new HColumnDescriptor("cf3"))
admin.enableTable(tableDesc)

在Java中,它等效于此:

Configuration config = HBaseConfiguration.create();
Admin admin = new Admin(conf);
TableName table = TableName.valueOf("myTable");

admin.disableTable(table);

HColumnDescriptor cf1 = ...;
admin.addColumn(table, cf1);      // adding new ColumnFamily
HColumnDescriptor cf2 = ...;
admin.modifyColumn(table, cf2);    // modifying existing ColumnFamily

admin.enableTable(table);