IDEA下使用JAVA远程操作linux下的Hbase
在Windows下远程连接操作Linux下的Hbase,采坑过程中遇到的问题和关键注意点记录如下
Centos7 下的配置如下:
hostname:tiger , IP =192.168.0.1
Hbase 版本1.2.*
1 在windows下的hosts文件(C:\Windows\System32\drivers\etc下)一定要添加linux的主机名和地址
192.168.0.1 tiger
2 HBase默认的端口是16000和16010,不同版本的具体的值可以在zookeeper命令行使用如下命令查看
get /hbase
3 linux下的Hbase的 hbase-site.xml 配置如下:
hbase.rootdir
hdfs://10.16.5.188:9000/hbase
The directory shared byregion servers.
hbase.zookeeper.property.clientPort
2181
Property from ZooKeeper'sconfig zoo.cfg. The port at which the clients
will connect.
zookeeper.session.timeout
10000
hbase.zookeeper.quorum
10.16.5.188
hbase.zookeeper.property.dataDir
/home/hadoop/software/zookeeper/zooKeeperData
hbase.tmp.dir
/home/hadoop/hbase/tmp
hbase.cluster.distributed
true
具体的配置参数请查阅官网,上面的参数是足够的,上面参数设置完成后对于在Linux本机操作Hbase 是没问题的,
如果想在远程主机通过Java或者scala操作Hbase需要在hbase的配置hbase-site.xml添加下面额外的配置,否则你可能陷入连接超时,重试超时各种查阅资料无法得到正确解决方案的苦恼中。
hbase.master.ipc.address
0.0.0.0
hbase.regionserver.ipc.address
0.0.0.0
具体需要增加此配置的原因请参考:
4 按照如下的顺序启动Hbase
hadoop ->zookeeper->hbase 顺序启动
5 使用Java 连接Hbase
其中如果在工程的resources下把linux下的hbase的配置文件hbase-site.xml 拷贝过来,那么
configuration=HBaseConfiguration.create();一句代码完成配置,如果不在工程下增加hbase-site.xml ,那么需要使用多行代码配置连接
configuration=HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","192.168.0.1:2181");
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.apache.hadoop.hbase.KeyValueUtil.copyToNewKeyValue;
public class HbaseTestCase
{
public static Configuration configuration;
static
{
configuration=HBaseConfiguration.create();
//configuration.set("hbase.master","10.16.5.188:16000");
// configuration.set("hbase.zookeeper.quorum","10.16.5.188:2181");
}
public static void createTable(String tablename) throws Exception
{
HBaseAdmin admin=new HBaseAdmin(configuration);
if(admin.tableExists(tablename))
{
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.println("开始创建表");
}
HTableDescriptor tableDescriptor=new HTableDescriptor(tablename);
tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
admin.createTable(tableDescriptor);
Put put1=new Put("123".getBytes());
put1.add("cf1".getBytes(),"column1".getBytes(),"value1".getBytes());
put1.add("cf1".getBytes(),"column2".getBytes(),"value2".getBytes());
put1.add("cf1".getBytes(),"column3".getBytes(),"value3".getBytes());
HTable table=new HTable(configuration,tablename);
table.put(put1);
System.out.println("创建表成功。。。。");
}
public static void main(String[] args)
{
try {
//createTable("TT");
Connection conn = ConnectionFactory.createConnection(configuration);
Admin admin = null;
try {
admin = conn.getAdmin();
if (!admin.tableExists(TableName.valueOf("wangxuetaotest"))) {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("wangxuetaotest"));
tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
admin.createTable(tableDescriptor);
}
Table table = conn.getTable(TableName.valueOf("wangxuetaotest"));
Put put = new Put(Bytes.toBytes("r1")); //行的puttos
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("3"));
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("4"));
byte [] temp=Bytes.toBytes(3);
table.put(put);
Get get = new Get(Bytes.toBytes("r1"));
Result rs = table.get(get);
byte[] value= rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("c1"));
String ValueString=Bytes.toString(value);
System.out.println("值。。。。"+ValueString);
Map datamap=new HashMap<>();
for (KeyValue kv : rs.raw()) {
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(new String(kv.getValue()) + " ");
System.out.println(kv.getTimestamp() + " ");
}
List cells=rs.listCells();
for (Cell kv : cells) {
System.out.print(new String(kv.getQualifierArray()) + " ");
System.out.print(new String(kv.getValueArray()) + " ");
System.out.println(kv.getTimestamp() + " ");
}
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22@@@");
Filter filter=new SingleColumnValueFilter(Bytes.toBytes("cf1"),Bytes.toBytes("c1"),CompareFilter.CompareOp.EQUAL,Bytes.toBytes("3"));
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner scanner=table.getScanner(scan);
for(Result result:scanner)
{
byte[] row=result.getRow();
List cells2=result.listCells();
for (Cell kv : cells2) {
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(new String(kv.getValue()) + " ");
System.out.println(kv.getTimestamp() + " ");
}
}
admin.close();
conn.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
System.out.println("创建表成功。。。。");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
| |
6 Hbase 在非集群模式下,是不能使用外部zookeeper的,只有在hbase.cluster.distribute为ture是才能使用外部Zookeeper
7 待续。。