1.HBASE基于Java开发

使用Java操作HBASE(增删查改)

  1. package com.chu;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.HColumnDescriptor;
  8. import org.apache.hadoop.hbase.HTableDescriptor;
  9. import org.apache.hadoop.hbase.KeyValue;
  10. import org.apache.hadoop.hbase.MasterNotRunningException;
  11. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  12. import org.apache.hadoop.hbase.client.Delete;
  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.HTablePool;
  17. import org.apache.hadoop.hbase.client.Put;
  18. import org.apache.hadoop.hbase.client.Result;
  19. import org.apache.hadoop.hbase.client.ResultScanner;
  20. import org.apache.hadoop.hbase.client.Scan;
  21. import org.apache.hadoop.hbase.filter.Filter;
  22. import org.apache.hadoop.hbase.filter.FilterList;
  23. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
  24. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
  25. import org.apache.hadoop.hbase.util.Bytes;
  26. public class CreateHBaseTableDemo
  27. {
  28. public static Configuration configuration;
  29. static
  30. {
  31. configuration = HBaseConfiguration.create();
  32. configuration.set("hbase.zookeeper.property.clientPort", "2181");
  33. configuration.set("hbase.zookeeper.quorum", "192.168.1.159");
  34. configuration.set("hbase.master", "192.168.1.159:600000");
  35. }
  36. public static void main(String[] args)
  37. {
  38. //createTable("HBaseDemoTable1");
  39. //insertData("HBaseDemoTable1");
  40. //QueryAll("HBaseDemoTable1");
  41. //QueryByCondition1("HBaseDemoTable1");
  42. //QueryByCondition2("HBaseDemoTable1");
  43. QueryByCondition3("HBaseDemoTable1");
  44. //deleteRow("wujintao","abcdef");
  45. //deleteByCondition("wujintao", "abcdef");
  46. }
  47. /**
  48. * 创建表
  49. *
  50. * @param tableName
  51. */
  52. public static void createTable(String tableName)
  53. {
  54. System.out.println("start create table ......");
  55. try
  56. {
  57. HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
  58. if (hBaseAdmin.tableExists(tableName))
  59. {
  60. // 如果存在要创建的表,那么先删除,再创建
  61. hBaseAdmin.disableTable(tableName);
  62. hBaseAdmin.deleteTable(tableName);
  63. System.out.println(tableName + " is exist,detele....");
  64. }
  65. HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
  66. tableDescriptor.addFamily(new HColumnDescriptor("column1"));
  67. tableDescriptor.addFamily(new HColumnDescriptor("column2"));
  68. tableDescriptor.addFamily(new HColumnDescriptor("column3"));
  69. hBaseAdmin.createTable(tableDescriptor);
  70. } catch (MasterNotRunningException e)
  71. {
  72. e.printStackTrace();
  73. } catch (ZooKeeperConnectionException e)
  74. {
  75. e.printStackTrace();
  76. } catch (IOException e)
  77. {
  78. e.printStackTrace();
  79. }
  80. System.out.println("end create table ......");
  81. }
  82. /**
  83. * 插入数据
  84. *
  85. * @param tableName
  86. */
  87. public static void insertData(String tableName) {
  88. System.out.println("start insert data ......");
  89. try
  90. {
  91. HTable table = new HTable(configuration, tableName);
  92. Put put = new Put(Bytes.toBytes("45678hyhyjju"));
  93. put.add(Bytes.toBytes("column1"),
  94. Bytes.toBytes("email"),
  95. Bytes.toBytes("5211486@qq.com"));
  96. table.put(put);// 放入表
  97. table.close();// 释放资源
  98. } catch (IOException e)
  99. {
  100. e.printStackTrace();
  101. }
  102. System.out.println("end insert data ......");
  103. }
  104. /**
  105. * 删除一张表
  106. *
  107. * @param tableName
  108. */
  109. public static void dropTable(String tableName) {
  110. try {
  111. HBaseAdmin admin = new HBaseAdmin(configuration);
  112. admin.disableTable(tableName);
  113. admin.deleteTable(tableName);
  114. } catch (MasterNotRunningException e) {
  115. e.printStackTrace();
  116. } catch (ZooKeeperConnectionException e) {
  117. e.printStackTrace();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. /**
  123. * 根据 rowkey删除一条记录
  124. *
  125. * @param tablename
  126. * @param rowkey
  127. */
  128. public static void deleteRow(String tablename, String rowkey) {
  129. try {
  130. HTable table = new HTable(configuration, tablename);
  131. List list = new ArrayList();
  132. Delete d1 = new Delete(rowkey.getBytes());
  133. list.add(d1);
  134. table.delete(list);
  135. System.out.println("删除行成功!");
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. /**
  141. * 组合条件删除
  142. *
  143. * @param tablename
  144. * @param rowkey
  145. */
  146. public static void deleteByCondition(String tablename, String rowkey) {
  147. //目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作
  148. }
  149. /**
  150. * 查询所有数据
  151. *
  152. * @param tableName
  153. */
  154. public static void QueryAll(String tableName) {
  155. try
  156. {
  157. HTable table = new HTable(configuration, tableName);
  158. ResultScanner rs = table.getScanner(new Scan());
  159. for (Result r : rs)
  160. {
  161. System.out.println("获得到rowkey:" + new String(r.getRow()));
  162. for (KeyValue keyValue : r.raw())
  163. {
  164. System.out.println("列:" + new String(keyValue.getFamily())
  165. + "====值:" + new String(keyValue.getValue()));
  166. }
  167. }
  168. }
  169. catch (IOException e)
  170. {
  171. e.printStackTrace();
  172. }
  173. }
  174. /**
  175. * 单条件查询,根据rowkey查询唯一一条记录
  176. *
  177. * @param tableName
  178. */
  179. public static void QueryByCondition1(String tableName) {
  180. try {
  181. HTable table = new HTable(configuration, tableName);
  182. Get scan = new Get("45678hyhyjju".getBytes());// 根据rowkey查询
  183. Result r = table.get(scan);
  184. System.out.println("获得到rowkey:" + new String(r.getRow()));
  185. for (KeyValue keyValue : r.raw()) {
  186. System.out.println("列:" + new String(keyValue.getFamily())
  187. + "====值:" + new String(keyValue.getValue()));
  188. }
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. /**
  194. * 单条件按查询,查询多条记录
  195. *
  196. * @param tableName
  197. */
  198. public static void QueryByCondition2(String tableName) {
  199. try {
  200. HTable table = new HTable(configuration, tableName);
  201. Filter filter = new SingleColumnValueFilter(Bytes
  202. .toBytes("column1"), null, CompareOp.EQUAL, Bytes
  203. .toBytes("123456")); // 当列column1的值为aaa时进行查询
  204. Scan s = new Scan();
  205. s.setFilter(filter);
  206. ResultScanner rs = table.getScanner(s);
  207. for (Result r : rs) {
  208. System.out.println("获得到rowkey:" + new String(r.getRow()));
  209. for (KeyValue keyValue : r.raw()) {
  210. System.out.println("列:" + new String(keyValue.getFamily())
  211. + "====值:" + new String(keyValue.getValue()));
  212. }
  213. }
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. /**
  219. * 组合条件查询
  220. *
  221. * @param tableName
  222. */
  223. public static void QueryByCondition3(String tableName) {
  224. try {
  225. HTable table = new HTable(configuration, tableName);
  226. List<Filter> filters = new ArrayList<Filter>();
  227. Filter filter1 = new SingleColumnValueFilter(Bytes
  228. .toBytes("column1"), null, CompareOp.EQUAL, Bytes
  229. .toBytes("123456"));
  230. filters.add(filter1);
  231. Filter filter2 = new SingleColumnValueFilter(Bytes
  232. .toBytes("column2"), null, CompareOp.EQUAL, Bytes
  233. .toBytes("123456"));
  234. filters.add(filter2);
  235. Filter filter3 = new SingleColumnValueFilter(Bytes
  236. .toBytes("column3"), null, CompareOp.EQUAL, Bytes
  237. .toBytes("c2g111"));
  238. filters.add(filter3);
  239. FilterList filterList1 = new FilterList(filters);
  240. Scan scan = new Scan();
  241. scan.setFilter(filterList1);
  242. ResultScanner rs = table.getScanner(scan);
  243. for (Result r : rs) {
  244. System.out.println("获得到rowkey:" + new String(r.getRow()));
  245. for (KeyValue keyValue : r.raw()) {
  246. System.out.println("列:" + new String(keyValue.getFamily())
  247. + "====值:" + new String(keyValue.getValue()));
  248. }
  249. }
  250. rs.close();
  251. } catch (Exception e) {
  252. e.printStackTrace();
  253. }
  254. }
  255. }

MAVEN库

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.chu</groupId>
  7. <artifactId>HBaseDemo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
  11. <dependency>
  12. <groupId>org.apache.hive</groupId>
  13. <artifactId>hive-jdbc</artifactId>
  14. <version>2.1.0</version>
  15. </dependency>
  16. <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
  17. <dependency>
  18. <groupId>org.hibernate.javax.persistence</groupId>
  19. <artifactId>hibernate-jpa-2.0-api</artifactId>
  20. <version>1.0.1.Final</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.hive</groupId>
  24. <artifactId>hive-exec</artifactId>
  25. <version>2.1.0</version>
  26. </dependency>
  27. <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
  28. <dependency>
  29. <groupId>org.apache.hbase</groupId>
  30. <artifactId>hbase-client</artifactId>
  31. <version>1.1.5</version>
  32. </dependency>
  33. <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase -->
  34. <dependency>
  35. <groupId>org.apache.hbase</groupId>
  36. <artifactId>hbase</artifactId>
  37. <version>1.1.5</version>
  38. <type>pom</type>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
  41. <dependency>
  42. <groupId>org.apache.hbase</groupId>
  43. <artifactId>hbase-common</artifactId>
  44. <version>1.1.5</version>
  45. </dependency>
  46. <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
  47. <dependency>
  48. <groupId>org.apache.hbase</groupId>
  49. <artifactId>hbase-server</artifactId>
  50. <version>1.1.5</version>
  51. </dependency>
  52. <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-protocol -->
  53. <dependency>
  54. <groupId>org.apache.hbase</groupId>
  55. <artifactId>hbase-protocol</artifactId>
  56. <version>1.1.5</version>
  57. </dependency>
  58. </dependencies>
  59. <repositories>
  60. <repository>
  61. <id>jboss</id>
  62. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  63. </repository>
  64. </repositories>
  65. </project>