Created
July 12, 2016 12:50
-
-
Save m-manu/1efade3316fce1845d88573df5b964c0 to your computer and use it in GitHub Desktop.
Check if you are able to access HBase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.hbase.HBaseConfiguration; | |
import org.apache.hadoop.hbase.KeyValue; | |
import org.apache.hadoop.hbase.client.*; | |
import org.apache.hadoop.hbase.ipc.HBaseRPC; | |
import org.apache.hadoop.hbase.ipc.HMasterInterface; | |
import org.apache.hadoop.hbase.ipc.RpcEngine; | |
import org.apache.hadoop.hbase.util.Bytes; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.util.*; | |
public class HBaseDemo { | |
public static void putSampleData(HTable hTable, String colFamilyName, int numRows, int numColumns) throws IOException { | |
List<Put> puts = new LinkedList<>(); | |
for (int i = 1; i <= numRows; i++) { | |
Put put = new Put(Bytes.toBytes("row" + i)); | |
for (int j = 1; j <= numColumns; j++) { | |
put.add(Bytes.toBytes(colFamilyName), Bytes.toBytes("column" + j), Bytes.toBytes("val" + j + "_" + i)); | |
} | |
put.add(Bytes.toBytes(colFamilyName), Bytes.toBytes("counter"), Bytes.toBytes(i)); | |
puts.add(put); | |
} | |
hTable.put(puts); | |
} | |
public static void putData(HTable hTable, String rowKey, String colFamilyName, String colName, String colValue) throws IOException { | |
Put put = new Put(Bytes.toBytes(rowKey)); | |
put.add(Bytes.toBytes(colFamilyName), Bytes.toBytes(colName), Bytes.toBytes(colValue)); | |
hTable.put(put); | |
} | |
public static void getRowDataAndPrint(HTable hTable, String rowKey) throws IOException { | |
System.out.println(rowKey); | |
Get get = new Get(Bytes.toBytes(rowKey)); | |
get.setMaxVersions(); | |
Result rows = hTable.get(get); | |
readMapAndPrint(rows.getMap()); | |
} | |
public static void readMapAndPrint(NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map) { | |
for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> row : map.entrySet()) { | |
String colFamilyName = Bytes.toString(row.getKey()); | |
NavigableMap<byte[], NavigableMap<Long, byte[]>> columns = row.getValue(); | |
for (Map.Entry<byte[], NavigableMap<Long, byte[]>> column : columns.entrySet()) { | |
String colName = Bytes.toString(column.getKey()); | |
System.out.print("\t" + colFamilyName + ":" + colName + " "); | |
NavigableMap<Long, byte[]> columnValues = column.getValue(); | |
System.out.print("{"); | |
for (Map.Entry columnValue : columnValues.entrySet()) { | |
Long timestamp = (Long) columnValue.getKey(); | |
String colValue = Bytes.toString((byte[]) columnValue.getValue()); | |
System.out.print(timestamp + ": \"" + colValue + "\", "); | |
} | |
System.out.println("}"); | |
} | |
} | |
} | |
public static void scanAndPrintTable(HTable hTable) throws IOException { | |
Scan scan = new Scan(); | |
scan.setMaxVersions(); | |
ResultScanner scanner = hTable.getScanner(scan); | |
for (Result result : scanner) { | |
String rowKey = Bytes.toString(result.getRow()); | |
System.out.println(rowKey + ": "); | |
readMapAndPrint(result.getMap()); | |
} | |
} | |
public static Map<String, String> get(Configuration conf, String hTable, | |
Collection<String> rowKeys, String family, String column) throws IOException { | |
List<Get> gets = new ArrayList<Get>(rowKeys.size()); | |
for (String rowKey : rowKeys) { | |
Get get = new Get(Bytes.toBytes(rowKey)); | |
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column)); | |
gets.add(get); | |
} | |
HTable signals = new HTable(conf, hTable); | |
Result[] results = signals.get(gets); | |
Map<String, String> map = new HashMap<String, String>(rowKeys.size()); | |
for (Result result : results) { | |
KeyValue kv = result.getColumnLatest(Bytes.toBytes(family), Bytes.toBytes(column)); | |
if (kv == null) | |
continue; | |
map.put(Bytes.toString(kv.getRow()), Bytes.toString(kv.getValue())); | |
} | |
return map; | |
} | |
public static void main(String args[]) throws Exception { | |
/* | |
HBase command: | |
newInstance 't1', {NAME => 'f1', VERSIONS => 3} | |
*/ | |
Configuration conf = HBaseConfiguration.create(); | |
conf.setInt("timeout", 120000); | |
conf.set("hbase.master", "localhost:60000"); | |
conf.set("hbase.zookeeper.quorum", "localhost"); | |
conf.set("hbase.zookeeper.property.clientPort", "2181"); | |
if (!isHbaseConnectionAlive(conf)) { | |
System.out.println("Sorry, couldn't connect to HBase"); | |
System.exit(1); | |
} | |
HTable hTable = new HTable(conf, "employees"); | |
putSampleData(hTable, "f1", 100, 6); | |
hTable.incrementColumnValue("row1".getBytes(), "f1".getBytes(), "counter".getBytes(), 6L); | |
scanAndPrintTable(hTable); | |
} | |
public static boolean isHbaseConnectionAlive(Configuration conf) { | |
boolean result = false; | |
RpcEngine rpcEngine = null; | |
try { | |
InetSocketAddress isa = new InetSocketAddress("localhost", 60000); | |
rpcEngine = HBaseRPC.getProtocolEngine(conf); | |
HMasterInterface master = rpcEngine.getProxy(HMasterInterface.class, HMasterInterface.VERSION, isa, conf, 1000); | |
result = master.isMasterRunning(); | |
} catch (Exception e) { | |
// ... | |
} finally { | |
if (rpcEngine != null) { | |
rpcEngine.close(); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment