一、使用IDEA的maven工程,工程结构如下:
二、maven的依赖pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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.hbasetest</groupId> <artifactId>HbaseTest</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.0</version> </dependency> </dependencies> </project>
三、hbase-site.xml,在HBase集群的{HBASE_HOME}/conf目录下下载到本地,放到resources资源目录下
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!-- 设置namenode所在位置 通过rootdir设置 也就是设置hdfs中存放的路径 --> <property> <name>hbase.rootdir</name> <value>hdfs://hd09-1:9000/hbase</value> </property> <!-- 是否开启集群 --> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <!-- 0.98 后的新变动,之前版本没有.port,默认端口为 60000 --> <property> <name>hbase.master.port</name> <value>16000</value> </property> <!-- zookeeper集群的位置 --> <property> <name>hbase.zookeeper.quorum</name> <value>hd09-1:2181,hd09-2:2181,hd09-3:2181</value> </property> <!-- hbase的元数据信息存储在zookeeper的位置 --> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/root/hd/zookeeper-3.4.10/zkData</value> </property> </configuration>
四、core-site.xml,在Hadoop集群的{HADOOP_HOME}/etc/hadoop目录下下载到本地,放到resources资源目录下
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration> <property> <name>fs.defaultFS</name> <value>hdfs://hd09-1:9000</value> </property> </configuration>
五、hdfs-site.xml,在Hadoop集群的{HADOOP_HOME}/etc/hadoop目录下下载到本地,放到resources资源目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>dfs.namenode.name.dir</name>
<value>/root/hd/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/root/hd/dfs/data</value>
</property>
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>hd09-2:50090</value>
</property>
</configuration>
六、修改本地 C:WindowsSystem32driversetchosts 文件,在文件最下面加上
192.168.146.132 hd09-1 192.168.146.133 hd09-2 192.168.146.134 hd09-3
七、HbaseAPI 类
package com.demo.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 java.io.IOException; import java.util.ArrayList; import java.util.List; public class HbaseAPI { //配置信息 public static Configuration conf; //获取配置信息 static { //alt + enter conf = HBaseConfiguration.create(); } //1.判断一张表是否存在 public static boolean isExist(String tableName) throws IOException { //对表操作需要使用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //管理表 HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); return admin.tableExists(TableName.valueOf(tableName)); } //2.在HBase集群创建表 create 'user','info','info1' public static void createTable(String tableName,String... columnFamily) throws IOException { //对表操作需要用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //管理表 HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); //1.表如果存在 请输入其他表名 if (isExist(tableName)){ System.out.println("表已经存在,请输入其它表名"); }else{ //2.注意,创建表的话 需要创建一个描述器 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); //3.创建列族 for (String cf : columnFamily) { htd.addFamily(new HColumnDescriptor(cf)); } //4.创建表 admin.createTable(htd); System.out.println("表已创建成功!"); } } //3.删除HBase中的表 public static void deleteTable(String tableName) throws IOException{ //对表操作需要使用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //管理表 HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); //1.如果表存在 删除 否则打印不存在 //需要先指定表不可用 再删除 if (isExist(tableName)){ //2.指定不可用 admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); }else { System.out.println("表不存在,请重新输入表名!"); } } //4.添加数据put 'user','rowKey' public static void addRow(String tableName, String rowkey, String cf, String column, String value) throws IOException { //对表操作需要使用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //拿到表对象 Table t = connection.getTable(TableName.valueOf(tableName)); HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); //1.用put方式加入数据 Put p = new Put(Bytes.toBytes(rowkey)); //2.加入数据 p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value)); t.put(p); } //5.删除表中一行数据 public static void deleteRow(String tableName, String rowkey, String cf) throws IOException { //对表操作需要用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //拿到表对象 Table t = connection.getTable(TableName.valueOf(tableName)); //1.根据rowkey删除数据 Delete d = new Delete(Bytes.toBytes(rowkey)); //2.删除 t.delete(d); } //6.删除多行数据 public static void deleteAll(String tableName, String... rowkeys) throws IOException { //对表操作需要用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //拿到表对象 Table t = connection.getTable(TableName.valueOf(tableName)); //1.把delete封装到集合 List<Delete> list = new ArrayList<Delete>(); //2.遍历 for (String row : rowkeys) { Delete d = new Delete(Bytes.toBytes(row)); list.add(d); } t.delete(list); } //7.扫描表数据 scan全表扫描 public static void scanAll(String tableName) throws IOException { //对表操作需要用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //拿到表对象 Table t = connection.getTable(TableName.valueOf(tableName)); //1.实例scan Scan s = new Scan(); //2.拿到Scanner对象 ResultScanner rs = t.getScanner(s); //3.遍历 for (Result r : rs) { Cell[] cells = r.rawCells(); //遍历具体数据 for (Cell c : cells) { System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c))); System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c))); System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c))); } } } //8.扫描指定的数据 public static void getRow(String tableName, String rowkey) throws IOException { //对表操作需要用HBaseAdmin Connection connection = ConnectionFactory.createConnection(conf); //拿到表对象 Table t = connection.getTable(TableName.valueOf(tableName)); //1.扫描指定数据需要实例Get Get g = new Get(Bytes.toBytes(rowkey)); //2.可加过滤条件 g.addFamily(Bytes.toBytes("info")); Result rs = t.get(g); Cell[] cells = rs.rawCells(); //3.遍历 //遍历具体数据 for (Cell c : cells) { System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c))); System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c))); System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c))); } } public static void main(String[] args) throws IOException { // System.out.println(isExist("emp11")); // createTable("zhaosi","henshuai","feichangshuai"); // createTable("zhaosi","info"); // deleteTable("zhaosi"); // createTable("yangmi","info"); // addRow("yangmi","101","info","age","18"); // deleteRow("yangmi","101","info"); // deleteAll("emp","1001","1002r"); // scanAll("yangmi"); getRow("lisi","102"); } }