Hbase操作:添加数据,查询数据,扫描数据,删除行,删除表
添加数据
public static void set() throws IOException { System.out.println("---------------------------set-----------------------"); Configuration config = HBaseConfiguration.create(); // 创建表简写 HTable table = new HTable(config, "test3"); // put对象实现添加数据,并指定row 参数为字节数组 Put put1 = new Put(Bytes.toBytes("row1")); // 添加列族,列,值 put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes(1), Bytes.toBytes("Hello1")); put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes(2), Bytes.toBytes("Hello2")); put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes(3), Bytes.toBytes("Hello3")); table.put(put1); Put put2 = new Put(Bytes.toBytes("row2")); // 添加列族,列,值 put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes(1), Bytes.toBytes("Hello1")); put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes(2), Bytes.toBytes("Hello2")); put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes(3), Bytes.toBytes("Hello3")); table.put(put2); table.close(); }
查询数据
public static void get() throws IOException { System.out.println("---------------------------get-----------------------"); // 创建Hbase的配置文件,是Hadoop的子类 Configuration config = HBaseConfiguration.create(); // 创建表简写 HTable table = new HTable(config, "test3"); // get对象实现查询数据,并指定row 参数为字节数组 ,封装查询 // 静态查询 Get get = new Get(Bytes.toBytes("row1")); // 添加列族,列,值 get.addColumn(Bytes.toBytes("data"), Bytes.toBytes(1)); get.addColumn(Bytes.toBytes("data"), Bytes.toBytes(2)); Result result = table.get(get); KeyValue kv = result.getColumnLatest(Bytes.toBytes("data"), Bytes.toBytes(1)); System.out.println(Bytes.toString(kv.getKey()) + "-------------" + kv.getValue()); table.close(); }
扫描数据
/** * 扫描表 * * @throws IOException */ public static void scan() throws IOException { System.out.println("---------------------------scan-----------------------"); // 创建Hbase的配置文件,是Hadoop的子类 Configuration config = HBaseConfiguration.create(); // 创建表简写 HTable table = new HTable(config, "test3"); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); for (Result result : rs) { System.out.println(Bytes.toString(result.getRow()) + ":" + Bytes.toShort(result.getColumnLatestCell(Bytes.toBytes("data"), Bytes.toBytes(1)).getValue())); } rs.close(); table.close(); }
删除行
/** * 删除行 * * @throws IOException */ public static void delete() throws IOException { System.out.println("---------------------------delete-----------------------"); // 创建Hbase的配置文件,是Hadoop的子类 Configuration config = HBaseConfiguration.create(); // 创建表简写 HTable table = new HTable(config, "test3"); Delete d = new Delete(Bytes.toBytes("row2")); table.delete(d); table.close(); }
删除表
/** * 删除表 * * @throws IOException */ public static void DropTable() throws IOException { System.out.println("---------------------------DropTable-----------------------"); // 创建Hbase的配置文件,是Hadoop的子类 Configuration config = HBaseConfiguration.create(); // 需要管理员 HBaseAdmin admin = new HBaseAdmin(config); admin.disableTable("test3"); admin.deleteTable("test3"); admin.close(); }
运行结果如下
[root@master local]# hbase com.xt.hbase.HbaseApp
---------------------------create-----------------------
2017-07-17 18:00:01,246 WARN [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/hbase/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2017-07-17 18:00:03,592 INFO [main] zookeeper.RecoverableZooKeeper: Process identifier=hconnection-0x2925bf5b connecting to ZooKeeper ensemble=slave1:2181,slave2:2181
2017-07-17 18:00:03,614 INFO [main] zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2017-07-17 18:00:03,614 INFO [main] zookeeper.ZooKeeper: Client environment:host.name=master
2017-07-17 18:00:03,614 INFO [main] zookeeper.ZooKeeper: Client environment:java.version=1.8.0_131
2017-07-17 18:00:03,614 INFO [main] zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
2017-07-17 18:00:03,614 INFO [main] zookeeper.ZooKeeper: Client environment:java.home=/usr/local/java/jre
2017-07-17 18:00:03,614 INFO [main] zookeeper.ZooKeeper: Client environment:java.class.path=/usr/local/hbase/conf:/usr/local/java/lib/tools.jar:/usr/local/hbase:/usr/local/hbase/lib/activation-1.1.jar:/usr/local/hbase/lib/aopalliance-1.0.jar:/usr/local/hbase/lib/apacheds-i18n-2.0.0-M15.jar:/usr/local/hbase/lib/apacheds-kerberos-codec-2.0.0-M15.jar:/usr/local/hbase/lib/api-asn1-api-1.0.0-M20.jar:/usr/local/hbase/lib/api-util-1.0.0-M20.jar:/usr/local/hbase/lib/asm-3.1.jar:/usr/local/hbase/lib/avro-1.7.4.jar:/usr/local/hbase/lib/commons-beanutils-1.7.0.jar:/usr/local/hbase/lib/commons-beanutils-core-1.8.0.jar:/usr/local/hbase/lib/commons-cli-1.2.jar:/usr/local/hbase/lib/commons-codec-1.9.jar:/usr/local/hbase/lib/commons-collections-3.2.2.jar:/usr/local/hbase/lib/commons-compress-1.4.1.jar:/usr/local/hbase/lib/commons-configuration-1.6.jar:/usr/local/hbase/lib/commons-daemon-1.0.13.jar:/usr/local/hbase/lib/commons-digester-1.8.jar:/usr/local/hbase/lib/commons-el-1.0.jar:/usr/local/hbase/lib/commons-httpclient-3.1.jar:/usr/local/hbase/lib/commons-io-2.4.jar:/usr/local/hbase/lib/commons-lang-2.6.jar:/usr/local/hbase/lib/commons-logging-1.2.jar:/usr/local/hbase/lib/commons-math-2.2.jar:/usr/local/hbase/lib/commons-math3-3.1.1.jar:/usr/local/hbase/lib/commons-net-3.1.jar:/usr/local/hbase/lib/disruptor-3.3.0.jar:/usr/local/hbase/lib/findbugs-annotations-1.3.9-1.jar:/usr/local/hbase/lib/guava-12.0.1.jar:/usr/local/hbase/lib/guice-3.0.jar:/usr/local/hbase/lib/guice-servlet-3.0.jar:/usr/local/hbase/lib/hadoop-anno