• hbase表数据内容重新改写


    停止表继续插入

    hbase shell>disable 'tbname'

    制作快照
    hbase shell> snapshot 'tbname', 'tbsnap'

    克隆快照为新的名字
    hbase shell> clone_snapshot 'tbsnap', 'newtb'

    删除快照
    hbase shell> delete_snapshot 'tableSnapshot'

    编写数据转换程序

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.lang.StringUtils;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.HTableInterface;
    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.filter.FilterList;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.springframework.data.hadoop.hbase.HbaseTemplate;
    
    import com.alibaba.fastjson.JSONObject;
    
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class HbaseUtil {
        
        public static void saveTemperature2hbase(String tbname,String rowKey, String v
                , HbaseTemplate hbaseTemplate) {
            hbaseTemplate.execute(tbname, (table) -> {
                Put put = new Put(Bytes.toBytes(rowKey));
                put.addColumn(Bytes.toBytes("XXX"), Bytes.toBytes("XXX"), Bytes.toBytes(v));
                table.put(put);
                return true;
            });
        }
        
           public static Map<String, List<Map<String, Object>>> getColumnDataFromHbase(String table
                   , String family, String column, HbaseTemplate hbaseTemplate) {
    
                return hbaseTemplate.execute(table, hTableInterface -> {
                    Scan scan = new Scan();
                    scan.setCacheBlocks(true);
                    scan.setCaching(30000);
                    scan.setBatch(300);
    
                    scan.addColumn(family.getBytes(), column.getBytes());
    
                    FilterList filterList = new FilterList();
    
    
                    scan.setFilter(filterList);
                    Map<String, List<Map<String, Object>>> tempData = new HashMap<>();
    
                    ResultScanner scanner = null;
    
                    try {
                        scanner = hTableInterface.getScanner(scan);
    
                        for (Result result : scanner) {
                            byte[] row = result.getRow();
                            String rowKey = Bytes.toString(row);
    
                            List<Cell> cells = result.listCells();
                            for (Cell cell : cells) {
                                Map<String, Object> data = new HashMap<>();
                                String id = Bytes.toString(CellUtil.cloneQualifier(cell));
                                String value = Bytes.toString(CellUtil.cloneValue(cell));
                                
                                log.info("rowKey:{}, column:{}, value:{}", rowKey, id, value);
                                
                                JSONObject json = new JSONObject();
                                
                                // 填写 json
                                
                                String jstr = json.toJSONString();
                                log.info(jstr);
                                
                                saveTemperature2hbase("XXX", rowKey, jstr, hbaseTemplate);
                            }
                        }
                    } finally {
                        if (null != scanner)
                            scanner.close();
                    }
                    return tempData;
                });
            }
           
           public static void main(String[] args) {
                HbaseTemplate hbaseTemplate = new HbaseTemplate();
                org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
                conf.set("hbase.zookeeper.quorum", "127.0.0.1");
                conf.set("hbase.zookeeper.port", "2181");
                hbaseTemplate.setConfiguration(conf);
                hbaseTemplate.setAutoFlush(true);
                
                getColumnDataFromHbase("XXX", "XXX", "XXX", hbaseTemplate);
           }
    }                

    删除原来表
    hbase shell> drop 'tableName'

  • 相关阅读:
    【题解】【神奇校内POIN模拟赛】小凯的疑惑
    【题解】【CF670C】cinema
    11 Remove Duplicates from Sorted Array II
    10 3SumClosest
    7 Container With Most Water
    8 String to Integer (atoi)
    9 3Sum
    6 Palindrome Number
    5 Zigzag Conversion
    4 Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/m2492565210/p/11343879.html
Copyright © 2020-2023  润新知