环境准备:我用的是Hadoop2.4.0以及Hbase0.98.15,首先保证hadoop和hbase已经安装并且环境配置完成。
新建一个maven工程,如图:
什么都不勾选,直接选next,然后根据自己的喜好写一个名字:
next,然后给工程起一个名字,finish
好,工程创建完成
接下来,打开pom.xml添加hbase需要的依赖,添加如下三段代码:
<repositories> <repository> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase</artifactId> <version>0.98.1-cdh5.1.5</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.98.1-cdh5.1.5</version> </dependency> </dependencies>
然后maven自动开始下载
下载成功后,在src/main/java目录下新建一个java文件:
HBaseTestCase,代码如下:
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseTestCase { static Configuration cfg = HBaseConfiguration.create(); /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String tablename = "priceforcity"; // 创建表名 String columnFamily = "base"; //创建列族 try { HBaseTestCase.create(tablename, columnFamily); HBaseTestCase.put(tablename, "row1", columnFamily, "price", "3000"); HBaseTestCase.get(tablename, "row1"); HBaseTestCase.scan(tablename); // if(true==HBaseTestCase.delete(tablename)) { // System.out.println("Delete table:"+tablename+" success!"); // } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * create a table :table_name(columnFamily) * @param tablename * @param columnFamily * @throws Exception */ public static void create(String tablename, String columnFamily) throws Exception { HBaseAdmin admin = new HBaseAdmin(cfg); if(admin.tableExists(tablename)) { System.out.println("table exists!"); System.exit(0); } else { HTableDescriptor tableDesc = new HTableDescriptor(tablename); tableDesc.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(tableDesc); System.out.println("create table success!"); } } /** * put a row data into table * @param tablename * @param row * @param columnFamily * @param column * @param data * @throws Exception */ public static void put(String tablename, String row, String columnFamily, String column, String data) throws Exception{ HTable table = new HTable(cfg, tablename); Put p1 = new Put(Bytes.toBytes(row)); p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data)); table.put(p1); System.out.println("put '"+row+"', '"+columnFamily+":"+column+"', '"+data+"'"); } /** * get a row data from a table * @param tablename * @param row * @throws Exception */ public static void get(String tablename, String row) throws Exception { HTable table = new HTable(cfg, tablename); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); System.out.println("Get: "+result); } /** * show all data from a table * @param tablename * @throws Exception */ public static void scan(String tablename) throws Exception { HTable table = new HTable(cfg, tablename); Scan s =new Scan(); ResultScanner rs = table.getScanner(s); for(Result r:rs) { System.out.println("Scan: "+r); } } /** * delete a table's data * @param tablename * @return * @throws IOException */ public static boolean delete(String tablename) throws IOException { HBaseAdmin admin = new HBaseAdmin(cfg); if(admin.tableExists(tablename)) { try { admin.disableTable(tablename); admin.deleteTable(tablename); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return false; } } return true; } }
然后运行main方法,输出如下信息:
好啦,hbase正常运行,大功告成!!