通过Java Api对Hbase进行操作
2016-11-03 22:37
375 查看
HBase提供了Java Api的访问接口,所以可以通过java实现HBase中的各种操作
表结构见上一个博客这里写链接内容
表结构见上一个博客这里写链接内容
package GID.AID; import java.io.IOException; import java.nio.ByteBuffer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; 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.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class HBase { private static Configuration conf = null; private static HBaseAdmin admin = null; private static Logger log = Logger.getLogger(App.class); private static String hTableName = "hbase_1102"; private static String[] families = {"cf1", "cf2"}; @SuppressWarnings("deprecation") public static void main( String[] args ) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ // 初始化log模块,这是hbase-client的依赖带来的额外工作 initLog(); // 设定conf,指定ZooKeeper的位置 conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "vm10-0-0-2.ksc.com"); // 建立一个角色,登陆到ZooKeeper上 admin = new HBaseAdmin(conf); log.info("Log in"); // Ex2 使用表存在、删除 doExercise2(); // Ex3 createTable doExercise3(); // Ex4读写数据 doExercise4(); //删除一条数据 doExercise5(); //查询所有数据 doExercise6(); // 关闭连接——很重要,不然服务会挂 admin.close(); } //查找所有数据 private static void doExercise6() throws IOException { //插入一条数据 if (tableExist(hTableName)){ getAllRow(hTableName); } } //删除一条数据 private static void doExercise5() throws IOException { //插入一条数据 if (tableExist(hTableName)){ deleteRow(hTableName, "003"); } } //添加一条数据 private static void doExercise4() throws IOException { //插入一条数据 if (tableExist(hTableName)){ createData(hTableName, "003", "cf1", "name", "Curry"); } } private static void doExercise2() throws IOException { Boolean bExist = admin.tableExists(hTableName); log.info("Table Exists? " + bExist); if (true == bExist){ admin.disableTable(hTableName); admin.deleteTable(hTableName); } } private static void doExercise3() throws IOException{ if (tableExist(hTableName)){ return; } createTable(hTableName, families); } private static boolean tableExist(String tableName) throws IOException{ return admin.tableExists(hTableName); } private static void createTable(String tableName, String[] families) throws IOException { @SuppressWarnings("deprecation") HTableDescriptor desc = new HTableDescriptor("hbase_1102_chenweican"); for (int iFamily = 0; iFamily < families.length; iFamily++){ HColumnDescriptor columnFamilyDesc = new HColumnDescriptor(families[iFamily]); desc.addFamily(columnFamilyDesc); } admin.createTable(desc); } //添加一条数据 private static void createData(String tableName,String rowKey,String columnFamily, String column, String value) throws IOException{ Put put = new Put(Bytes.toBytes(rowKey)); HTable table = new HTable(conf, tableName); put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); } //删除一条数据 private static void deleteRow(String tableName,String rowKey) throws IOException{ HTable table = new HTable(conf, tableName); Delete del = new Delete(Bytes.toBytes(rowKey)); table.delete(del); } private static void getAllRow(String tableName) throws IOException { HTable table = new HTable(conf, tableName); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); for(Result result:results){ for(KeyValue rowKV : result.raw()){ System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("时间戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out.print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } } private static void initLog(){ BasicConfigurator.configure(); log.setLevel(Level.INFO); } }
相关文章推荐
- 使用Java API对HBase进行操作
- 使用Java Api 对HBase进行简单操作
- [置顶] Java API连接HBase 进行增删查改操作
- 使用Java API对HBase进行CRUD操作
- 使用Java Api 对HBase进行简单操作
- 通过ICE接口对hbase进行文件操作
- eclipse下使用java api 进行hbase的常用的操作所需的jar包,以及如何查看java build path下的jar是否使用
- Window xp + VMWare + Win7通过VMWare Tools 进行共享(操作步骤)
- 目前比较流行的ASP木马主要通过三种技术来进行对服务器的相关操作
- InstallShield安装打包编译自动化(1) - 通过命令行进行源代码控制操作
- QTP设置检查点,通过判断检查点checkpoint的值进行相应操作
- 第2章_基本数据类型和基本操作_编程练习Exercise2.7通过创建窗口进行大小写的切换
- VB中通过oo4o进行Oracle数据库操作-Update Delete Insert
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- Spring中使用getSession()与通过HibernateTemplate进行数据操作的差别
- 使用黑莓8900通过蓝牙连接华为交换机进行现场网络配置与操作(一)
- 通过Socket进行HttP/HTTPS网页操作
- java平台下通过jacob对excel,word进行打印等操作
- 通过进程ID查看进程信息,调出进程线程集合,查看进程模块,并对其进行操作!
- DELPHI通过ACTIVESYNC连接到移动设备,并进行文件的复制,删除等操作!