快捷键
Alt + /
Alt +shift+ l
pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>C:/Program Files/Java/jdk1.8.0_74/lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
Constant
public class Constant {
public static String ZK_ADDRESS="cdh1,cdh2,cdh3";
public static String ZK_PORT="2181";
}
HBaseUnit
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
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.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
public class HBaseUnit {
private static Log log = LogFactory.getLog(HBaseUnit.class);
private Connection conn ;
private static String zkString = Constant.ZK_ADDRESS;
private static String zkPort = Constant.ZK_PORT;
/**
* HBaseUnit初始化
* @param zookeeperQuorum
* @param clientPort
*/
private HBaseUnit(String zookeeperQuorum,String clientPort){
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", clientPort);
conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("create hbase connection error", e);
}
}
/**
* 获取HBaseUnit对象
* @return
*/
public static HBaseUnit getHBaseUnit(){
HBaseUnit hBaseUnit = new HBaseUnit(zkString,zkPort);
return hBaseUnit ;
}
/**
* 获取表句柄
* @return
*/
public Table getTable(String tableName){
Table table = null ;
try {
table = conn.getTable(TableName.valueOf(tableName));
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("get hbase table error,tablename="+tableName, e);
}
return table ;
}
/**
* 创建hbase表
* @return
*/
public void createTable(String tableName,String family){
try {
Admin admin = conn.getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
desc.addFamily(new HColumnDescriptor(family));
if (!admin.tableExists(TableName.valueOf(tableName))){
admin.createTable(desc);
System.out.println(tableName+"表创建成功");
}else{
System.out.println(tableName+"表已经存在");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection();
}
}
/**
* 删除hbase表
* @return
*/
public void deleteTable(String tableName ){
try {
Admin admin = conn.getAdmin();
TableName table =TableName.valueOf(tableName);
if (admin.tableExists(TableName.valueOf(tableName))){
admin.disableTable(table);
admin.deleteTable(table);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection();
}
}
/**
* 插入数据
* @return
*/
public void put(String tableName,String row,String columnFamily,String column,String value){
Table table =getTable(tableName);
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
try {
table.put(put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeTable(table);
closeConnection();
}
}
/**
* 删除数据
* @return
*/
public void deleteRecord(String tableName,String row ){
Table table =getTable(tableName);
Delete delete = new Delete(row.getBytes());
try {
table.delete(delete);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeTable(table);
closeConnection();
}
}
/**
* 查询单条记录
* @return
*/
public void getResultByRow(String tableName,String row ){
Table table =getTable(tableName);
Get get = new Get(row.getBytes());
try {
Result result = table.get(get);
for (Cell cell:result.listCells()){
//column family 列簇
System.out.println("family:"+Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
//Qualifier 列名
System.out.println("family:"+Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
//value 值
System.out.println("family:"+Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
//timestamp
System.out.println("timestamp:"+cell.getTimestamp());
System.out.println("***************");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeTable(table);
closeConnection();
}
}
/**
* 浏览记录
* @return
*/
public void getResultByScan(String tableName ){
Table table =getTable(tableName);
Scan scan = new Scan();
try {
ResultScanner rsa = table.getScanner(scan);
for (Result result:rsa){
for (Cell cell:result.listCells()){
//column family 列簇
System.out.println("family:"+Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
//Qualifier 列名
System.out.println("family:"+Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
//value 值
System.out.println("family:"+Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
//timestamp
System.out.println("timestamp:"+cell.getTimestamp());
System.out.println("***************");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeTable(table);
closeConnection();
}
}
/**
* 关闭table
* @return
*/
public void closeTable(Table table ){
if (table!=null ){
try {
table.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("close table error,tableName="+table.getName(), e);
}
}
}
/**
* 关闭connect连接
* @return
*/
public void closeConnection(){
if (conn!=null && !conn.isClosed()){
try {
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("close hbase connection error", e);
}
}
}
}
HBaseManager
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Row;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseManager {
public static HBaseUnit hbaseUnit = HBaseUnit.getHBaseUnit();
public static void main(String[] args) {
//创建表
// hbaseUnit.createTable("user", "cf");
//插入数据
insertData("user","102");
//批量插入
// batchInsertData("user");
//根据rowkey查询数据
// hbaseUnit.getResultByRow("user", "102");
//扫描数据
// hbaseUnit.getResultByScan("user");
//删除一条记录
// hbaseUnit.deleteRecord("user", "101");
//drop 表
// hbaseUnit.deleteTable("user");
}
/**
* 插入数据
* @param tablename
* @param row
*/
public static void insertData(String tablename,String row){
Table table = hbaseUnit.getTable(tablename);
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("xiaozhang"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("age"), Bytes.toBytes("28"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("birthday"), Bytes.toBytes("1979-09-18"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("company"), Bytes.toBytes("alibaba"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("contry"), Bytes.toBytes("China"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("province"), Bytes.toBytes("shandong"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("city"), Bytes.toBytes("qingdao"));
try {
table.put(put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
hbaseUnit.closeTable(table);
hbaseUnit.closeConnection();
}
}
/**
* 批量插入
* @param tablename
*/
public static void batchInsertData(String tablename ){
Table table = hbaseUnit.getTable(tablename);
ArrayList<Row> batch = new ArrayList<Row>();
Put put2 = new Put(Bytes.toBytes("102"));
put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
batch.add(put2);
Put put3 = new Put(Bytes.toBytes("103"));
put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
batch.add(put3);
Put put4 = new Put(Bytes.toBytes("104"));
put4.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));
batch.add(put4);
Object[] results = new Object[batch.size()];
try {
table.batch(batch,results);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
hbaseUnit.closeTable(table);
hbaseUnit.closeConnection();
}
for (int i=0;i<results.length;i++){
System.out.println("Result["+i+"]:"+results[i]);
}
}
}