<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://myhostname:8020/hbase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>192.168.1.20</value> </property> <property> <name>hbase.zookeeper.property.clientPort</name> <value>2181</value> </property> </configuration>Windows下HBase客戶端操做HBase服務器端時,須要在%WINDOWS_HOME%\System32\drivers\etc\hosts下綁定HBase使用Zookeeper主機名稱,好比
192.168.1.20 cos2
這行。其中cos2 爲hbase運行的zookeeper服務器名稱。 服務器
3.2 建立表
/**
* 建立表操做
* @throws IOException
*/
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.valueOf(tableName));
for (int i = 0; i < cfs.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
}
admin.createTable(tableDesc);
System.out.println("表建立成功!");
}
} eclipse
3.3 刪除表
/**
* 刪除表操做
* @param tablename
* @throws IOException
*/
public void deleteTable(String tablename) throws IOException {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.println("表刪除成功!");
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
} jsp
3.4 插入一行記錄
/**
* 插入一行記錄
* @param tablename
* @param cfs
*/
public void writeRow(String tablename, String[] cfs) {
try {
HTable table = new HTable(conf, tablename);
Put put = new Put(Bytes.toBytes("rows1"));
for (int j = 0; j < cfs.length; j++) {
put.add(Bytes.toBytes(cfs[j]),
Bytes.toBytes(String.valueOf(1)),
Bytes.toBytes("value_1"));
table.put(put);
}
} catch (IOException e) {
e.printStackTrace();
}
} ui
3.5 刪除一行記錄
/**
* 刪除一行記錄
* @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("刪除行成功!");
} spa
3.6 查找一行記錄
/**
* 查找一行記錄
* @param tablename
* @param rowkey
*/
public static void selectRow(String tablename, String rowKey)
throws IOException {
HTable table = new HTable(conf, tablename);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.print(new String(kv.getRow()) + " ");
System.out.print(new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()));
}
} code
3.7 查詢表中全部行
/**
* 查詢表中全部行
* @param tablename
*/
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();
}
} xml