2 需修改windows的host地址,在hosts文件下添加 hdp3.site 192.168.×.× hdp2.site 192.168.×.× hdp1.site 192.168.×.× hdp3.site,hdp2.site,hdp1.site为hbase对应的hadoop集群的host
3 具体代码
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Delete; 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; /** * HBase 工具类 */ @SuppressWarnings("deprecation") public class HBaseUtil { private static HBaseConfiguration conf = null; static { conf = new HBaseConfiguration(); conf.addResource("hbase-site.xml"); } /** * 建立表格 * * @param tableName * @param family * 列簇数组 * */ @SuppressWarnings("resource") public static void creatTable(String tableName, String[] family) { try { HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor desc = new HTableDescriptor(tableName.getBytes("utf-8")); for (int i = 0; i < family.length; i++) { desc.addFamily(new HColumnDescriptor(family[i])); } if (admin.tableExists(tableName)) { System.out.println("表格已存在!"); System.exit(0); } else { admin.createTable(desc); System.out.println("创建表格" + tableName + "成功!"); } } catch (Exception e) { } } /** * 为表添加数据(适合知道有多少列族的固定表) 同一列簇名下 * * @param rowKey * @param tableName * @param familyName * @param columns * 列名数组 * @param values * 值数组 * @return */ @SuppressWarnings("resource") public static void addData(String rowKey, String tableName, String familyName, String[] columns, String[] values) { try { Put put = new Put(rowKey.getBytes("utf-8"));// 设置rowkey HTable table = new HTable(conf, tableName.getBytes("utf-8"));// HTabel负责跟记录相关的操作如增删改查等// // 获取表 for (int i = 0; i < columns.length; i++) { put.add(familyName.getBytes("utf-8"), columns[i].getBytes("utf-8"), values[i].getBytes("utf-8")); } table.put(put); System.out.println("数据插入成功!"); } catch (Exception e) { e.printStackTrace(); } } /** * 根据rwokey查询 * * @rowKey rowKey * * @tableName 表名 */ @SuppressWarnings("resource") public static void getResult(String tableName, String rowKey) { try { Get get = new Get(Bytes.toBytes(rowKey)); HTable table = new HTable(conf, tableName.getBytes("utf-8"));// 获取表 Result result = table.get(get); for (KeyValue kv : result.raw()) { System.out.print(new String(kv.getRow()) + "\t");// rowkey System.out.print(new String(kv.getFamily()) + "\t");// 列簇名 System.out.print(new String(kv.getQualifier()) + "\t");// 列名 System.out.println(new String(kv.getValue()));// 值 } } catch (Exception e) { e.printStackTrace(); } } /* * 遍历查询hbase表 * * @tableName 表名 */ @SuppressWarnings("resource") public static void getResultScann(String tableName) { ResultScanner rs = null; try { Scan scan = new Scan(); HTable table = new HTable(conf, tableName.getBytes("utf-8")); rs = table.getScanner(scan); for (Result r : rs) { for (KeyValue kv : r.raw()) { System.out.print(new String(kv.getRow()) + "\t");// rowkey System.out.print(new String(kv.getFamily()) + "\t");// 列簇名 System.out.print(new String(kv.getQualifier()) + "\t");// 列名 System.out.println(new String(kv.getValue()));// 值 } } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); } } /** * 更新表中的某一列 * * @tableName 表名 * @rowKey rowKey * @familyName 列族名 * @columnName 列名 * @value 更新后的值 */ @SuppressWarnings("resource") public static void updateTable(String tableName, String rowKey, String familyName, String columnName, String value) { try { HTable table = new HTable(conf, tableName.getBytes("utf-8")); Put put = new Put(rowKey.getBytes("utf-8")); put.add(familyName.getBytes("utf-8"), columnName.getBytes("utf-8"), value.getBytes("utf-8")); table.put(put); System.out.println("更新数据成功!"); } catch (Exception e) { e.printStackTrace(); } } /** * 删除指定的列 * * @tableName 表名 * @rowKey rowKey */ @SuppressWarnings("resource") public static void deleteAllColumn(String tableName, String rowKey) { try { HTable table = new HTable(conf, tableName.getBytes("utf-8")); Delete deleteAll = new Delete(rowKey.getBytes("utf-8")); table.delete(deleteAll); System.out.println("删除指定行数据成功!"); } catch (Exception e) { e.printStackTrace(); } } /** * 删除表 * * @tableName 表名 */ @SuppressWarnings("resource") public static void deleteTable(String tableName) { try { HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tableName.getBytes("utf-8")); admin.deleteTable(tableName.getBytes("utf-8")); System.out.println(tableName + "删除成功!"); } catch (Exception e) { e.printStackTrace(); } } }将上面的
static { conf = new HBaseConfiguration(); conf.addResource("hbase-site.xml"); }替换为
static { conf = new HBaseConfiguration(); conf.set( "hbase.zookeeper.property.clientPort", "2181"); conf.set( "hbase.zookeeper.quorum", "hdp3.site,hdp2.site,hdp1.site"); }hdp3.site,hdp2.site,hdp1.site为hbase对应的hadoop集群的host
此方法不需要拷贝hbase的配置文件habse.xml
文件中若未引入jdk则项目报错Missing artifact jdk.tools
<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.study</groupId> <artifactId>hadoop</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.6</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency> </dependencies> </project>