|
@@ -2,6 +2,7 @@ package com.alibaba.otter.canal.client.adapter.hbase;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
|
+import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -9,8 +10,11 @@ import javax.sql.DataSource;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
import org.apache.hadoop.conf.Configuration;
|
|
import org.apache.hadoop.hbase.HBaseConfiguration;
|
|
import org.apache.hadoop.hbase.HBaseConfiguration;
|
|
-import org.apache.hadoop.hbase.client.Connection;
|
|
|
|
-import org.apache.hadoop.hbase.client.ConnectionFactory;
|
|
|
|
|
|
+import org.apache.hadoop.hbase.client.*;
|
|
|
|
+import org.apache.hadoop.hbase.client.Result;
|
|
|
|
+import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
|
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
|
import com.alibaba.otter.canal.client.adapter.hbase.config.MappingConfig;
|
|
import com.alibaba.otter.canal.client.adapter.hbase.config.MappingConfig;
|
|
@@ -29,12 +33,15 @@ import com.alibaba.otter.canal.client.adapter.support.*;
|
|
@SPI("hbase")
|
|
@SPI("hbase")
|
|
public class HbaseAdapter implements OuterAdapter {
|
|
public class HbaseAdapter implements OuterAdapter {
|
|
|
|
|
|
- private static volatile Map<String, MappingConfig> hbaseMapping = null; // 文件名对应配置
|
|
|
|
- private static volatile Map<String, MappingConfig> mappingConfigCache = null; // 库名-表名对应配置
|
|
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(HbaseAdapter.class);
|
|
|
|
+
|
|
|
|
+ private static volatile Map<String, MappingConfig> hbaseMapping = null; // 文件名对应配置
|
|
|
|
+ private static volatile Map<String, MappingConfig> mappingConfigCache = null; // 库名-表名对应配置
|
|
|
|
|
|
private Connection conn;
|
|
private Connection conn;
|
|
private HbaseSyncService hbaseSyncService;
|
|
private HbaseSyncService hbaseSyncService;
|
|
private HbaseTemplate hbaseTemplate;
|
|
private HbaseTemplate hbaseTemplate;
|
|
|
|
+ private Configuration hbaseConfig;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void init(OuterAdapterConfig configuration) {
|
|
public void init(OuterAdapterConfig configuration) {
|
|
@@ -55,7 +62,7 @@ public class HbaseAdapter implements OuterAdapter {
|
|
|
|
|
|
Map<String, String> propertites = configuration.getProperties();
|
|
Map<String, String> propertites = configuration.getProperties();
|
|
|
|
|
|
- Configuration hbaseConfig = HBaseConfiguration.create();
|
|
|
|
|
|
+ hbaseConfig = HBaseConfiguration.create();
|
|
propertites.forEach(hbaseConfig::set);
|
|
propertites.forEach(hbaseConfig::set);
|
|
conn = ConnectionFactory.createConnection(hbaseConfig);
|
|
conn = ConnectionFactory.createConnection(hbaseConfig);
|
|
hbaseTemplate = new HbaseTemplate(conn);
|
|
hbaseTemplate = new HbaseTemplate(conn);
|
|
@@ -90,6 +97,28 @@ public class HbaseAdapter implements OuterAdapter {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Map<String, Object> count(String task) {
|
|
|
|
+ MappingConfig config = hbaseMapping.get(task);
|
|
|
|
+ String hbaseTable = config.getHbaseOrm().getHbaseTable();
|
|
|
|
+ long rowCount = 0L;
|
|
|
|
+ try {
|
|
|
|
+ HTable table = new HTable(hbaseConfig, hbaseTable);
|
|
|
|
+ Scan scan = new Scan();
|
|
|
|
+ scan.setFilter(new FirstKeyOnlyFilter());
|
|
|
|
+ ResultScanner resultScanner = table.getScanner(scan);
|
|
|
|
+ for (Result result : resultScanner) {
|
|
|
|
+ rowCount += result.size();
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ Map<String, Object> res = new LinkedHashMap<>();
|
|
|
|
+ res.put("hbaseTable", hbaseTable);
|
|
|
|
+ res.put("count", rowCount);
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void destroy() {
|
|
public void destroy() {
|
|
if (conn != null) {
|
|
if (conn != null) {
|