put用法:
put '表名','主键','列族:列名','值'
create用法:
create '表名','列族名'
scan用法:
查询表
scan '表明'
get用法:
查询具体列族
get '表名','主键','列族名:列'
删除表步骤:
1、禁用表
disable '表名'
2、删除表
drop '表名'
Java 操作表:
准备:
1、导入HBase中lib中的Jar文件和JUnit Jar文件
2、把hbase-site.xml、hdfs-site.xml配置文件拷贝到工程src目录下
代码:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HbaseDemo {
HBaseAdmin hBaseAdmin;
String TN="phone";
HTable hTable;
@Before
public void begin() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1,node2,node3");
hBaseAdmin=new HBaseAdmin(conf);
hTable=new HTable(conf, TN);
}
@After
public void end(){
if(hBaseAdmin!=null){
try {
hBaseAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(hTable!=null){
try {
hTable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// @Test
// public void insert() throws IOException{
// String rowkey="13964223261_201612312456";
// Put put=new Put(rowkey.getBytes());
// put.add("cf1".getBytes(), "type".getBytes(), "1".getBytes());
// put.add("cf1".getBytes(), "time".getBytes(), "2016".getBytes());
// put.add("cf1".getBytes(), "time".getBytes(), "2016".getBytes());
// hTable.put(put);
// }
// @Test
// public void createTable() throws IOException{
// if(hBaseAdmin.tableExists(TN)){
// hBaseAdmin.disableTable(TN);
// hBaseAdmin.deleteTable(TN);
// }
// HTableDescriptor hTableDescriptor=new
// HTableDescriptor(TableName.valueOf(TN));
// HColumnDescriptor family=new HColumnDescriptor("cf1");
// family.setBlockCacheEnabled(true);
// family.setInMemory(true);
// family.setMaxVersions(1);
// hTableDescriptor.addFamily(family);
// hBaseAdmin.createTable(hTableDescriptor);
// }
@Test
public void get() throws IOException{
String rowkey="13964223261_201612312456";
Get get=new Get(rowkey.getBytes());
//下面两行表示请求查询服务器时只查询'type'、'time'字段
get.addColumn("cf1".getBytes(), "type".getBytes());
get.addColumn("cf1".getBytes(), "time".getBytes());
Result res=hTable.get(get);
//从结果中拿出需要的结果
Cell cell=res.getColumnLatestCell("cf1".getBytes(), "time".getBytes());
System.out.println("========="+new String(CellUtil.cloneValue(cell)));
Cell cell1=res.getColumnLatestCell("cf1".getBytes(), "type".getBytes());
System.out.println("========="+new String(CellUtil.cloneValue(cell1)));
}
}