My local environment:
OS X 10.9.2, Hbase-0.94.17, Java 1.6
My Hbase mode: standalone
I was able to do operation in shell, but when I used java api, it did not work.
My java code:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
public class MyLittleHBaseClient {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
config.addResource("/Users/apple/Documents/tools/hbase-0.94.17/conf/hbase-site.xml");
HTable table = new HTable(config, "myLittleHBaseTable");
Put p = new Put(Bytes.toBytes("myLittleRow"));
p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
Bytes.toBytes("Some Value"));
table.put(p);
Get g = new Get(Bytes.toBytes("myLittleRow"));
Result r = table.get(g);
byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
Bytes.toBytes("someQualifier"));
String valueStr = Bytes.toString(value);
System.out.println("GET: " + valueStr);
Scan s = new Scan();
s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
ResultScanner scanner = table.getScanner(s);
try {
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
// print out the row we found and the columns we were looking for
System.out.println("Found row: " + rr);
}
} finally {
scanner.close();
}
}
}
Here is some of my conf doc:
###########################################hbase-env.sh:export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`
export HBASE_OPTS="-XX:+UseConcMarkSweepGC"
###########################################hbase-site.sh:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///Users/apple/Documents/tools/hbase-0.94.17/hbase-rootdir/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/Users/apple/Documents/tools/hbase-0.94.17/hbase-zookeeper/zookeeper</value>
</property>
</configuration>
When I ran the java code above, I got the following information:
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2014-03-31 13:20:15.037 java[873:1003] Unable to load realm info from SCDynamicStore
and then the console just stopped here and gave nothing more.(no more response)
Also I attach my /Users/apple/Documents/tools/hbase-0.94.17/logs/hbase-apple...local.log here:
https://docs.google.com/file/d/0BxtBre5A8J61Y2k3SXNtNGs1WXM/edit
Thanks for your patient, your answer will be a great help to me :)