
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class KeySalter {
private AtomicInteger index = new AtomicInteger(0);
private String[] prefixes = {"a", "b", "c", "d"};
public String getRowKey(String originalKey) {
StringBuilder sb = new StringBuilder(prefixes[index.incrementAndGet() % 4]);
sb.append("-").append(originalKey);
return sb.toString();
}
public List<String> getAllRowKeys(String originalKey) {
List<String> allKeys = new ArrayList<>();
for (String prefix : prefixes) {
StringBuilder sb = new StringBuilder(prefix);
sb.append("-").append(originalKey);
allKeys.add(sb.toString());
}
//a-boo0001
//b-boo0001
//c-boo0001
//d-boo0001
return allKeys;
}
}
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SaltingTest {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
try (Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("test_salt"))) {
KeySalter keySalter = new KeySalter();
List<String> rowkeys = Arrays.asList("boo0001", "boo0002", "boo0003", "boo0004");
List<Put> puts = new ArrayList<>();
for (String key : rowkeys) {
Put put = new Put(Bytes.toBytes(keySalter.getRowKey(key)));
put.addColumn(Bytes.toBytes("f"), null, Bytes.toBytes("value" + key));
puts.add(put);
}
table.put(puts);
}
}
}
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SaltingGetter {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
try (Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("test_salt"))) {
KeySalter keySalter = new KeySalter();
List<String> allKeys = keySalter.getAllRowKeys("boo0001"); //读取boo001
List<Get> gets = new ArrayList<>();
for (String key : allKeys) {
Get get = new Get(Bytes.toBytes(key));
gets.add(get);
}
Result[] results = table.get(gets);
for (Result result : results) {
if (result != null) {
//do something
}
}
}
}
}
import org.apache.hadoop.hbase.util.MD5Hash;
public class KeyHasher {
public static String getRowKey(String originalKey) {
return MD5Hash.getMD5AsHex(originalKey.getBytes());
}
}
package com.twq.hbase.rowkey.hash;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class HashingTest {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
try (Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("test_hash"))) {
List<String> rowkeys = Arrays.asList("boo0001", "boo0002", "boo0003", "boo0004");
List<Put> puts = new ArrayList<>();
for (String key : rowkeys) {
Put put = new Put(Bytes.toBytes(KeyHasher.getRowKey(key)));
put.addColumn(Bytes.toBytes("f"), null, Bytes.toBytes("value" + key));
puts.add(put);
}
table.put(puts);
}
}
}
import com.twq.hbase.rowkey.salt.KeySalter;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HashingGetter {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
try (Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("test_hash"))) {
Get get = new Get(Bytes.toBytes(KeyHasher.getRowKey("boo0001")));
Result results = table.get(get);
// process result...
for (Cell cell : results.listCells()) {
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "===> " +
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
Bytes.toString(CellUtil.cloneQualifier(cell)) + "{" +
Bytes.toString(CellUtil.cloneValue(cell)) + "}");
}
}
}
}

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class DataFilter {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
//Add any necessary configuration files (hbase-site.xml, core-site.xml)
config.addResource(new Path("src/main/resources/hbase-site.xml"));
config.addResource(new Path("src/main/resources/core-site.xml"));
try(Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("sound"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("00000120120901"));
scan.setStopRow(Bytes.toBytes("00000120121001"));
SingleColumnValueFilter nameFilter = new SingleColumnValueFilter(Bytes.toBytes("f"), Bytes.toBytes("n"),
CompareFilter.CompareOp.EQUAL, new SubstringComparator("中国好声音"));
SingleColumnValueFilter categoryFilter = new SingleColumnValueFilter(Bytes.toBytes("f"), Bytes.toBytes("c"),
CompareFilter.CompareOp.EQUAL, new SubstringComparator("综艺"));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(nameFilter);
filterList.addFilter(categoryFilter);
scan.setFilter(filterList);
ResultScanner rs = table.getScanner(scan);
try {
for (Result r = rs.next(); r != null; r = rs.next()) {
// process result...
for (Cell cell : r.listCells()) {
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "===> " +
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
Bytes.toString(CellUtil.cloneQualifier(cell)) + "{" +
Bytes.toString(CellUtil.cloneValue(cell)) + "}");
}
}
} finally {
rs.close(); // always close the ResultScanner!
}
}
}
}
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* create ‘sound‘,
*/
public class DataPrepare {
public static void main(String[] args) throws IOException {
InputStream ins = DataPrepare.class.getClassLoader().getResourceAsStream("sound.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
List<SoundInfo> soundInfos = new ArrayList<>();
String line = null;
while ((line = br.readLine()) != null) {
SoundInfo soundInfo = new SoundInfo();
String[] arr = line.split("\\|");
String rowkey = format(arr[4], 6) + arr[1] + format(arr[0], 6);
soundInfo.setRowkey(rowkey);
soundInfo.setName(arr[2]);
soundInfo.setCategory(arr[3]);
soundInfos.add(soundInfo);
}
Configuration config = HBaseConfiguration.create();
//Add any necessary configuration files (hbase-site.xml, core-site.xml)
config.addResource(new Path("src/main/resources/hbase-site.xml"));
config.addResource(new Path("src/main/resources/core-site.xml"));
try (Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("sound"));
List<Put> puts = new ArrayList<>();
for (SoundInfo soundInfo : soundInfos) {
Put put = new Put(Bytes.toBytes(soundInfo.getRowkey()));
put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("n"), Bytes.toBytes(soundInfo.getName()));
put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("c"), Bytes.toBytes(soundInfo.getCategory()));
puts.add(put);
}
table.put(puts);
}
}
public static String format(String str, int num) {
return String.format("%0" + num + "d", Integer.parseInt(str));
}
}
原文:https://www.cnblogs.com/tesla-turing/p/11490889.html