Skip to content

Commit

Permalink
重新整理结构,放入hive和hbase的例子
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyofin committed Jun 28, 2019
1 parent a5ef4b8 commit c52f4b5
Show file tree
Hide file tree
Showing 35 changed files with 1,644 additions and 38 deletions.
39 changes: 39 additions & 0 deletions hbase-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>bigdata-starter</artifactId>
<groupId>com.wugui</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hbase-starter</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
62 changes: 62 additions & 0 deletions hbase-starter/src/main/java/api/HBaseConn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
* on 18-2-25.
*/
public class HBaseConn {

private static final HBaseConn INSTANCE = new HBaseConn();
private static Configuration configuration;
private static Connection connection;

private HBaseConn() {
try {
if (configuration == null) {
configuration = HBaseConfiguration.create();
// 此处一定要改成你的zookeeper的连接地址
configuration.set("hbase.zookeeper.quorum", "localhost:2181");

}
} catch (Exception e) {
e.printStackTrace();
}
}

private Connection getConnection() {
if (connection == null || connection.isClosed()) {
try {
connection = ConnectionFactory.createConnection(configuration);
} catch (Exception e) {
e.printStackTrace();
}
}
return connection;
}

public static Connection getHBaseConn() {
return INSTANCE.getConnection();
}

public static Table getTable(String tableName) throws IOException {
return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}

public static void closeConn() {
if (connection != null) {
try {
connection.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
204 changes: 204 additions & 0 deletions hbase-starter/src/main/java/api/HBaseUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package api;

import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;

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

/**
* on 18-2-25.
*/
public class HBaseUtil {

/**
* 创建HBase表.
*
* @param tableName 表名
* @param cfs 列族的数组
* @return 是否创建成功
*/
public static boolean createTable(String tableName, String[] cfs) {
try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {
if (admin.tableExists(tableName)) {
return false;
}
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
Arrays.stream(cfs).forEach(cf -> {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf);
columnDescriptor.setMaxVersions(1);
tableDescriptor.addFamily(columnDescriptor);
});
admin.createTable(tableDescriptor);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}


/**
* 删除hbase表.
*
* @param tableName 表名
* @return 是否删除成功
*/
public static boolean deleteTable(String tableName) {
try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

/**
* hbase插入一条数据.
*
* @param tableName 表名
* @param rowKey 唯一标识
* @param cfName 列族名
* @param qualifier 列标识
* @param data 数据
* @return 是否插入成功
*/
public static boolean putRow(String tableName, String rowKey, String cfName, String qualifier,
String data) {
try (Table table = HBaseConn.getTable(tableName)) {
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
table.put(put);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return true;
}

public static boolean putRows(String tableName, List<Put> puts) {
try (Table table = HBaseConn.getTable(tableName)) {
table.put(puts);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return true;
}

/**
* 获取单条数据.
*
* @param tableName 表名
* @param rowKey 唯一标识
* @return 查询结果
*/
public static Result getRow(String tableName, String rowKey) {
try (Table table = HBaseConn.getTable(tableName)) {
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}

public static Result getRow(String tableName, String rowKey, FilterList filterList) {
try (Table table = HBaseConn.getTable(tableName)) {
Get get = new Get(Bytes.toBytes(rowKey));
get.setFilter(filterList);
return table.get(get);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}

public static ResultScanner getScanner(String tableName) {
try (Table table = HBaseConn.getTable(tableName)) {
Scan scan = new Scan();
scan.setCaching(1000);
return table.getScanner(scan);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}

/**
* 批量检索数据.
*
* @param tableName 表名
* @param startRowKey 起始RowKey
* @param endRowKey 终止RowKey
* @return ResultScanner实例
*/
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey) {
try (Table table = HBaseConn.getTable(tableName)) {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setCaching(1000);
return table.getScanner(scan);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}

public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey,
FilterList filterList) {
try (Table table = HBaseConn.getTable(tableName)) {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setFilter(filterList);
scan.setCaching(1000);
return table.getScanner(scan);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}

/**
* HBase删除一行记录.
*
* @param tableName 表名
* @param rowKey 唯一标识
* @return 是否删除成功
*/
public static boolean deleteRow(String tableName, String rowKey) {
try (Table table = HBaseConn.getTable(tableName)) {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return true;
}

public static boolean deleteColumnFamily(String tableName, String cfName) {
try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {
admin.deleteColumn(tableName, cfName);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

public static boolean deleteQualifier(String tableName, String rowKey, String cfName,
String qualifier) {
try (Table table = HBaseConn.getTable(tableName)) {
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier));
table.delete(delete);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return true;
}
}
31 changes: 31 additions & 0 deletions hbase-starter/src/test/java/HBaseConnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import api.HBaseConn;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.junit.Test;

import java.io.IOException;

/**
* on 18-2-25.
*/
public class HBaseConnTest {

@Test
public void getConnTest() {
Connection conn = HBaseConn.getHBaseConn();
System.out.println(conn.isClosed());
HBaseConn.closeConn();
System.out.println(conn.isClosed());
}

@Test
public void getTableTest() {
try {
Table table = HBaseConn.getTable("US_POPULATION");
System.out.println(table.getName().getNameAsString());
table.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Loading

0 comments on commit c52f4b5

Please sign in to comment.