原创

Java对Hbase进行CRUD操作--跟我一起学Hbase

一、目标 实现java访问hbase,并对其进行操作 二、准备环境 搭建hadoop集群环境 搭建hdfs集群环境 搭建hbase集群环境 安装zookeeper环境 三、Java代码
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.amssy</groupId> <artifactId>Hbase_Test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Hbase</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-core</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.apache.hadoop.hbase</groupId> <artifactId>hbase</artifactId> <version>0.94.24</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.4.0a</version> </dependency> </dependencies> </project>
 
/** * 创建表操作 * * @throws IOException */ public static void createTable(String tablename, String[] cfs) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tablename)) { System.out.println("表已经存在!"); } else { HTableDescriptor tableDesc = new HTableDescriptor(tablename); for (int i = 0; i < cfs.length; i++) { tableDesc.addFamily(new HColumnDescriptor(cfs[i])); } admin.createTable(tableDesc); System.out.println("表创建成功!"); } }
/** * 删除表操作 * * @param tablename * @throws IOException */ public void deleteTable(String tablename) throws IOException { try { HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tablename); admin.deleteTable(tablename); System.out.println("表删除成功!"); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } }
/** * 插入一行记录 * * @param tablename * @param cfs */ public static void writeRow(String tablename, String[] cfs) { try { HTable table = new HTable(conf, tablename); Put put = new Put(Bytes.toBytes("rows1")); for (int j = 0; j < cfs.length; j++) { put.add(Bytes.toBytes(cfs[j]), Bytes.toBytes(String.valueOf(j)), Bytes.toBytes("value_1")); table.put(put); System.out.println(put.getFamilyMap().toString()); System.out.println(new String(put.getRow()).toString()); } } catch (IOException e) { e.printStackTrace(); } }
/** * 删除一行记录 * * @param tablename * @param rowkey * @throws IOException */ public void deleteRow(String tablename, String rowkey) throws IOException { HTable table = new HTable(conf, tablename); List list = new ArrayList(); Delete d1 = new Delete(rowkey.getBytes()); list.add(d1); table.delete(list); System.out.println("删除行成功!"); }
/** * 查找一行记录 * * @param tablename * @param rowkey */ public static void selectRow(String tablename, String rowKey) throws IOException { HTable table = new HTable(conf, tablename); Get g = new Get(rowKey.getBytes()); Result rs = table.get(g); for (KeyValue kv : rs.raw()) { System.out.print(new String(kv.getRow()) + ""); System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + ""); System.out.print(kv.getTimestamp() + ""); System.out.println(new String(kv.getValue())); } }
/** * 查询表中所有行 * * @param tablename */ public void scaner(String tablename) { try { HTable table = new HTable(conf, tablename); Scan s = new Scan(); ResultScanner rs = table.getScanner(s); for (Result r : rs) { KeyValue[] kv = r.raw(); for (int i = 0; i < kv.length; i++) { System.out.print(new String(kv[i].getRow()) + ""); System.out.print(new String(kv[i].getFamily()) + ":"); System.out.print(new String(kv[i].getQualifier()) + ""); System.out.print(kv[i].getTimestamp() + ""); System.out.println(new String(kv[i].getValue())); } } } catch (IOException e) { e.printStackTrace(); } }
java文件下载:HBaseTest.rar QQ图片20141104134655 四、常见问题 1.hbase-default.xml file seems to be for and old version of 解决办法:客户端保持服务端版本号一致   2.执行hbase创建表时找不到protobuf 解决:增加缺少的依赖包
<dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.4.0a</version> </dependency>
3.主机名找不到 解决办法:需要在hosts里面添加对应关系 192.168.1.202 nameNode 192.168.1.102 datanode1 192.168.1.205 datanode2 192.168.1.206 datanode3 192.168.1.201 datanode4 192.168.1.112 datanode5
正文到此结束
Loading...