Hbase java api

Note: To debug HBASE Java Api in eclipse, you need to copy the hbase-site.xml and hdfs-site.xml configuration files to the classpath

 

mavne loads dependency packages (choose your own corresponding version):

<!-- hbase java api -->
	<dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-server</artifactId>
	    <version>1.1.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-client</artifactId>
	    <version>1.1.2</version>
	</dependency>

 

Hbase java api example:

 

package com.hqmart.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 HbaseUtils {
	
	public static void main(String[] args) {
		HbaseUtils.createTable("myTest", "myfc1", "myfc2", "myfc3");
		
		HbaseUtils.insert("myTest", "1", "myfc1", "sex", "men");
		HbaseUtils.insert("myTest", "1", "myfc1", "name", "xiaoming");
        HbaseUtils.insert("myTest", "1", "myfc1", "age", "32");
        HbaseUtils.insert("myTest", "2", "myfc2", "name", "xiaohong");
        HbaseUtils.insert("myTest", "2", "myfc2", "sex", "woman");
        HbaseUtils.insert("myTest", "2", "myfc2", "age", "23");
        
        //Get the data of a column under the next column family of a row
        String result = HbaseUtils.byGet("myTest", "1", "myfc1", "name");
        System.out.println("The result is: " + result);
        
        //Get the data of all columns under the next column family of a row
        Map<String, String> result1 = HbaseUtils.byGet("myTest", "1", "myfc1");
        System.out.println("The result is: " + result1);
        
        //Get the data of all column families of a row
        Map<String, Map<String, String>> result2 = HbaseUtils.byGet("myTest", "1");
        System.out.println("The data of all column families is: "+result2);
        System.out.println("The result is: " + result2.get("myfc1"));
        System.out.println("The result is: " + result2.get("myfc2"));
        
        
//		HbaseUtils.dropTable("myTest");
		
		HbaseUtils.close();
	}
	
	private static Configuration conf;
    private static Connection con;
    // initialize the connection
    static {
        conf = HBaseConfiguration.create(); // Get the configuration file object
        conf.set("hbase.zookeeper.quorum", "172.xx.x.xxx,172.xx.x.xxx,172.xx.x.xxx");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        try {
            con = ConnectionFactory.createConnection(conf);// Get the connection object
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    // get the connection
    public static Connection getCon() {
        if (con == null || con.isClosed()) {
            try {
                con = ConnectionFactory.createConnection(conf);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
        return con;
    }

    // close the connection
    public static void close() {
        if (con != null) {
            try {
                con.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }

    // create table
    public static void createTable(String tableName, String... FamilyColumn) {
        TableName tn = TableName.valueOf(tableName);
        try {
            Admin admin = getCon().getAdmin();
            HTableDescriptor htd = new HTableDescriptor(tn);
            for (String fc : FamilyColumn) {
                HColumnDescriptor hcd = new HColumnDescriptor(fc);
                htd.addFamily(hcd);
            }
            admin.createTable(htd);
            admin.close();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    // delete table
    public static void dropTable(String tableName) {
        TableName tn = TableName.valueOf(tableName);
        try {
            Admin admin = con.getAdmin();
            admin.disableTable(tn);
            admin.deleteTable(tn);
            admin.close();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    // insert or update data
    public static boolean insert(String tableName, String rowKey,
            String family, String qualifier, String value) {
        try {
            Table t = getCon().getTable(TableName.valueOf(tableName));
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
                    Bytes.toBytes(value));
            t.put(put);
            return true;
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
        	HbaseUtils.close();
        }
        return false;
    }

    // delete
    public static boolean del(String tableName, String rowKey, String family,
            String qualifier) {
        try {
            Table t = getCon().getTable(TableName.valueOf(tableName));
            Delete del = new Delete(Bytes.toBytes(rowKey));

            if (qualifier != null) {
                del.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
            } else if (family != null) {
                del.addFamily(Bytes.toBytes(family));
            }
            t.delete(del);
            return true;
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
        	HbaseUtils.close();
        }
        return false;
    }
    // delete a line
    public static boolean del(String tableName, String rowKey) {
        return del(tableName, rowKey, null, null);
    }
    // delete a column family under a row
    public static boolean del(String tableName, String rowKey, String family) {
        return del(tableName, rowKey, family, null);
    }

    // data read
    // get a value
    public static String byGet(String tableName, String rowKey, String family,
            String qualifier) {
        try {
            Table t = getCon().getTable(TableName.valueOf(tableName));
            Get get = new Get(Bytes.toBytes(rowKey));
            get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
            Result r = t.get(get);
            return Bytes.toString(CellUtil.cloneValue(r.listCells().get(0)));
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return null;
    }

    // get the value of a family column
        public static Map<String, String> byGet(String tableName, String rowKey, String family) {
            Map<String, String> result = null ;
            try {
                Table t = getCon().getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowKey));
                get.addFamily(Bytes.toBytes(family));
                Result r = t.get(get);
                List<Cell> cs = r.listCells();
                result = cs.size() > 0 ? new HashMap<String, String>() : result;
                for (Cell cell : cs) {
                    result.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
                }
            } catch (IOException e) {
                e.printStackTrace ();
            }
            return result;
        }
    // Get the values ​​of multiple family columns
        public static Map<String, Map<String, String>> byGet(String tableName, String rowKey) {
            Map<String, Map<String, String>> results = null ;
            try {
                Table t = getCon().getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowKey));
                Result r = t.get(get);
                List<Cell> cs = r.listCells();
                results = cs.size() > 0 ? new HashMap<String, Map<String, String>> () : results;
                for (Cell cell : cs) {
                    String familyName = Bytes.toString(CellUtil.cloneFamily(cell));
                    if (results.get(familyName) == null)
                    {
                        results.put(familyName, new HashMap<String,  String> ());
                    }
                    results.get(familyName).put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
                }
            } catch (IOException e) {
                e.printStackTrace ();
            }
            return results;
        }
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326178143&siteId=291194637