Java连接HBASE数据库

HBASE技术学习  https://www.itkc8.com   

1、引入所需要Jar包以及hbase-site.xml

2、创建源文件

 

[java] view plaincopy

  1. package hbase;  
  2.   
  3. /** 
  4.  *  
  5.  */  
  6.   
  7. import java.io.IOException;  
  8.   
  9. import org.apache.hadoop.conf.Configuration;  
  10. import org.apache.hadoop.hbase.HBaseConfiguration;  
  11. import org.apache.hadoop.hbase.HColumnDescriptor;  
  12. import org.apache.hadoop.hbase.HTableDescriptor;  
  13. import org.apache.hadoop.hbase.client.Get;  
  14. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  15. import org.apache.hadoop.hbase.client.HTable;  
  16. import org.apache.hadoop.hbase.client.Put;  
  17. import org.apache.hadoop.hbase.client.Result;  
  18. import org.apache.hadoop.hbase.client.ResultScanner;  
  19. import org.apache.hadoop.hbase.client.Scan;  
  20. import org.apache.hadoop.hbase.util.Bytes;  
  21.   
  22. public class HBaseTestCase {  
  23.   
  24.     /** 
  25.      * @param args 
  26.      */  
  27.     public static void main(String[] args) {  
  28.         // TODO Auto-generated method stub  
  29.         String tableName = "test";  
  30.         String columnFamily = "cf";  
  31.         try {  
  32.   
  33.             if (true == HBaseTestCase.delete(tableName)) {  
  34.                 System.out.println("Delete Table " + tableName + " success!");  
  35.   
  36.             }  
  37.   
  38.             HBaseTestCase.create(tableName, columnFamily);  
  39.             HBaseTestCase.put(tableName, "row1", columnFamily, "column1",  
  40.                     "data1");  
  41.             HBaseTestCase.put(tableName, "row2", columnFamily, "column2",  
  42.                     "data2");  
  43.             HBaseTestCase.put(tableName, "row3", columnFamily, "column3",  
  44.                     "data3");  
  45.             HBaseTestCase.put(tableName, "row4", columnFamily, "column4",  
  46.                     "data4");  
  47.             HBaseTestCase.put(tableName, "row5", columnFamily, "column5",  
  48.                     "data5");  
  49.   
  50.             HBaseTestCase.get(tableName, "row1");  
  51.   
  52.             HBaseTestCase.scan(tableName);  
  53.   
  54.         } catch (Exception e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
  58.   
  59.     static Configuration cfg = HBaseConfiguration.create();  
  60.     static {  
  61.         System.out.println(cfg.get("hbase.master"));  
  62.     }  
  63.   
  64.     public static void create(String tableName, String columnFamily)  
  65.             throws Exception {  
  66.         HBaseAdmin admin = new HBaseAdmin(cfg);  
  67.         if (admin.tableExists(tableName)) {  
  68.             System.out.println(tableName + " exists!");  
  69.         } else {  
  70.             HTableDescriptor tableDesc = new HTableDescriptor(tableName);  
  71.             tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
  72.             admin.createTable(tableDesc);  
  73.             System.out.println(tableName + " create successfully!");  
  74.         }  
  75.     }  
  76.   
  77.     public static void put(String tablename, String row, String columnFamily,  
  78.             String column, String data) throws Exception {  
  79.   
  80.         HTable table = new HTable(cfg, tablename);  
  81.         Put put = new Put(Bytes.toBytes(row));  
  82.   
  83.         put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),  
  84.                 Bytes.toBytes(data));  
  85.   
  86.         table.put(put);  
  87.   
  88.         System.out.println("put '" + row + "', '" + columnFamily + ":" + column  
  89.                 + "', '" + data + "'");  
  90.   
  91.     }  
  92.   
  93.     public static void get(String tablename, String row) throws Exception {  
  94.         HTable table = new HTable(cfg, tablename);  
  95.         Get get = new Get(Bytes.toBytes(row));  
  96.         Result result = table.get(get);  
  97.         System.out.println("Get: " + result);  
  98.     }  
  99.   
  100.     public static void scan(String tableName) throws Exception {  
  101.   
  102.         HTable table = new HTable(cfg, tableName);  
  103.         Scan s = new Scan();  
  104.         ResultScanner rs = table.getScanner(s);  
  105.   
  106.         for (Result r : rs) {  
  107.             System.out.println("Scan: " + r);  
  108.   
  109.         }  
  110.     }  
  111.   
  112.     public static boolean delete(String tableName) throws IOException {  
  113.   
  114.         HBaseAdmin admin = new HBaseAdmin(cfg);  
  115.         if (admin.tableExists(tableName)) {  
  116.             try {  
  117.                 admin.disableTable(tableName);  
  118.                 admin.deleteTable(tableName);  
  119.             } catch (Exception e) {  
  120.                 e.printStackTrace();  
  121.                 return false;  
  122.             }  
  123.         }  
  124.         return true;  
  125.     }  
  126. }  


3、执行结果

 

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. null  
  2. 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  
  3. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:host.name=lenovo-PC  
  4. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_43  
  5. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.  
  6. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.home=C:\Program Files\Java\jdk1.6.0_43\jre  
  7. 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  
  8. 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;;.  
  9. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\lenovo\AppData\Local\Temp\  
  10. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>  
  11. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 7  
  12. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:os.arch=x86  
  13. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:os.version=6.1  
  14. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:user.name=lenovo  
  15. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\lenovo  
  16. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\Work\workspace\hbase  
  17. 14/07/06 14:13:01 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=180000 watcher=hconnection  
  18. 14/07/06 14:13:01 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC  
  19. 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 (无法定位登录配置)  
  20. 14/07/06 14:13:01 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session  
  21. 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  
  22. 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  
  23. 14/07/06 14:13:02 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC  
  24. 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 (无法定位登录配置)  
  25. 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session  
  26. 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  
  27. 14/07/06 14:13:02 INFO zookeeper.ZooKeeper: Session: 0x1470a3fadc20005 closed  
  28. Delete Table test success!  
  29. 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: EventThread shut down  
  30. 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  
  31. 14/07/06 14:13:02 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC  
  32. 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 (无法定位登录配置)  
  33. 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session  
  34. 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  
  35. 14/07/06 14:13:02 INFO zookeeper.ZooKeeper: Session: 0x1470a3fadc20006 closed  
  36. 14/07/06 14:13:02 INFO zookeeper.ClientCnxn: EventThread shut down  
  37. 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  
  38. 14/07/06 14:13:03 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6436@lenovo-PC  
  39. 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 (无法定位登录配置)  
  40. 14/07/06 14:13:03 INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session  
  41. 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  
  42. 14/07/06 14:13:04 INFO zookeeper.ZooKeeper: Session: 0x1470a3fadc20007 closed  
  43. 14/07/06 14:13:04 INFO zookeeper.ClientCnxn: EventThread shut down  
  44. test create successfully!  
  45. put 'row1', 'cf:column1', 'data1'  
  46. put 'row2', 'cf:column2', 'data2'  
  47. put 'row3', 'cf:column3', 'data3'  
  48. put 'row4', 'cf:column4', 'data4'  
  49. put 'row5', 'cf:column5', 'data5'  
  50. Get: keyvalues={row1/cf:column1/1404627184157/Put/vlen=5/ts=0}  
  51. Scan: keyvalues={row1/cf:column1/1404627184157/Put/vlen=5/ts=0}  
  52. Scan: keyvalues={row2/cf:column2/1404627184174/Put/vlen=5/ts=0}  
  53. Scan: keyvalues={row3/cf:column3/1404627184183/Put/vlen=5/ts=0}  
  54. Scan: keyvalues={row4/cf:column4/1404627184186/Put/vlen=5/ts=0}  
  55. Scan: keyvalues={row5/cf:column5/1404627184189/Put/vlen=5/ts=0}  

HBASE技术学习  https://www.itkc8.com   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于在HBase数据库中使用Java创建表的方法如下: 首先,需要导入HBaseJava API相关的包: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.TableDescriptor; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.io.compress.CompressionAlgorithm; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; ``` 然后,通过以下代码建立与HBase数据库连接: ```java Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin(); ``` 其中,"localhost"为HBase数据库所在的主机名。接下来,可以使用以下代码创建表: ```java String tableName = "myTable"; TableName name = TableName.valueOf(tableName); TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(name) .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf")) .setCompressionType(CompressionAlgorithm.GZ) .setBloomFilterType(BloomType.ROWCOL) .build()) .build(); admin.createTable(tableDescriptor); ``` 这里创建了一个名为"myTable"的表,并设置了一个名为"cf"的列族,其中设置了压缩算法和Bloom过滤器类型。最后,使用admin.createTable()方法创建表。 需要注意的是,在使用完后需要关闭与HBase数据库连接: ```java admin.close(); connection.close(); ``` 以上就是使用JavaHBase数据库中创建表的方法,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值