java API进行hbase操作

    技术2026-04-08  5

    判断表是否存在

    //表是否存在 import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HBaseAdmin; import java.io.IOException; public class Hbase { public static boolean isExist(String tableName) throws IOException { HBaseConfiguration configuration=new HBaseConfiguration(); configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03"); HBaseAdmin hBaseAdmin=new HBaseAdmin(configuration); boolean exist=hBaseAdmin.tableExists(tableName); hBaseAdmin.close(); return exist; } public static void main(String[] args) throws IOException{ System.out.println("aaaaaaaaa"); System.out.println(isExist("mytable2")); System.out.println("bbb"); } }

    创建表

    //创建表 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HConnection; import org.apache.hadoop.hbase.client.HConnectionManager; import org.junit.Before; import java.io.IOException; public class HbaseTest { public static void main(String[] args) { String createTableName = "mytable2"; Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "centos01,centos02,centos03"); //configuration.set("hbase.master", "192.168.31.128:600000"); System.out.println("start create table ......"); try { HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration); HTableDescriptor tableDescriptor = new HTableDescriptor(createTableName); tableDescriptor.addFamily(new HColumnDescriptor("column1")); tableDescriptor.addFamily(new HColumnDescriptor("column2")); tableDescriptor.addFamily(new HColumnDescriptor("column3")); hBaseAdmin.createTable(tableDescriptor); hBaseAdmin.close(); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("end create table ......"); } }

    插入数据

    //插入数据 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; 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.util.Bytes; import java.io.IOException; public class HbaseDataAdd { public static void main(String[] args) { String tablename = "mytable2"; Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03"); HBaseAdmin admin; try { admin = new HBaseAdmin(configuration); if (admin.tableExists(tablename)){ HTable table = new HTable(configuration,tablename); Put put = new Put(Bytes.toBytes("8")); put.add(Bytes.toBytes("column1"),Bytes.toBytes("name"),Bytes.toBytes("xiaoming")); table.put(put); System.out.println("add success!"); }else { System.out.println(tablename+" does not exist!"); } } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

    获取表中的数据

    //读取表中的数据 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.yecht.Data; import java.io.IOException; public class HbaseAddData { public static void main(String[] args) { String tablename = "mytable2"; Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03"); HTable table; try { table = new HTable(configuration,tablename); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); for (Result result:results){ for (Cell cell:result.rawCells()){ 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))+" "); } } } catch (IOException e) { e.printStackTrace(); } } }

    读一行数据

    //读一行数据 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; import org.yecht.Data; import java.io.IOException; public class HbaseGetData { public static void main(String[] args) { String tablename = "mytable2"; Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03"); HTable table; try { table = new HTable(configuration,tablename); Get get = new Get(Bytes.toBytes("1")); Result result = table.get(get); for (Cell cell:result.rawCells()){ 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))+" "); } } catch (IOException e) { e.printStackTrace(); } } }

    删除数据

    //删除数据 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HbaseDataDelete { public static void main(String[] args) { String tablename = "mytable2"; Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "centos01,centos02,centos03"); HBaseAdmin admin; try { admin = new HBaseAdmin(configuration); if (admin.tableExists(tablename)){ HTable table = new HTable(configuration,tablename); Delete delete = new Delete(Bytes.toBytes("1")); table.delete(delete); System.out.println("delete success!"); } else { System.out.println("Table does not exist"); } } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

    删除单行数据

    //删除单行数据 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; import org.yecht.Data; import java.io.IOException; public class HbaseDeleteData { public static void main(String[] args) { String tableName = "mytable2"; Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "centos01,centos02,centos03"); HBaseAdmin admin; try { admin = new HBaseAdmin(configuration); if (admin.tableExists(tableName)){ HTable table = new HTable(configuration,tableName); Delete delete = new Delete(Bytes.toBytes(2)); delete.deleteColumn(Bytes.toBytes("info"),Bytes.toBytes("age")); table.delete(delete); System.out.println("delete success"); }else { System.out.println("Table does not exist"); } } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

    注:在运行程序前需打开hdfs、zookeeper、hbase,并关闭防火墙,对本地的hosts(C:\Windows\System32\drivers\etc\hosts)进行配置,在hosts文件后边加上虚拟机ip及名称,然后在项目中引入hbase客户端的moven依赖。 此项目我加入的依赖是

    <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> </dependencies>
    Processed: 0.014, SQL: 9