使用Maven开发hbase的例子

创建Maven项目

 

 




配置pom.xml

 

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0modelVersion>

 

  <groupId>hbaseDemogroupId>

  <artifactId>hbaseDemoartifactId>

  <version>0.0.1-SNAPSHOTversion>

  <packaging>jarpackaging>

 

  <name>hbaseDemoname>

  <url>http://maven.apache.orgurl>

 

  <properties>

    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

  properties>

 

  <dependencies>

    <dependency>

      <groupId>junitgroupId>

      <artifactId>junitartifactId>

      <version>3.8.1version>

      <scope>testscope>

    dependency>

    

        <dependency>

            <groupId>org.apache.hadoopgroupId>

            <artifactId>hadoop-hdfsartifactId>

            <version>2.7.3version>

        dependency>

       

        <dependency>

            <groupId>org.apache.hbasegroupId>

            <artifactId>hbase-clientartifactId>

            <version>1.2.6version>

        dependency>

        <dependency>

            <groupId>jdk.toolsgroupId>

            <artifactId>jdk.toolsartifactId>

            <version>1.8version>

            <scope>systemscope>

            <systemPath>${JAVA_HOME}/lib/tools.jarsystemPath>

        dependency>

  dependencies>

project>

题外话,如果是WEB项目,还需要在pom.xml中导入javaee-api.jar

<dependency>

            <groupId>javaxgroupId>

            <artifactId>javaee-apiartifactId>

            <version>7.0version>

dependency>

 

packagehbaseDemo.hbaseDemo;

importjava.io.IOException;

import java.util.*;

 

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.hbase.HBaseConfiguration;

importorg.apache.hadoop.hbase.HColumnDescriptor;

importorg.apache.hadoop.hbase.HTableDescriptor;

importorg.apache.hadoop.hbase.TableName;

importorg.apache.hadoop.hbase.util.Bytes;

importorg.apache.hadoop.hbase.client.Put;

importorg.apache.hadoop.hbase.client.Table;

importorg.apache.hadoop.hbase.client.ConnectionFactory;

importorg.apache.hadoop.hbase.client.Admin;

importorg.apache.hadoop.hbase.client.Connection;

 

public class App

{

     private staticConfiguration conf = HBaseConfiguration.create();

        static {

           conf.set("hbase.rootdir", "hdfs://master:9000/hbase");

           // 设置Zookeeper,直接设置IP地址

           conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");

        }

 

        // 创建表

        public static voidcreateTable(String tablename, String columnFamily) throwsException {

           Connection connection =ConnectionFactory.createConnection(conf);

           Admin admin = connection.getAdmin();

 

           TableName tableNameObj = TableName.valueOf(tablename);

 

           if (admin.tableExists(tableNameObj)) {

               System.out.println("Tableexists!");

               System.exit(0);

           } else {

               HTableDescriptor tableDesc = newHTableDescriptor(TableName.valueOf(tablename));

               tableDesc.addFamily(newHColumnDescriptor(columnFamily));

               admin.createTable(tableDesc);

               System.out.println("createtable success!");

           }

           admin.close();

           connection.close();

        }

 

        // 删除表

        public static voiddeleteTable(String tableName) {

           try {

               Connection connection =ConnectionFactory.createConnection(conf);

               Admin admin = connection.getAdmin();

               TableName table = TableName.valueOf(tableName);

               admin.disableTable(table);

               admin.deleteTable(table);

               System.out.println("deletetable " + tableName + "ok.");

           } catch (IOException e) {

               e.printStackTrace();

           }

        }

 

        // 插入一行记录

       public static voidaddRecord(String tableName, String rowKey,String family, String qualifier,String value){

           try {

               Connection connection =ConnectionFactory.createConnection(conf);

               Table table = connection.getTable(TableName.valueOf(tableName));

               Put put = new Put(Bytes.toBytes(rowKey));

               put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));

               put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));

               table.put(put);

               table.close();

               connection.close();

               System.out.println("insertrecored " + rowKey + " totable " + tableName + "ok.");

           } catch (IOException e) {

               e.printStackTrace();

           }

        }

 

        public static voidmain(String[] args) throws Exception {

        App.createTable("testTb", "info");

        App.addRecord("testTb", "001", "info", "name", "zhangsan");

        App.addRecord("testTb", "001", "info", "age", "20");

           //HbaseDao.deleteTable("testTb");

        }

}

 

你可能感兴趣的:(hadoop)