Copy Cell From Existing To New Column Family
package copyrecord;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
/**
* Created with IntelliJ IDEA.
* User: Hadoop Share
* Date: 29/11/13
* Time: 1:52 PM
* To change this template use File | Settings | File Templates.
*/
public class CopyCellFromExistingToNewFamily {
private static Configuration configuration = HBaseConfiguration.create();
static {
configuration.set("hbase.zookeeper.quorum","localhost");
configuration.set("hbase.zookeeper.property.port","2181");
}
private void copyCellFromOneToAnotherFamily(String tableName, String currentFamily, String toBeFamily) throws IOException {
HTableInterface table = new HTable(configuration,tableName);
Scan scan = new Scan();
scan.addFamily(currentFamily.getBytes());
Filter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("20131022:")); // fetch all records for a certain date
Filter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("REQUIRED_QUALIFIER:")); // fetch required qualifier
FilterList filterList = new FilterList();
filterList.addFilter(rowFilter);
filterList.addFilter(qualifierFilter);
scan.setFilter(filterList);
ResultScanner resultScanner = table.getScanner(scan);
List<Put> putList = new ArrayList<Put>();
for(Result result:resultScanner){
NavigableMap<byte[],byte[]> map = result.getFamilyMap(currentFamily.getBytes());
String qualifier = null;
String value = null;
for(byte[]key:map.keySet()){
qualifier = Bytes.toString(key);
value = Bytes.toString(map.get(key));
System.out.println(Bytes.toString(result.getRow()) + " :: " + " QUALIFIER: " + qualifier +" VALUE: " + value) ;
Put put = new Put(result.getRow());
put.add(toBeFamily.getBytes(),qualifier.getBytes(),value.getBytes()) ;
putList.add(put);
}
}
table.put(putList);
}
public static void main(String[] args) {
CopyCellFromExistingToNewFamily copyCellFromExistingToNewFamily = new CopyCellFromExistingToNewFamily();
try {
copyCellFromExistingToNewFamily.copyCellFromOneToAnotherFamily("TABLE_NAME","EXISTING_FAMILY","NEW_FAMILY");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
package copyrecord;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
/**
* Created with IntelliJ IDEA.
* User: Hadoop Share
* Date: 29/11/13
* Time: 1:52 PM
* To change this template use File | Settings | File Templates.
*/
public class CopyCellFromExistingToNewFamily {
private static Configuration configuration = HBaseConfiguration.create();
static {
configuration.set("hbase.zookeeper.quorum","localhost");
configuration.set("hbase.zookeeper.property.port","2181");
}
private void copyCellFromOneToAnotherFamily(String tableName, String currentFamily, String toBeFamily) throws IOException {
HTableInterface table = new HTable(configuration,tableName);
Scan scan = new Scan();
scan.addFamily(currentFamily.getBytes());
Filter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("20131022:")); // fetch all records for a certain date
Filter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("REQUIRED_QUALIFIER:")); // fetch required qualifier
FilterList filterList = new FilterList();
filterList.addFilter(rowFilter);
filterList.addFilter(qualifierFilter);
scan.setFilter(filterList);
ResultScanner resultScanner = table.getScanner(scan);
List<Put> putList = new ArrayList<Put>();
for(Result result:resultScanner){
NavigableMap<byte[],byte[]> map = result.getFamilyMap(currentFamily.getBytes());
String qualifier = null;
String value = null;
for(byte[]key:map.keySet()){
qualifier = Bytes.toString(key);
value = Bytes.toString(map.get(key));
System.out.println(Bytes.toString(result.getRow()) + " :: " + " QUALIFIER: " + qualifier +" VALUE: " + value) ;
Put put = new Put(result.getRow());
put.add(toBeFamily.getBytes(),qualifier.getBytes(),value.getBytes()) ;
putList.add(put);
}
}
table.put(putList);
}
public static void main(String[] args) {
CopyCellFromExistingToNewFamily copyCellFromExistingToNewFamily = new CopyCellFromExistingToNewFamily();
try {
copyCellFromExistingToNewFamily.copyCellFromOneToAnotherFamily("TABLE_NAME","EXISTING_FAMILY","NEW_FAMILY");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
No comments:
Post a Comment