1. 程式人生 > >4.HBase_基礎應用Java API

4.HBase_基礎應用Java API

使用Java呼叫HBase的常用操作。首先,新建一個Maven專案,匯入HBase依賴,編寫Java操作HBase的工具包。

1.新建Maven專案,匯入HBase相關依賴

<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>cn.lv</groupId>
	<artifactId>hbase-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

	<dependencies>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.7.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase-client</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.7</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
	</dependencies>
	<build>
		<finalName>hbase-study</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>assembly</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2.編寫程式碼

package cn.lv;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

@SuppressWarnings("all")
public class HBaseOperationUtil {
	/**
	 * 1.配置檔案 2.會話連線 3.管理物件
	 */
	private static Logger logger = Logger.getLogger(HBaseOperationUtil.class);
	private static Configuration conf = null;
	private static Connection conn = null;
	private static Admin admin = null;

	/**
	 * 初始化
	 */
	static {
		try {
			conf = HBaseConfiguration.create();
			conn = ConnectionFactory.createConnection(conf);
			admin = conn.getAdmin();
			logger.info("init complete successfully!");
		} catch (IOException e) {
			logger.error("init error" + e.getMessage());
		}
	}

	/**
	 * 建立表
	 * 
	 * @param tb
	 * @param cf
	 * @throws IOException
	 */
	public static void createHTable(String tb, String cf) throws IOException {
		TableName tableName = TableName.valueOf(tb);
		if (!admin.tableExists(tableName)) {
			HTableDescriptor desc = new HTableDescriptor(tableName);
			HColumnDescriptor family = new HColumnDescriptor(cf);
			desc.addFamily(family);
			admin.createTable(desc);
			logger.info(tb + " create success!");
		} else {
			logger.warn(tb + " is already exists!");
		}
	}

	/**
	 * 刪除表
	 * 
	 * @param tb
	 * @throws IOException
	 */
	public static void deleteTable(String tb) throws IOException {
		TableName tableName = TableName.valueOf(tb);
		admin.deleteTable(tableName);
		logger.info(tb + " delete success!");
	}

	/**
	 * 插入資料
	 * 
	 * @param tb
	 * @param rowKey
	 * @param family
	 * @param column
	 * @param value
	 * @throws IOException
	 */
	public static void put(String tb, String rowKey, String family, String column, String value) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Put put = new Put(Bytes.toBytes(rowKey));
		put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
		table.put(put);
		table.close();
		logger.info("put table " + tb + " data success!");
	}

	/**
	 * 查詢資料
	 * 
	 * @param tb
	 * @param rowKey
	 * @param family
	 * @param qualifier
	 * @return
	 * @throws IOException
	 */
	public static String get(String tb, String rowKey, String family, String qualifier) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addFamily(Bytes.toBytes(family));
		get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
		Result result = table.get(get);
		return Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
	}

	/**
	 * 範圍掃描
	 * 
	 * @param tb
	 * @param startRow
	 * @param endRow
	 * @param family
	 * @param qualifier
	 * @return
	 * @throws IOException
	 */
	public static List<String> scan(String tb, String startRow, String endRow, String family, String qualifier)
			throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytes(startRow));
		scan.setStopRow(Bytes.toBytes(endRow));
		scan.addFamily(Bytes.toBytes(family));
		scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
		ResultScanner result = table.getScanner(scan);
		List<String> list = new ArrayList<String>();
		for (Result r : result) {
			list.add(Bytes.toString(r.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
		}
		return list;
	}

	/**
	 * 條件查詢
	 * 
	 * @param tb
	 * @param family
	 * @param qualifier
	 * @return
	 * @throws IOException
	 */
	public static List<String> scan(String tb, String family, String qualifier) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Scan scan = new Scan();
		scan.addFamily(Bytes.toBytes(family));
		scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
		ResultScanner result = table.getScanner(scan);
		List<String> list = new ArrayList<String>();
		for (Result r : result) {
			list.add(Bytes.toString(r.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
		}
		return list;
	}

	/**
	 * 全表掃描
	 * 
	 * @param tb
	 * @return
	 * @throws IOException
	 */
	public static List<String> scan(String tb) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Scan scan = new Scan();
		ResultScanner result = table.getScanner(scan);
		List<String> list = new ArrayList<String>();
		for (Result r : result) {
			List<Cell> cells = r.listCells();
			for (Cell cell : cells) {
				StringBuilder sb = new StringBuilder();
				sb.append("rowKey:").append(Bytes.toString(CellUtil.cloneRow(cell))).append("\t");
				sb.append("family:").append(Bytes.toString(CellUtil.cloneFamily(cell))).append(",");
				sb.append("qualifier:").append(Bytes.toString(CellUtil.cloneQualifier(cell))).append("=");
				sb.append("value:").append(Bytes.toString(CellUtil.cloneValue(cell)));
				list.add(sb.toString());
			}
		}
		return list;
	}

	/**
	 * 刪除資料
	 * 
	 * @param tb
	 * @param rowKey
	 * @param family
	 * @param qualifier
	 * @throws IOException
	 */
	public static void delete(String tb, String rowKey, String family, String qualifier) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Delete delete = new Delete(Bytes.toBytes(rowKey));
		delete.addColumn(family.getBytes(), qualifier.getBytes());
		table.delete(delete);
	}

	/**
	 * 關閉
	 * 
	 * @throws IOException
	 */
	public static void close() throws IOException {
		admin.close();
		conn.close();
	}

	public static void main(String[] args) throws IOException {
		// 插入資料
		put("lv:emp", "10002", "info", "name", "lisi");
		put("lv:emp", "10002", "info", "age", "22");
		// 查詢資料,只取name
		List<String> list = scan("lv:emp", "info", "name");
		// 刪除資料,刪除10001的age列
		delete("lv:emp", "10001", "info", "age");

	}
}