packagecom.yilian.util;importjava.io.File;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.Delete;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.client.Table;importorg.apache.hadoop.hbase.util.Bytes;public classHbaseTest2 {public staticConfiguration configuration;public staticConnection connection;public staticAdmin admin;public static void main(String[] args) throwsIOException {//createTable("t2", new String[] { "cf1", "cf2" });
listTables();/** insterRow("t2", "rw1", "cf1", "q1", "val1"); getData("t2", "rw1",
* "cf1", "q1"); scanData("t2", "rw1", "rw2");
* deleRow("t2","rw1","cf1","q1"); deleteTable("t2");*/}//初始化链接
public static voidinit() {
configuration=HBaseConfiguration.create();/** configuration.set("hbase.zookeeper.quorum",
* "10.10.3.181,10.10.3.182,10.10.3.183");
* configuration.set("hbase.zookeeper.property.clientPort","2181");
* configuration.set("zookeeper.znode.parent","/hbase");*/configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "101.236.39.141,101.236.46.114,101.236.46.113");
configuration.set("hbase.master", "101.236.39.141:60000");
File workaround= new File(".");
System.getProperties().put("hadoop.home.dir",
workaround.getAbsolutePath());new File("./bin").mkdirs();try{new File("./bin/winutils.exe").createNewFile();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
}catch(IOException e) {
e.printStackTrace();
}
}//关闭连接
public static voidclose() {try{if (null !=admin)
admin.close();if (null !=connection)
connection.close();
}catch(IOException e) {
e.printStackTrace();
}
}//建表
public static void createTable(String tableNmae, String[] cols) throwsIOException {
init();
TableName tableName=TableName.valueOf(tableNmae);if(admin.tableExists(tableName)) {
System.out.println("talbe is exists!");
}else{
HTableDescriptor hTableDescriptor= newHTableDescriptor(tableName);for(String col : cols) {
HColumnDescriptor hColumnDescriptor= newHColumnDescriptor(col);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
close();
}//删表
public static void deleteTable(String tableName) throwsIOException {
init();
TableName tn=TableName.valueOf(tableName);if(admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}//查看已有表
public static void listTables() throwsIOException {
init();
HTableDescriptor hTableDescriptors[]=admin.listTables();for(HTableDescriptor hTableDescriptor : hTableDescriptors) {
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}//插入数据
public static voidinsterRow(String tableName, String rowkey, String colFamily, String col, String val)throwsIOException {
init();
Table table=connection.getTable(TableName.valueOf(tableName));
Put put= newPut(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);//批量插入
/** List putList = new ArrayList(); puts.add(put);
* table.put(putList);*/table.close();
close();
}//删除数据
public static void deleRow(String tableName, String rowkey, String colFamily, String col) throwsIOException {
init();
Table table=connection.getTable(TableName.valueOf(tableName));
Delete delete= newDelete(Bytes.toBytes(rowkey));//删除指定列族//delete.addFamily(Bytes.toBytes(colFamily));//删除指定列//delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);//批量删除
/** List deleteList = new ArrayList();
* deleteList.add(delete); table.delete(deleteList);*/table.close();
close();
}//根据rowkey查找数据
public static void getData(String tableName, String rowkey, String colFamily, String col) throwsIOException {
init();
Table table=connection.getTable(TableName.valueOf(tableName));
Get get= newGet(Bytes.toBytes(rowkey));//获取指定列族数据//get.addFamily(Bytes.toBytes(colFamily));//获取指定列数据//get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
Result result =table.get(get);
showCell(result);
table.close();
close();
}//格式化输出
public static voidshowCell(Result result) {
Cell[] cells=result.rawCells();for(Cell cell : cells) {
System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp:" + cell.getTimestamp() + " ");
System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
}
}//批量查找数据
public static void scanData(String tableName, String startRow, String stopRow) throwsIOException {
init();
Table table=connection.getTable(TableName.valueOf(tableName));
Scan scan= newScan();//scan.setStartRow(Bytes.toBytes(startRow));//scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner resultScanner =table.getScanner(scan);for(Result result : resultScanner) {
showCell(result);
}
table.close();
close();
}
}