Hbase Java API

启动Hadoop

cd /opt/hadoop/sbin
hadoop namenode -format
./start-all.sh

启动hbase

cd /opt/hbase-1.2.6/bin/
./start-hbase.sh

hbasePro/maven

sudo mkdir /home/ubuntu/.m2
sudo cp /opt/apache-maven-3.6.1/conf/settings.xml ~/.m2
cd /home/ubuntu/.m2
sudo vim settings.xml
<localRepository>/home/ubuntu/local_repo</localRepository>

<mirrors> </mirrors>之间添加下面配置:

<mirror>
      <id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
      <name>aliyun maven</name> 
      <url>http://maven.aliyun.com/repository/public</url>
 </mirror>

maven路径

/home/ubuntu/.m2/settings.xml

hbasePro/pom.xml

添加Hadoop相关依赖,在<dependencies> </dependencies> 之间添加以下依赖:

<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.7.3</version>
    </dependency>
     <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.3</version>
    </dependency>
     <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.7.3</version>
    </dependency>
     <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-core</artifactId>
      <version>2.7.3</version>
    </dependency>
    <dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.1.2</version>
    </dependency>
    <dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.1.2</version>
</dependency>

</project> 之前添加以下配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>${app.main.class}</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

JavaHBase.java

第24行改表名!!!

第24行改表名!!!

第24行改表名!!!

第148行改姓名!!!

第148行改姓名!!!

第148行改姓名!!!

package com.mytest.hbasePro;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class javaHbase {
    public static Configuration conf = null;
    public static Connection con = null;    
    public static String TABLE_NAME="18309";

    public static void getConntection() {        
        conf = HBaseConfiguration.create();        
        conf.set("hbase.zookeeper.quorum", "localhost");
        conf.set("hbase.zookeeper.property.clientPort", "2181");    
        try {
            con = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void createTable() throws Exception {
        Admin admin = con.getAdmin();        
        TableName tn = TableName.valueOf(TABLE_NAME);        
        if (admin.tableExists(tn)) {
            System.out.println("====>table is exist, delete it....");        
            admin.disableTable(tn);        
            admin.deleteTable(tn);
            System.out.println("delete success.....");
        }        
        HTableDescriptor htd = new HTableDescriptor(tn);
        
        HColumnDescriptor h1 = new HColumnDescriptor("pinfo");
        HColumnDescriptor h2 = new HColumnDescriptor("padd");
        
        htd.addFamily(h1);
        htd.addFamily(h2);
        admin.createTable(htd);
    }
    
    public static void putInfo() throws IOException {
        Table table = con.getTable(TableName.valueOf(TABLE_NAME));
        Put put1 = new Put(Bytes.toBytes("0090"));
        put1.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("name"),
                Bytes.toBytes("Jane")); 
        put1.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("high"), 
                Bytes.toBytes("170")); 
        put1.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("weigh"), 
                Bytes.toBytes("54"));
        put1.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("city"),
                Bytes.toBytes("benxi")); 
        put1.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("room"),
                Bytes.toBytes("a401")); 
        
        Put put2 = new Put(Bytes.toBytes("0091"));
        put2.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("name"),
                Bytes.toBytes("Mike")); 
        put2.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("high"), 
                Bytes.toBytes("180")); 
        put2.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("weigh"), 
                Bytes.toBytes("84"));
        put2.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("city"),
                Bytes.toBytes("shenyang")); 
        put2.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("room"),
                Bytes.toBytes("a402")); 
    
        Put put3 = new Put(Bytes.toBytes("0092"));
        put3.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("name"),
                Bytes.toBytes("Peter")); 
        put3.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("high"), 
                Bytes.toBytes("185")); 
        put3.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("weigh"), 
                Bytes.toBytes("70"));
        put3.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("city"),
                Bytes.toBytes("dandong")); 
        put3.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("room"),
                Bytes.toBytes("b403"));
        table.put(put1);
        table.put(put2);
        table.put(put3);
        table.close();
    }
    public static void putNewInfo() throws IOException {
        Table table = con.getTable(TableName.valueOf(TABLE_NAME));
        Put put1 = new Put(Bytes.toBytes("0090"));
        put1.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("name"),
                Bytes.toBytes("Jane")); 
        put1.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("high"), 
                Bytes.toBytes("170")); 
        put1.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("weigh"), 
                Bytes.toBytes("54"));
        put1.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("city"),
                Bytes.toBytes("benxi")); 
        put1.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("room"),
                Bytes.toBytes("a401")); 
    
        table.put(put1);
        table.close();
    }
    
    public static void deleteData() throws Exception {
        Table table = con.getTable(TableName.valueOf(TABLE_NAME));        
        Delete delete=new Delete(Bytes.toBytes("0090"));        
        table.delete(delete);
    }
    
    public static void updateData() throws IOException {
        Table table = con.getTable(TableName.valueOf(TABLE_NAME));
        Put put = new Put(Bytes.toBytes("0034"));
        put.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("name"),
                Bytes.toBytes("liyizhuang")); 
        put.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("high"), 
                Bytes.toBytes("173")); 
        put.addColumn(Bytes.toBytes("pinfo"), 
                Bytes.toBytes("weigh"), 
                Bytes.toBytes("51"));
        put.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("city"),
                Bytes.toBytes("benxi")); 
        put.addColumn(Bytes.toBytes("padd"),
                Bytes.toBytes("room"),
                Bytes.toBytes("a615"));
        table.put(put);
        table.close();
    }
    
    public static void main(String[] args) throws Exception {
        getConntection();
//        createTable();
//        putInfo();
//        putNewInfo();
//        updateData();
        deleteData();
    }
}
describe ''
scan ''
describe '18309'
scan '18309'