创建表
put操作
public class myHbase {
private static Configuration conf= null;
static {
conf=HBaseConfigurationcreate()
}
/**
* column familys add column <>hbase columnName
* @param tableName
* @param familys
* @throws Exception
*/
public void createTable(String tableNameString[] familys) throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf)
if(!admintableExists(tableName)){
HTableDescriptor desc = new HTableDescriptor(tableName)
for (int i = ; i < familyslength; i++) {
descaddFamily(new HColumnDescriptor(familys[i]))
}
admincreateTable(desc)
}else{
Systemoutprintln(the table:+ tableName + exsists)
};
}
/**
* @param tableName
* @param rowKey
* @param family
* @param qualifier
* @param value
* @throws Exception
*/
public void putData(String tableNameString rowKey String family
String qualifier String value) throws Exception {
HTable ht = new HTable(conf tableName)
Put put = new Put(BytestoBytes(rowKey))
putadd(BytestoBytes(family) BytestoBytes(qualifier) BytestoBytes(value))
htput(put)
}
public static void main(String [] agrs) throws Exception{
myHbase h= new myHbase()
String [] familys = {familyfamily};
hcreateTable(mytable familys)
hputData(mytable row family qualifier:name value)
}
}
运行java应用程序登录hbase shell 发现可以get和scan到数据了
hbase(main):> scan mytable
ROW COLUMN+CELL
row column=family:qualifier:name timestamp= v
alue=value
row(s) in seconds
hbase(main):> get mytablerowfamily:qualifier:name
COLUMN CELL
family:qualifier:na timestamp= value=value
me
row(s) in seconds