<configuration>
<property>
<name>hbase.rootdirname>
<value>file:///root/test/hbasevalue>
property>
<property>
<name>hbase.zookeeper.property.dataDirname>
<value>/root/test/zookeepervalue>
property>
<property>
<name>hbase.zookeeper.quorumname>
<value>127.0.0.1:2181value>
property>
configuration>
改成自己本地下的java的路径
export JAVA_HOME=/usr/local/java
root@fonxian-desktop:~/hbase/bin# start-hbase.sh
测试有没有启动成功,输入命令
root@fonxian-desktop:~/hbase/bin# jps
16787 Jps
9653 HMaster
9131 JobTracker
9963 Main
8715 NameNode
9292 TaskTracker
9037 SecondaryNameNode
8878 DataNode
有HMaster说明配置成功
root@fonxian-desktop:~/hbase/bin# ./hbase shell
其中’mytable’是表名,’cf’是列族名
HBase中,每个表必须至少要有一个列族(column family)
create 'mytable','cf'
scan 'mytablee'
在’first’行的’cf:message’列,写入数据
put 'mytable','first','cf:message','hello HBase'
get 'mytable','first'
describe 'mytable'
list
首先先要让表无效,否则无法直接删除
disable 'tablename'
drop 'tablename'
删除列
delete 'tablename','rowkey','family:column'
<configuration>
<property>
<name>hbase.rootdirname>
<value>file:///root/test/hbasevalue>
property>
<property>
<name>hbase.cluster.distributedname>
<value>flasevalue>
property>
<property>
<name>hbase.zookeeper.property.dataDirname>
<value>/root/test/zookeepervalue>
property>
<property>
<name>hbase .zookeeper.quorumname>
<value>127.0.0.1:2181value>
property>
<property skipInDoc="true">
<name>hbase.defaults.for.versionname>
<value>0.94.14value>
property>
configuration>
将HBase/lib下的jar包都导入到eclipse中,不导入的话,会出现诸多类找不到的错误
private static Configuration conf = null;
/**
* HBaseConfiguration作用:对HBase进行配置
*/
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "127.0.0.1");
conf.set("hbase.master", "127.0.0.1:60010");
}
/**
* 创建表操作
*
* @throws IOException
* HBaseAdmin作用:提供HBase数据库中表相关信息的接口
*/
public void createTable(String tablename, String[] cfs) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename)) {
System.out.println("表已经存在!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tablename);
for (int i = 0; i < cfs.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
}
admin.createTable(tableDesc);
System.out.println("表创建成功!");
}
}
/**
* 插入一行记录
*
* @param tablename
* @param cfs
* Put的作用:向表 tablename 添加 “family,qualifier,value”指定的值。
* put 'tableName','rowKey','family:qualifier','value',
*/
public void writeRow(String tableName, String rowKey, String family, String qualifier, String value) {
try {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
System.out.println("插入记录成功");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查询表中所有行
*
* @param tablename
* HTable的作用是与HBase中的表进行通信
*/
public void scaner(String tablename) {
try {
HTable table = new HTable(conf, tablename);
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
KeyValue[] kv = r.raw();
for (int i = 0; i < kv.length; i++) {
System.out.print(new String(kv[i].getRow()) + "");
System.out.print(new String(kv[i].getFamily()) + ":");
System.out.print(new String(kv[i].getQualifier()) + "");
System.out.print(kv[i].getTimestamp() + "");
System.out.println(new String(kv[i].getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除一行记录
*
* @param tablename
* @param rowkey
* @throws IOException
*/
public void deleteRow(String tablename, String rowkey) throws IOException {
HTable table = new HTable(conf, tablename);
List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1);
table.delete(list);
System.out.println("删除行成功!");
}
public static void main(String[] args) throws Exception {
HBaseTest t = new HBaseTest();
// 创建表
String tableName = "scores";
String[] cfs = { "grade", "course" };
t.createTable(tableName, cfs);
// 添加记录
t.writeRow(tableName, "second", "grade", "message", "87");
// 打印表中数据
t.scaner("scores");
//删除记录
t.deleteRow("scores", "second");
}