在日常测试中,遇到一个问题:在购买aliyun-Hbase之后,没有在页面找到对应的操作页面。这个时候,我们要怎么办?
第一步下载hbaseue-shell.tar.gz
第二步修改config/hbase-site.xml文件中hbase.zookeeper.quorum,username和password。
<property>
<name>hbase.zookeeper.quorum</name>
<value>xxxx</value>
</property>
<property>
<name>hbase.client.username</name>
<value>xxx</value>
</property>
<property>
<name>hbase.client.password</name>
<value>xxx</value>
</property>
第三步,进入/bin目录下,输入启动命令:./hbase shellimage.png
Hbase的相关操作命令如下:
1、查询所有列表:list
2、查询表是否存在:exists '{table}'
3、查看表结构: describe '{table}'
4、查询表中所有内容: scan '{table}',{LIMIT=>5}
5、查询表中对应row的内容: get '{table}','{row}'
6、表中插入数据:put {table},{rowkey},{family:column},{value},{timestamp}
7、删除表中数据(列):delete {table},{rowkey},{family:column}
8、删除表中数据(行):deleteall {table},{rowkey}
Java代码链接:
public static Configuration conf = HBaseConfiguration.create();
private static HBaseAdmin admin = null;
public static HBaseAdmin createConnect() {
conf.set("hbase.zookeeper.quorum", "IP");//此处应填你的IP地址,如果在windows下运行必须要填写
conf.set("hbase.zookeeper.property.clientPort", "2181");
try {
admin = new HBaseAdmin(conf);
} catch (IOException e) {
e.printStackTrace();
}
return admin;
}
创建hbase的Table。
public void createTable(HBaseAdmin admin, String tableName, String[] columns) throws IOException {
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(Bytes.toBytes(tableName)));
for (String column : columns) {
descriptor.addFamily(new HColumnDescriptor(column));
}
admin.createTable(descriptor);
}
往Table里面插入数据
public void insertData(Configuration conf, String tableName, String rowKey, String colFamily, String column, String value) throws IOException {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
}
//在另一个类里面调用上面的方法。里面的HbaseConnector.conf是HBaseConfiguration.create();
@RequestMapping("/insert")
public String insert(String tableName, String colFamily, String column, String value) throws IOException {
hBaseUtils.insertData(HbaseConnector.conf, tableName, "rowkey", colFamily, column, value);
return "OK";
}