多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
使用Java对Hbase进行增、删、改、查,其中改就是增。<br/> **1. 随便创建一个Maven项目,然后添加下面的依赖** *`pom.xml`* ```xml <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <hadoop.version>2.6.0</hadoop.version> <hive.version>1.1.0</hive.version> <hbase.version>1.2.0</hbase.version> </properties> <repositories> <repository> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url> </repository> </repositories> <dependencies> <!--hadoop--> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>${hadoop.version}</version> </dependency> <!--日志--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!--MapReduce--> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-jobclient</artifactId> <version>${hadoop.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <!--zookeeper--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> <type>pom</type> </dependency> <!--hbase--> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>${hbase.version}</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!--<scope>test</scope>--> </dependency> </dependencies> ``` *`resource/log4j.properties`* ```xml log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n ``` **2. Java代码** *`hbase/HbaseApi.java`* ```java package hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; import java.io.IOException; public class HbaseApi { /** * ========== 连接到Hbase ========== */ public Configuration createConnection(){ Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop101"); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.master", "hadoop101:160000"); /* 也可以使用配置文件来代替上面的配置 conf.addResource(new Path("/opt/install/hbase/conf/hbase-site.xml")); conf.addResource(new Path("/opt/install/hadoop/etc/hadoop/core-site.xml")); */ return conf; } /** * ========== 建表 ========== */ @Test public void createTable() throws IOException { // 1. 创建连接 Connection conn = ConnectionFactory.createConnection(this.createConnection()); // 2. 创建admin Admin admin = conn.getAdmin(); // 3. 添加表相关信息 HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student")); // 表名 student.addFamily(new HColumnDescriptor("info")); // 列族 // student.addFamily(new HColumnDescriptor("score")); // 列族 // 4. 建表 admin.createTable(student); // 5. 关闭连接 conn.close(); } /** * ========== 添加数据 ========== */ @Test public void putDataToTable() throws IOException { // 1. 创建连接 Connection conn = ConnectionFactory.createConnection(this.createConnection()); // 2. 获取表 Table student = conn.getTable(TableName.valueOf("student")); // 3. 创建数据 Put put = new Put(Bytes.toBytes("r00001")); // row_key put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan")); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("male")); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(11)); // 4. 添加 student.put(put); // 5. 关闭连接 conn.close(); } /** * ========== 获取表数据 ========== */ @Test public void getDataFromTable() throws IOException{ // 1. 创建连接 Connection conn = ConnectionFactory.createConnection(this.createConnection()); // 2. 获取表 Table student = conn.getTable(TableName.valueOf("student")); // 3. 读取数据 Get get = new Get(Bytes.toBytes("r00001")); // 根据row_key来读取 Result result = student.get(get); Cell[] cells = result.rawCells(); for(Cell cell : cells) { System.out.println("row_key = " + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("列族 = " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列名 = " + Bytes.toString(CellUtil.cloneQualifier(cell))); // 需要注意的是在存储age时是int数据,当用toString读取时返回的是 该符号 // 应该使用Bytes.toInt(CellUtil.cloneValue(cell)) 读取age才正常看到效果 System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("-------------------"); } // 4. 关闭连接 conn.close(); } /** * ========== 删除表 ========== * 无论表是否为空都可以删除 */ @Test public void dropTable() throws IOException{ // 1. 创建连接 Connection conn = ConnectionFactory.createConnection(this.createConnection()); // 2. 获取Admin Admin admin = conn.getAdmin(); // 3. 禁用表 admin.disableTable(TableName.valueOf("student")); // 4. 删除表 admin.deleteTable(TableName.valueOf("student")); // 5. 关闭连接 conn.close(); } } ```