public static int getDynamicTable(Configuration config) { /** Connection to the cluster. A single connection shared by all application threads. */ Connection connection = null; /** A lightweight handle to a specific table. Used from a single thread. */ Table table = null; try { connection = ConnectionFactory.createConnection(config); table = connection.getTable(TABLE_NAME1); Get get = new Get(Bytes.toBytes("cloudera")); get.addFamily(CF); get.setMaxVersions(Integer.MAX_VALUE); Result result = table.get(get); NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap(); for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyEntry : map.entrySet()) { NavigableMap<byte[], NavigableMap<Long, byte[]>> columnMap = columnFamilyEntry.getValue(); for (Entry<byte[], NavigableMap<Long, byte[]>> columnEntry : columnMap.entrySet()) { NavigableMap<Long, byte[]> cellMap = columnEntry.getValue(); for (Entry<Long, byte[]> cellEntry : cellMap.entrySet()) { System.out.println( String.format( "Key : %s, Value :%s", Bytes.toString(columnEntry.getKey()), Bytes.toString(cellEntry.getValue()))); } } } } catch (IOException e) { e.printStackTrace(); } return 0; }
/** 删除指定行 */ public static void deleteRow(byte[] rowKey) { Connection conn = null; HTable table = null; try { conn = ConnectionFactory.createConnection(conf); table = (HTable) conn.getTable(TableName.valueOf("test")); table.delete(new Delete(rowKey)); } catch (Exception e) { logger.error("HBaseAdmin deleteRow exception, errMsg:{}", e.getMessage()); } finally { if (null != table) { try { table.close(); } catch (IOException e) { logger.error("HTable close exception, errMsg:{}", e.getMessage()); } } if (null != conn) { try { conn.close(); } catch (Exception e) { logger.error("Connection close exception, errMsg:{}", e.getMessage()); } } } }
public void putBatch(Optional<List<Request>> putRequests, boolean optimize) { if (!valid) { Logger.error("CANNOT PUT! NO VALID CONNECTION"); return; } List<Put> puts = new ArrayList<>(); if (putRequests.isPresent() && !putRequests.get().isEmpty()) { String tableName = putRequests.get().get(0).table; putRequests .get() .forEach( pr -> pr.getPut() .ifPresent( p -> { if (optimize) { p.setDurability(Durability.SKIP_WAL); } puts.add(p); })); try { final Table table = connection.getTable(TableName.valueOf(tableName)); if (optimize && table instanceof HTable) { ((HTable) table).setAutoFlush(false, true); } table.put(puts); table.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * rowFilter的使用 * * @param tableName * @param reg * @throws Exception */ public void getRowFilter(String tableName, String reg) throws Exception { Connection conn = null; HTable table = null; try { conn = ConnectionFactory.createConnection(conf); table = (HTable) conn.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); // Filter RowFilter rowFilter = new RowFilter(CompareOp.NOT_EQUAL, new RegexStringComparator(reg)); scan.setFilter(rowFilter); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println(new String(result.getRow())); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != table) { try { table.close(); } catch (IOException e) { logger.error("HTable close exception, errMsg:{}", e.getMessage()); } } if (null != conn) { try { conn.close(); } catch (Exception e) { logger.error("Connection close exception, errMsg:{}", e.getMessage()); } } } }
/** * 根据RowKey查询单行 * * @param rowKey * @return */ public static Result queryByRowKey(byte[] rowKey) { Result result = null; if (null == rowKey || rowKey.length < 0) { return result; } Connection conn = null; HTable table = null; try { conn = ConnectionFactory.createConnection(conf); table = (HTable) conn.getTable(TableName.valueOf("test")); result = table.get(new Get(rowKey)); } catch (Exception e) { logger.error("queryByRowKey exception, rowKey:{}, errMsg:{}", new String(rowKey), e); } finally { if (null != table) { try { table.close(); } catch (IOException e) { logger.error("HTable close exception, errMsg:{}", e.getMessage()); } } if (null != conn) { try { conn.close(); } catch (IOException e) { logger.error("Connection close exception, errMsg:{}", e.getMessage()); } } } return result; }
public Map<String, Long> getRegionSizes(String tableName) { Map<String, Long> regions = new HashMap<>(); try { final Table table = connection.getTable(TableName.valueOf(tableName)); RegionLocator regionLocator = connection.getRegionLocator(table.getName()); List<HRegionLocation> tableRegionInfos = regionLocator.getAllRegionLocations(); Set<byte[]> tableRegions = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR); for (HRegionLocation regionInfo : tableRegionInfos) { tableRegions.add(regionInfo.getRegionInfo().getRegionName()); } ClusterStatus clusterStatus = connection.getAdmin().getClusterStatus(); Collection<ServerName> servers = clusterStatus.getServers(); final long megaByte = 1024L * 1024L; for (ServerName serverName : servers) { ServerLoad serverLoad = clusterStatus.getLoad(serverName); for (RegionLoad regionLoad : serverLoad.getRegionsLoad().values()) { byte[] regionId = regionLoad.getName(); if (tableRegions.contains(regionId)) { long regionSizeBytes = regionLoad.getStorefileSizeMB() * megaByte; regions.put(regionLoad.getNameAsString(), regionSizeBytes); } } } } catch (IOException e) { e.printStackTrace(); } return regions; }
public Optional<byte[]> get( Optional<String> table, Optional<String> family, Optional<String> qualifier, Optional<String> key) { if (!valid) { Logger.error("CANNOT GET! NO VALID CONNECTION"); return Optional.empty(); } if (table.isPresent() && family.isPresent() && qualifier.isPresent() && key.isPresent() && !key.get().isEmpty()) { try { final Table htable = connection.getTable(TableName.valueOf(table.get())); Result result = htable.get(new Get(key.get().getBytes("UTF8"))); return Optional.ofNullable( result.getValue(family.get().getBytes("UTF8"), qualifier.get().getBytes("UTF8"))); } catch (IOException e) { e.printStackTrace(); } } return Optional.empty(); }
public Optional<Response> get(Optional<Request> request) { if (!valid) { Logger.error("CANNOT GET! NO VALID CONNECTION"); return Optional.empty(); } Response response = new Response(); if (request.isPresent()) { Request r = request.get(); response.key = r.key; response.table = r.table; try { final Table htable = connection.getTable(TableName.valueOf(r.table)); Result result = htable.get(new Get(r.key)); if (result == null || result.isEmpty()) { return Optional.empty(); } r.columns.forEach( c -> response.columns.add( new Request.Column( c.family, c.qualifier, result.getValue(c.family.getBytes(), c.qualifier.getBytes())))); } catch (IOException e) { e.printStackTrace(); } } return Optional.of(response); }
public static void main(String[] args) throws Exception { conf.set("hbase.zookeeper.quorum", "hadoop271.itversity.com"); conf.set("hbase.zookeeper.property.clientPort", "2181"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("demo")); Scan scan1 = new Scan(); ResultScanner scanner1 = table.getScanner(scan1); for (Result res : scanner1) { System.out.println(Bytes.toString(res.getRow())); System.out.println(Bytes.toString(res.getValue("cf1".getBytes(), "column1".getBytes()))); System.out.println(Bytes.toString(res.getValue("cf1".getBytes(), "column2".getBytes()))); } scanner1.close(); Put put = new Put("3".getBytes()); put.addColumn("cf1".getBytes(), "column1".getBytes(), "value1".getBytes()); put.addColumn("cf1".getBytes(), "column2".getBytes(), "value2".getBytes()); table.put(put); Get get = new Get("3".getBytes()); Result getResult = table.get(get); System.out.println("Printing colunns for rowkey 3"); System.out.println(Bytes.toString(getResult.getValue("cf1".getBytes(), "column1".getBytes()))); System.out.println(Bytes.toString(getResult.getValue("cf1".getBytes(), "column2".getBytes()))); scanner1 = table.getScanner(scan1); System.out.println("Before Delete"); for (Result res : scanner1) { System.out.println(Bytes.toString(res.getRow())); System.out.println(Bytes.toString(res.getValue("cf1".getBytes(), "column1".getBytes()))); System.out.println(Bytes.toString(res.getValue("cf1".getBytes(), "column2".getBytes()))); } scanner1.close(); Delete del = new Delete("3".getBytes()); table.delete(del); System.out.println("After Delete"); scanner1 = table.getScanner(scan1); for (Result res : scanner1) { System.out.println(Bytes.toString(res.getRow())); System.out.println(Bytes.toString(res.getValue("cf1".getBytes(), "column1".getBytes()))); System.out.println(Bytes.toString(res.getValue("cf1".getBytes(), "column2".getBytes()))); } scanner1.close(); table.close(); connection.close(); }
public void put(String tablename, Put p) { try { final Table table = connection.getTable(TableName.valueOf(tablename)); table.put(p); table.close(); } catch (IOException e) { e.printStackTrace(); } }
/** * Confirm ImportTsv via data in online table. * * @param dataAvailable */ private static void validateTable( Configuration conf, TableName tableName, String family, int valueMultiplier, boolean dataAvailable) throws IOException { LOG.debug("Validating table."); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(tableName); boolean verified = false; long pause = conf.getLong("hbase.client.pause", 5 * 1000); int numRetries = conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 5); for (int i = 0; i < numRetries; i++) { try { Scan scan = new Scan(); // Scan entire family. scan.addFamily(Bytes.toBytes(family)); if (dataAvailable) { ResultScanner resScanner = table.getScanner(scan); for (Result res : resScanner) { LOG.debug("Getting results " + res.size()); assertTrue(res.size() == 2); List<Cell> kvs = res.listCells(); assertTrue(CellUtil.matchingRow(kvs.get(0), Bytes.toBytes("KEY"))); assertTrue(CellUtil.matchingRow(kvs.get(1), Bytes.toBytes("KEY"))); assertTrue( CellUtil.matchingValue(kvs.get(0), Bytes.toBytes("VALUE" + valueMultiplier))); assertTrue( CellUtil.matchingValue(kvs.get(1), Bytes.toBytes("VALUE" + 2 * valueMultiplier))); // Only one result set is expected, so let it loop. verified = true; } } else { ResultScanner resScanner = table.getScanner(scan); Result[] next = resScanner.next(2); assertEquals(0, next.length); verified = true; } break; } catch (NullPointerException e) { // If here, a cell was empty. Presume its because updates came in // after the scanner had been opened. Wait a while and retry. } try { Thread.sleep(pause); } catch (InterruptedException e) { // continue } } table.close(); connection.close(); assertTrue(verified); }
public Result get(String table, String family, byte[] key) { final Table htable; try { htable = connection.getTable(TableName.valueOf(table)); return htable.get(new Get(key)); } catch (IOException e) { e.printStackTrace(); } return null; }
private static void setup() { conf = HBaseConfiguration.create(); try { connection = ConnectionFactory.createConnection(conf); table = connection.getTable(TableName.valueOf(PropertyConstant.TABLENAME)); } catch (IOException e) { System.out.println("Error: while set up database configuration " + e); System.exit(-1); } }
public void doScan() throws IOException { Table tableRef = connection.getTable(TableName.valueOf(table)); Scan scan = new Scan(); ResultScanner scanner = tableRef.getScanner(scan); long now = System.currentTimeMillis(); if (verbose) System.out.println("Starting scan"); for (Result res : scanner) { if (verbose) System.out.println(res); } if (verbose) System.out.printf("Scan finished: %d ms\n\n", System.currentTimeMillis() - now); tableRef.close(); }
@Before public void init() { conf = HBaseConfiguration.create(); try { conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); tn = TableName.valueOf(tableName); table = conn.getTable(tn); initData(); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1", "colfam2"); System.out.println("Adding rows to table..."); helper.fillTable("testtable", 1, 10, 10, "colfam1", "colfam2"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); // vv SingleColumnValueFilterExample SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5")); filter.setFilterIfMissing(true); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); // ^^ SingleColumnValueFilterExample System.out.println("Results of scan:"); // vv SingleColumnValueFilterExample for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println( "Cell: " + cell + ", Value: " + Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } scanner.close(); Get get = new Get(Bytes.toBytes("row-6")); get.setFilter(filter); Result result = table.get(get); System.out.println("Result of get: "); for (Cell cell : result.rawCells()) { System.out.println( "Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } // ^^ SingleColumnValueFilterExample }
@Override protected void setup( Reducer<Key_IMOAndRecordTime, TextArrayWritable, NullWritable, NullWritable>.Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub connection = ConnectionFactory.createConnection(context.getConfiguration()); TableName VtLocation_Name = TableName.valueOf("cdb_vessel:vessel_location"); VTLocation = connection.getBufferedMutator(VtLocation_Name); VTLocation_Table = connection.getTable(VtLocation_Name); TableName VtEvent_Name = TableName.valueOf("cdb_vessel:vessel_event"); VTEvent = connection.getBufferedMutator(VtEvent_Name); VTEvent_Table = connection.getTable(VtEvent_Name); TableName Vessel_Name = TableName.valueOf("cdb_vessel:vessel"); Vessel_Table = connection.getTable(Vessel_Name); TableName LastLocation_Name = TableName.valueOf("cdb_vessel:latest_location"); LastLocation_BM = connection.getBufferedMutator(LastLocation_Name); TableName TrackInfo_Name = TableName.valueOf("cdb_vessel:vessel_track_info"); TrackInfo_BM = connection.getBufferedMutator(TrackInfo_Name); TrackInfo_Table = connection.getTable(TrackInfo_Name); try { File Zonemapfile = new File("VesselZone"); ObjectInputStream OIS = new ObjectInputStream(new FileInputStream(Zonemapfile)); Zonemap = (HashMap<Integer, VesselZone>) OIS.readObject(); OIS.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void main(String[] args) throws IOException { Configuration conf = HBaseClientHelper.loadDefaultConfiguration(); Connection connection = ConnectionFactory.createConnection(conf); try { Table table = connection.getTable(TableName.valueOf("testtable")); try { // 1 Put Put p = new Put(Bytes.toBytes("row1")); p.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1")); table.put(p); // 2 Get Get g = new Get(Bytes.toBytes("row1")); Result r = table.get(g); byte[] value = r.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); String valueStr = Bytes.toString(value); System.out.println("GET: " + valueStr); // 3 Scan Scan s = new Scan(); s.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); ResultScanner scanner = table.getScanner(s); try { for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { System.out.println("Found row: " + rr); } // The other approach is to use a foreach loop. Scanners are // iterable! // for (Result rr : scanner) { // System.out.println("Found row: " + rr); // } } finally { scanner.close(); } // Close your table and cluster connection. } finally { if (table != null) table.close(); } } finally { connection.close(); } }
/** * Initialize any state for this DB. Called once per DB instance; there is one DB instance per * client thread. */ @Override public void init() throws DBException { if ("true".equals(getProperties().getProperty("clientbuffering", "false"))) { this.clientSideBuffering = true; } if (getProperties().containsKey("writebuffersize")) { writeBufferSize = Long.parseLong(getProperties().getProperty("writebuffersize")); } if (getProperties().getProperty("durability") != null) { this.durability = Durability.valueOf(getProperties().getProperty("durability")); } try { connection = ConnectionFactory.createConnection(config); } catch (java.io.IOException e) { throw new DBException(e); } if ((getProperties().getProperty("debug") != null) && (getProperties().getProperty("debug").compareTo("true") == 0)) { debug = true; } if ("false".equals(getProperties().getProperty("hbase.usepagefilter", "true"))) { usePageFilter = false; } columnFamily = getProperties().getProperty("columnfamily"); if (columnFamily == null) { System.err.println("Error, must specify a columnfamily for HBase table"); throw new DBException("No columnfamily specified"); } columnFamilyBytes = Bytes.toBytes(columnFamily); // Terminate right now if table does not exist, since the client // will not propagate this error upstream once the workload // starts. String table = com.yahoo.ycsb.workloads.CoreWorkload.table; try { final TableName tName = TableName.valueOf(table); HTableDescriptor dsc = connection.getTable(tName).getTableDescriptor(); } catch (IOException e) { throw new DBException(e); } }
public void scan( Consumer<Result> callback, String tableName, String columnFamily, String... qualifiers) { if (callback != null && tableName != null && columnFamily != null && qualifiers != null) { Scan s = new Scan(); byte[] family = Bytes.toBytes(columnFamily); for (String qualifier : qualifiers) { s.addColumn(family, Bytes.toBytes(qualifier)); } try { final Table table = connection.getTable(TableName.valueOf(tableName)); ResultScanner scanner = table.getScanner(s); for (Result r = scanner.next(); r != null; r = scanner.next()) { callback.accept(r); } } catch (IOException e) { e.printStackTrace(); } } }
public void put(Optional<Request> putRequest) { if (!valid) { Logger.error("CANNOT PUT! NO VALID CONNECTION"); return; } putRequest.ifPresent( pr -> pr.getPut() .ifPresent( p -> { try { final Table table = connection.getTable(TableName.valueOf(pr.table)); table.put(p); table.close(); } catch (IOException e) { e.printStackTrace(); } })); }
/** * Add by linjy on 2016-01-06 * * @param totalCount 总数量 * @param city 城市名 保存分析每天,每个城市的职位数的数量的结果到HBase 中 */ private void saveDayCityPCount(List<Map<String, String>> resultLists) { // String dateTime = DateUtils.getYest0day("yyyyMMdd"); try { Connection conn = HBaseUtils.getConnection(); Table table = conn.getTable(TableName.valueOf("SalaryInfoResult".getBytes())); logger.info("保存每天,每个城市的职位数的数量的结果开始......统计结果数量:" + resultLists.size()); List<Put> puts = new ArrayList<Put>(); for (Map<String, String> map : resultLists) { String totalCount = map.get("totalcount"); String city = map.get("city"); String positionName = map.get("positionname"); String createDate = map.get("createdate"); String rowKey = createDate + getPostitionNameSX(positionName) + city; Put put = new Put(rowKey.getBytes()); put.addColumn( "ResultInfoFamily".getBytes(), "resultCount".getBytes(), totalCount.getBytes()); put.addColumn("ResultInfoFamily".getBytes(), "city".getBytes(), city.getBytes()); put.addColumn( "ResultInfoFamily".getBytes(), "positionName".getBytes(), positionName.getBytes()); put.addColumn( "ResultInfoFamily".getBytes(), "createDate".getBytes(), createDate.getBytes()); put.addColumn( "ResultInfoFamily".getBytes(), "insertDate".getBytes(), (DateUtils.getDateFormat(new Date(), "yyyyMMdd")).getBytes()); put.addColumn("ResultInfoFamily".getBytes(), "flag".getBytes(), "0".getBytes()); puts.add(put); // System.out.println(positionName + " " + rowKey); } // 插入数据 table.put(puts); // 刷新缓冲区 table.close(); logger.info("保存每天,每个城市的职位数的数量的结果结束......"); } catch (Exception e) { e.printStackTrace(); logger.error("出入结果数据到HBase总出错,出错原因:" + e.getMessage()); } }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); List<Put> puts = new ArrayList<Put>(); // vv PutListErrorExample2 Put put1 = new Put(Bytes.toBytes("row1")); put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1")); puts.add(put1); Put put2 = new Put(Bytes.toBytes("row2")); put2.addColumn(Bytes.toBytes("BOGUS"), Bytes.toBytes("qual1"), Bytes.toBytes("val2")); puts.add(put2); Put put3 = new Put(Bytes.toBytes("row2")); put3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"), Bytes.toBytes("val3")); puts.add(put3); /*[*/ Put put4 = new Put(Bytes.toBytes("row2")); puts.add( put4); /*]*/ // co PutListErrorExample2-1-AddErrorPut Add put with no content at all to // list. /*[*/ try { /*]*/ table.put(puts); /*[*/ } catch (Exception e) { System.err.println("Error: " + e); // table.flushCommits(); // todo: FIX! /*]*/ // co PutListErrorExample2-2-Catch Catch local exception and commit queued updates. /*[*/ } /*]*/ // ^^ PutListErrorExample2 table.close(); connection.close(); helper.close(); }
/** * 添加多条记录到表中 * * @param tableName * @param puts * @throws ParamIsNullException 参数为空 * @throws TableNotFoundException 表不存在/获取表连接失败 */ public static void insertRecords(String tableName, List<Put> puts) throws ParamIsNullException, TableNotFoundException { if (null == tableName) { throw new ParamIsNullException("tableName不能为空"); } if (null == puts) { throw new ParamIsNullException("Put不能为空"); } logger.info( "insertRecords begin......, tableName:{}, List<Put> size:{}", tableName, puts.size()); Connection conn = null; HTable hTable = null; try { conn = ConnectionFactory.createConnection(conf); hTable = (HTable) conn.getTable(TableName.valueOf(tableName)); if (null != hTable) { hTable.put(puts); logger.info("hTable.put(puts) ......"); } else { throw new TableNotFoundException(tableName); } } catch (Exception e) { logger.error("获取Hbase连接发生异常, errMsg:{}", e); } finally { if (null != hTable) { try { hTable.close(); } catch (IOException e) { logger.error("HTable close exception, errMsg:{}", e); } } if (null != conn) { try { conn.close(); } catch (IOException e) { logger.error("Connection close exception, errMsg:{}", e); } } } logger.info("insertRecords end ......"); }
public static int getTable(Configuration config) throws IOException { /** Connection to the cluster. A single connection shared by all application threads. */ Connection connection = null; /** A lightweight handle to a specific table. Used from a single thread. */ Table table = null; try { // establish the connection to the cluster. connection = ConnectionFactory.createConnection(config); // retrieve a handle to the target table. table = connection.getTable(TABLE_NAME1); // describe the data we want to write. Get g = new Get(Bytes.toBytes("row4")); // send the data. table.get(g); } finally { // close everything down if (table != null) table.close(); if (connection != null) connection.close(); } return 0; }
// select data // http://blog.csdn.net/cnweike/article/details/42920547 for more detail. public static String select( String tableName, String begin, String end, String colFamily, String column, String value) { String result = ""; TableName tn = TableName.valueOf(tableName); try { Table table = conn.getTable(tn); Scan scan = new Scan(Bytes.toBytes(begin), Bytes.toBytes(end)); SingleColumnValueFilter scvf = new SingleColumnValueFilter( Bytes.toBytes(colFamily), Bytes.toBytes(column), CompareFilter.CompareOp.EQUAL, new SubstringComparator(value)); scvf.setFilterIfMissing(false); scvf.setLatestVersionOnly(true); // OK scan.setFilter(scvf); ResultScanner scanner = table.getScanner(scan); List<Map<String, String>> total = new ArrayList<Map<String, String>>(); for (Result res : scanner) { Map<String, String> map = new HashMap<String, String>(); for (Cell cell : res.rawCells()) map.put( Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); total.add(map); } ObjectMapper mapper = new ObjectMapper(); result = mapper.writeValueAsString(total); } catch (IOException e) { e.printStackTrace(); } return result; }
// insert data public static void insert(String tableName, String key, Map<String, Map<String, String>> data) { TableName tn = TableName.valueOf(tableName); List<Put> puts = new ArrayList<Put>(); try { Table table = conn.getTable(tn); Put put = new Put(Bytes.toBytes(key)); for (Entry<String, Map<String, String>> colFamily : data.entrySet()) { String colFamilyName = colFamily.getKey(); for (Entry<String, String> attr : colFamily.getValue().entrySet()) { String attrName = attr.getKey(); String attrValue = attr.getValue(); put.addColumn( Bytes.toBytes(colFamilyName), Bytes.toBytes(attrName), Bytes.toBytes(attrValue)); puts.add(put); } table.put(puts); } } catch (IOException e) { e.printStackTrace(); } }
// select data // get data where its key value between two values. // this adapt for envinfo / measureinfo / weatherinfo. public static String select(String tableName, String begin, String end) { String result = ""; TableName tn = TableName.valueOf(tableName); try { Table table = conn.getTable(tn); Scan scan = new Scan(Bytes.toBytes(begin), Bytes.toBytes(end)); ResultScanner scanner = table.getScanner(scan); List<Map<String, String>> total = new ArrayList<Map<String, String>>(); for (Result res : scanner) { Map<String, String> map = new HashMap<String, String>(); for (Cell cell : res.rawCells()) map.put( Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); total.add(map); } ObjectMapper mapper = new ObjectMapper(); result = mapper.writeValueAsString(total); } catch (IOException e) { e.printStackTrace(); } return result; }
@SuppressWarnings("deprecation") public static int putTable(Configuration config) throws IOException { /** Connection to the cluster. A single connection shared by all application threads. */ Connection connection = null; /** A lightweight handle to a specific table. Used from a single thread. */ Table table = null; try { // establish the connection to the cluster. connection = ConnectionFactory.createConnection(config); // retrieve a handle to the target table. table = connection.getTable(TABLE_NAME1); // describe the data we want to write. Put p = new Put(Bytes.toBytes("row4")); p.add(CF, Bytes.toBytes("b"), Bytes.toBytes("datos agregados desde java")); p.add(CF, Bytes.toBytes("qual"), Bytes.toBytes("segunda columna")); // send the data. table.put(p); } finally { // close everything down if (table != null) table.close(); if (connection != null) connection.close(); } return 0; }
/** * 添加一条记录到表中 * * @param tableName * @param put * @throws ParamIsNullException 参数为空 * @throws IOException */ public void insertRecordOnline(Connection conn, String tableName, Put put) throws ParamIsNullException, IOException { if (null == tableName) { throw new ParamIsNullException("tableName不能为空"); } if (null == put) { throw new ParamIsNullException("Put不能为空"); } logger.info("insertOneRecord ..."); HTable hTable = null; try { if (null == conn) { conn = ConnectionFactory.createConnection(conf); } hTable = (HTable) conn.getTable(TableName.valueOf(tableName)); if (null != hTable) { hTable.put(put); } else { throw new TableNotFoundException(tableName); } } catch (Exception e) { logger.error("conn.getTable exception, errMsg:{}", e); } }