HBASE技术学习 https://www.itkc8.com
1、引入所需要Jar包以及hbase-site.xml
2、创建源文件
[java] view plaincopy
- package hbase;
- /**
- *
- */
- import java.io.IOException;
- 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.client.Get;
- 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;
- public class HBaseTestCase {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String tableName = "test";
- String columnFamily = "cf";
- try {
- if (true == HBaseTestCase.delete(tableName)) {
- System.out.println("Delete Table " + tableName + " success!");
- }
- HBaseTestCase.create(tableName, columnFamily);
- HBaseTestCase.put(tableName, "row1", columnFamily, "column1",
- "data1");
- HBaseTestCase.put(tableName, "row2", columnFamily, "column2",
- "data2");
- HBaseTestCase.put(tableName, "row3", columnFamily, "column3",
- "data3");
- HBaseTestCase.put(tableName, "row4", columnFamily, "column4",
- "data4");
- HBaseTestCase.put(tableName, "row5", columnFamily, "column5",
- "data5");
- HBaseTestCase.get(tableName, "row1");
- HBaseTestCase.scan(tableName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static Configuration cfg = HBaseConfiguration.create();
- static {
- System.out.println(cfg.get("hbase.master"));
- }
- public static void create(String tableName, String columnFamily)
- throws Exception {
- HBaseAdmin admin = new HBaseAdmin(cfg);
- if (admin.tableExists(tableName)) {
- System.out.println(tableName + " exists!");
- } else {
- HTableDescriptor tableDesc = new HTableDescriptor(tableName);
- tableDesc.addFamily(new HColumnDescriptor(columnFamily));
- admin.createTable(tableDesc);
- System.out.println(tableName + " create successfully!");
- }
- }
- public static void put(String tablename, String row, String columnFamily,
- String column, String data) throws Exception {
- HTable table = new HTable(cfg, tablename);
- Put put = new Put(Bytes.toBytes(row));
- put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
- Bytes.toBytes(data));
- table.put(put);
- System.out.println("put '" + row + "', '" + columnFamily + ":" + column
- + "', '" + data + "'");
- }
- public static void get(String tablename, String row) throws Exception {
- HTable table = new HTable(cfg, tablename);
- Get get = new Get(Bytes.toBytes(row));
- Result result = table.get(get);
- System.out.println("Get: " + result);
- }
- public static void scan(String tableName) throws Exception {
- HTable table = new HTable(cfg, tableName);
- Scan s = new Scan();
- ResultScanner rs = table.getScanner(s);
- for (Result r : rs) {
- System.out.println("Scan: " + r);
- }
- }
- public static boolean delete(String tableName) throws IOException {
- HBaseAdmin admin = new HBaseAdmin(cfg);
- if (admin.tableExists(tableName)) {
- try {
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- }
3、执行结果
[plain] view plaincopy
- null
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:host.name=lenovo-PC
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_43
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.home=C:\Program Files\Java\jdk1.6.0_43\jre
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.class.path=E:\Work\workspace\hbase\bin;E:\Work\cygwin\usr\local\hbase-0.94.20\hbase-0.94.20.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\protobuf-java-2.4.0a.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\commons-io-2.1.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\commons-lang-2.5.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\commons-logging-1.1.1.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\commons-configuration-1.6.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\jackson-core-asl-1.8.8.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\jackson-jaxrs-1.8.8.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\jackson-mapper-asl-1.8.8.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\jackson-xc-1.8.8.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\log4j-1.2.16.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\slf4j-api-1.4.3.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\slf4j-log4j12-1.4.3.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\zookeeper-3.4.5.jar;E:\Work\cygwin\usr\local\hbase-0.94.20\lib\hadoop-core-1.0.4.jar
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.library.path=C:\Program Files\Java\jdk1.6.0_43\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Program Files\Java\jdk1.6.0_43\bin;C:\Program Files\IDM Computer Solutions\UltraEdit-32;C:\Program Files\IDM Computer Solutions\UltraEdit-32\;E:\Work\eclipse;;.
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\lenovo\AppData\Local\Temp\
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 7
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:os.arch=x86
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:os.version=6.1
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:user.name=lenovo
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\lenovo
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\Work\workspace\hbase
- 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=180000 watcher=hconnection
- 14/07/06 14:13:01 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC
- 14/07/06 14:13:01 INFO zookeeper.ClientCnxn: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
- 14/07/06 14:13:01 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 14/07/06 14:13:01 INFO zookeeper.ClientCnxn: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1470a3fadc20004, negotiated timeout = 180000
- 14/07/06 14:13:02 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=180000 watcher=catalogtracker-on-org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation@ba8602
- 14/07/06 14:13:02 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1470a3fadc20005, negotiated timeout = 180000
- 14/07/06 14:13:02 INFO zookeeper.ZooKeeper: Session: 0x1470a3fadc20005 closed
- Delete Table test success!
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: EventThread shut down
- 14/07/06 14:13:02 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=180000 watcher=catalogtracker-on-org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation@ba8602
- 14/07/06 14:13:02 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1470a3fadc20006, negotiated timeout = 180000
- 14/07/06 14:13:02 INFO zookeeper.ZooKeeper: Session: 0x1470a3fadc20006 closed
- 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: EventThread shut down
- 14/07/06 14:13:03 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=180000 watcher=catalogtracker-on-org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation@ba8602
- 14/07/06 14:13:03 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC
- 14/07/06 14:13:03 INFO zookeeper.ClientCnxn: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
- 14/07/06 14:13:03 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 14/07/06 14:13:04 INFO zookeeper.ClientCnxn: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1470a3fadc20007, negotiated timeout = 180000
- 14/07/06 14:13:04 INFO zookeeper.ZooKeeper: Session: 0x1470a3fadc20007 closed
- 14/07/06 14:13:04 INFO zookeeper.ClientCnxn: EventThread shut down
- test create successfully!
- put 'row1', 'cf:column1', 'data1'
- put 'row2', 'cf:column2', 'data2'
- put 'row3', 'cf:column3', 'data3'
- put 'row4', 'cf:column4', 'data4'
- put 'row5', 'cf:column5', 'data5'
- Get: keyvalues={row1/cf:column1/1404627184157/Put/vlen=5/ts=0}
- Scan: keyvalues={row1/cf:column1/1404627184157/Put/vlen=5/ts=0}
- Scan: keyvalues={row2/cf:column2/1404627184174/Put/vlen=5/ts=0}
- Scan: keyvalues={row3/cf:column3/1404627184183/Put/vlen=5/ts=0}
- Scan: keyvalues={row4/cf:column4/1404627184186/Put/vlen=5/ts=0}
- Scan: keyvalues={row5/cf:column5/1404627184189/Put/vlen=5/ts=0}
HBASE技术学习 https://www.itkc8.com