1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
| import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.client.coprocessor.AggregationClient; import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch;
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
@DependsOn("springContextHolder") @Component public class HBaseUtils {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static HbaseConfig hbaseConfig = SpringContextHolder.getBean("hbaseConfig");
private static Configuration conf = HBaseConfiguration.create(); private static ExecutorService pool = Executors.newScheduledThreadPool(20); private static Connection connection = null; private static HBaseUtils instance = null; private static Admin admin = null;
private HBaseUtils(){ if(connection == null){ try { Map<String, String> confMap = hbaseConfig.getconfMaps(); for (Map.Entry<String,String> confEntry : confMap.entrySet()) { conf.set(confEntry.getKey(), confEntry.getValue()); } connection = ConnectionFactory.createConnection(conf, pool); admin = connection.getAdmin(); } catch (IOException e) { logger.error("HbaseUtils实例初始化失败!错误信息为:" + e.getMessage(), e); } } }
public static synchronized HBaseUtils getInstance(){ if(instance == null){ instance = new HBaseUtils(); } return instance; }
public void createTable(String tableName, String[] columnFamily) throws IOException{ TableName name = TableName.valueOf(tableName); if (admin.tableExists(name)) { admin.disableTable(name); admin.deleteTable(name); logger.error("create htable error! this table {} already exists!", name); } else { HTableDescriptor desc = new HTableDescriptor(name); for (String cf : columnFamily) { desc.addFamily(new HColumnDescriptor(cf)); } admin.createTable(desc); } }
public void insertRecords(String tableName, String row, String columnFamilys, String[] columns, String[] values) throws IOException { TableName name = TableName.valueOf(tableName); Table table = connection.getTable(name); Put put = new Put(Bytes.toBytes(row)); for (int i = 0; i < columns.length; i++) { put.addColumn(Bytes.toBytes(columnFamilys), Bytes.toBytes(columns[i]), Bytes.toBytes(values[i])); table.put(put); } }
public void insertOneRecord(String tableName, String row, String columnFamily, String column, String value) throws IOException { TableName name = TableName.valueOf(tableName); Table table = connection.getTable(name); Put put = new Put(Bytes.toBytes(row)); put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); }
public void deleteRow(String tablename, String rowkey) throws IOException { TableName name = TableName.valueOf(tablename); Table table = connection.getTable(name); Delete d = new Delete(rowkey.getBytes()); table.delete(d); }
public void deleteColumnFamily(String tablename, String rowkey, String columnFamily) throws IOException { TableName name = TableName.valueOf(tablename); Table table = connection.getTable(name); Delete d = new Delete(rowkey.getBytes()).deleteFamily(Bytes.toBytes(columnFamily)); table.delete(d); }
public void deleteColumn(String tablename, String rowkey, String columnFamily, String column) throws IOException { TableName name = TableName.valueOf(tablename); Table table = connection.getTable(name); Delete d = new Delete(rowkey.getBytes()).deleteColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); table.delete(d); }
public static String selectRow(String tablename, String rowKey) throws IOException { String record = ""; TableName name=TableName.valueOf(tablename); Table table = connection.getTable(name); Get g = new Get(rowKey.getBytes()); Result rs = table.get(g); NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = rs.getMap(); for (Cell cell : rs.rawCells()) { StringBuffer stringBuffer = new StringBuffer().append(Bytes.toString(cell.getRow())).append("\t") .append(Bytes.toString(cell.getFamily())).append("\t") .append(Bytes.toString(cell.getQualifier())).append("\t") .append(Bytes.toString(cell.getValue())).append("\n"); String str = stringBuffer.toString(); record += str; } return record; }
public static String selectValue(String tablename, String rowKey, String columnFamily, String column) throws IOException { TableName name=TableName.valueOf(tablename); Table table = connection.getTable(name); Get g = new Get(rowKey.getBytes()); g.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); Result rs = table.get(g); return Bytes.toString(rs.value()); }
public String scanAllRecord(String tablename) throws IOException { String record = ""; TableName name=TableName.valueOf(tablename); Table table = connection.getTable(name); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); try { for(Result result : scanner){ for (Cell cell : result.rawCells()) { StringBuffer stringBuffer = new StringBuffer().append(Bytes.toString(cell.getRow())).append("\t") .append(Bytes.toString(cell.getFamily())).append("\t") .append(Bytes.toString(cell.getQualifier())).append("\t") .append(Bytes.toString(cell.getValue())).append("\n"); String str = stringBuffer.toString(); record += str; } } } finally { if (scanner != null) { scanner.close(); } }
return record; }
public List scanReportDataByRowKeyword(String tablename, String rowKeyword) throws IOException { ArrayList<Object> list = new ArrayList<>();
Table table = connection.getTable(TableName.valueOf(tablename)); Scan scan = new Scan();
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(rowKeyword)); scan.setFilter(rowFilter);
ResultScanner scanner = table.getScanner(scan); try { for (Result result : scanner) { list.add(null); } } finally { if (scanner != null) { scanner.close(); } }
return list; }
public List scanReportDataByRowKeywordTimestamp(String tablename, String rowKeyword, Long minStamp, Long maxStamp) throws IOException { ArrayList<Object> list = new ArrayList<>();
Table table = connection.getTable(TableName.valueOf(tablename)); Scan scan = new Scan(); scan.setTimeRange(minStamp, maxStamp);
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(rowKeyword)); scan.setFilter(rowFilter);
ResultScanner scanner = table.getScanner(scan); try { for (Result result : scanner) { list.add(null); } } finally { if (scanner != null) { scanner.close(); } }
return list; }
public void deleteTable(String tablename) throws IOException { TableName name=TableName.valueOf(tablename); if(admin.tableExists(name)) { admin.disableTable(name); admin.deleteTable(name); } }
public Long countRowsWithCoprocessor(String tablename) throws Throwable { TableName name=TableName.valueOf(tablename); HTableDescriptor descriptor = admin.getTableDescriptor(name);
String coprocessorClass = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation"; if (! descriptor.hasCoprocessor(coprocessorClass)) { admin.disableTable(name); descriptor.addCoprocessor(coprocessorClass); admin.modifyTable(name, descriptor); admin.enableTable(name); }
StopWatch stopWatch = new StopWatch(); stopWatch.start();
Scan scan = new Scan(); AggregationClient aggregationClient = new AggregationClient(conf);
Long count = aggregationClient.rowCount(name, new LongColumnInterpreter(), scan);
stopWatch.stop(); System.out.println("RowCount:" + count + ",全表count统计耗时:" + stopWatch.getTotalTimeMillis());
return count; }
}
|