package client; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; public class HBaseClientTest { private static final Logger logger = LoggerFactory.getLogger(HBaseClientTest.class); public static Configuration configuration; public static Connection connection; public static Admin admin; public static void main(String[] args) { System.out.println("Start "); init(); // String tableName = "person"; //// String[] colsValue = {"food", "traffic", "work"}; String tableName = "animal"; String[] colsValue = {"food", "action"}; try { //创建一个表 // createTable(tableName, colsValue); //列出所有的表 listTables(); // scanData(tableName, "ssf", "lbr"); // getData(tableName, "ssf", "food", "breakfast"); // deleRow(tableName, "ssf", "traffic", "work"); // deleteTable(tableName); } catch (IOException e) { e.printStackTrace(); } close(); System.out.println("End "); //和test里一样测试即可 } //删表 public static void deleteTable(String tableName) throws IOException { init(); TableName tn = TableName.valueOf(tableName); if (admin.tableExists(tn)) { admin.disableTable(tn); admin.deleteTable(tn); } close(); } //格式化输出 public static void showCell(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 deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowkey)); //删除指定列族 // delete.addFamily(Bytes.toBytes(colFamily)); //删除指定列 delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col)); table.delete(delete); //批量删除 /* List<Delete> deleteList = new ArrayList<Delete>(); deleteList.add(delete); table.delete(deleteList);*/ table.close(); } //根据rowkey查找数据 public static void getData(String tableName,String rowkey,String colFamily,String col)throws IOException{ Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(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(); } //批量查找数据 public static void scanData(String tableName,String startRow,String stopRow)throws IOException{ Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner){ System.out.println("RowName Next "); showCell(result); } table.close(); close(); } public static void listTables() throws IOException { HTableDescriptor hTableDescriptors[] = admin.listTables(); for(HTableDescriptor hTableDescriptor :hTableDescriptors){ System.out.println(hTableDescriptor.getNameAsString()); } } public static void testInsert(){ try { insterRow("person", "lbr", "food", "breakfast", "milk and bread"); insterRow("person", "lbr", "food", "lunch", "noodle"); insterRow("person", "lbr", "traffic", "work", "car"); insterRow("person", "lbr", "traffic", "workoff", "Bicycle"); insterRow("person", "lbr", "work", "morning", "coding"); insterRow("person", "lbr", "work", "morning", "listening"); insterRow("person", "tjw", "food", "breakfast", "milk and bread"); insterRow("person", "tjw", "food", "lunch", "noodle"); insterRow("person", "tjw", "traffic", "work", "car"); insterRow("person", "tjw", "traffic", "workoff", "Bicycle"); insterRow("person", "tjw", "work", "morning", "coding"); insterRow("person", "tjw", "work", "morning", "listening"); } catch (IOException e) { e.printStackTrace(); } } //初始化链接 public static void init(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","10.154.2.192,10.154.2.193,10.154.2.194"); configuration.set("hbase.zookeeper.property.clientPort","2181"); configuration.set("zookeeper.znode.parent","/hbase-unsecure"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } //建表 public static void createTable(String tableNmae,String[] cols) throws IOException { TableName tableName = TableName.valueOf(tableNmae); if(admin.tableExists(tableName)){ System.out.println("talbe is exists!"); }else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for(String col:cols){ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); } close(); } //插入数据 public static void insterRow(String tableName,String rowkey, String colFamily, String col, String val) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val)); table.put(put); //批量插入 /* List<Put> putList = new ArrayList<Put>(); puts.add(put); table.put(putList);*/ table.close(); } //关闭连接 public static void close(){ try { if(null != admin) admin.close(); if(null != connection) connection.close(); } catch (IOException e) { e.printStackTrace(); } } }
java 调用hbase的api进行表操作
最新推荐文章于 2024-03-12 17:08:44 发布