cassandra 在window上的demo
Cassandra window使用
1. 下载:http://cassandra.apache.org/download/。
2. 解压后,bin目录下,cassandra.bat 直接启动。 Cassandra-cli.bat进入cassandra。
3. 安装python2.7.10 版本太高,可能会不兼容,安装python 使用 CQL 操作cassandra数据库。
4. 配置环境变量后,以管理员方式运行 cmd :
powershell Set-ExecutionPolicy Unrestricted 成功如下:

5. cmd cqlsh.bat进入cql编辑;(没有安装python 无法使用cqlsh.bat)
CREATE KEYSPACE IF NOT EXISTS pimin_net
WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':1};
USE pimin_net;
CREATE TABLE IF NOT EXISTS users (
id uuid,
first_name varchar,
last_name varchar,
age int,
emails set<text>,
avatar blob,
PRIMARY KEY (id)
);
SELECT * FROM users;
用户表(users)包含了主键、firest_name、last_name、年龄、多个邮箱地址以及头像。关于使用java驱动,建议使用eclipse+maven创建,比如我们创建一个cassandra.test的工程。在pom.xml里面加入
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.0</version>
</dependency>
|
1 2 3 4 5 |
<dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> <version>2.1.0</version> </dependency> |
如果环境不允许使用maven,那么就自己找依赖包吧:cassandra-driver-core-2.1.0.jar、netty-3.9.0.Final.jar、guava-16.0.1.jar、metrics-core-3.0.2.jar和slf4j-api-1.7.5.jar。
解压目录 lib和 apache-cassandra-2.1.16\tools\lib 下,有jar包。
下面是所有java测试代码:实现连接Cassandra集群,新增数据,查询数据。其他的情况类似。
如果需要远程连接,那么cassandra.yaml里面的rpc_address就需要改变为您当前环境实际的IP地址,否则会报错! 本地:client.connect("127.0.0.1");
package cassandra;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
public class TestJava {
private Cluster cluster;
private Session session;
public Cluster getCluster() {
return cluster;
}
public void setCluster(Cluster cluster) {
this.cluster = cluster;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
/**
* 连接节点
*
* @param node
*/
public void connect(String node) {
cluster = Cluster.builder().addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());
for (Host host : metadata.getAllHosts()) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack());
}
this.session = cluster.connect();
}
public void insertData() {
PreparedStatement insertStatement = getSession().prepare(
"INSERT INTO pimin_net.users "
+ "(id, first_name, last_name, age, emails,avatar) "
+ "VALUES (?, ?, ?, ?, ?, ?);");
BoundStatement boundStatement = new BoundStatement(insertStatement);
Set<String> emails = new HashSet<String>();
emails.add("xxx@qq.com");
emails.add("xxx@163.com");
java.nio.ByteBuffer avatar = null;
try {
avatar = toByteBuffer("f:\\user.png");
avatar.flip();
System.out.println("头像大小:" + avatar.capacity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getSession().execute(boundStatement.bind(UUID.fromString("756716f7-2e54-4715-9f00-91dcbea6cf50"),
"pi", "min", 10, emails, avatar));
}
public void loadData() {
ResultSet resultSet = getSession().execute(
"SELECT first_name,last_name,age,avatar FROM pimin_net.users;");
System.out
.println(String
.format("%-30s\t%-20s\t%-20s\n%s", "first_name","last_name", "age",
"-------------------------------+-----------------------+--------------------"));
for (Row row : resultSet) {
System.out.println(String.format("%-30s\t%-20s\t%-20s",
row.getString("first_name"), row.getString("last_name"),
row.getInt("age")));
ByteBuffer byteBuffer = row.getBytes("avatar");
System.out.println("头像大小:" + (byteBuffer.limit() - byteBuffer.position()));
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("f:\\2.png");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOutputStream.write(byteBuffer.array(),
byteBuffer.position(),
byteBuffer.limit() - byteBuffer.position());
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println();
}
public void close() {
cluster.close();
}
/**
* 读取文件
*
* @param filename
* @return
* @throws IOException
*/
public static ByteBuffer toByteBuffer(String filename) throws IOException {
File f = new File(filename);
if (!f.exists()) {
throw new FileNotFoundException(filename);
}
FileChannel channel = null;
FileInputStream fs = null;
try {
fs = new FileInputStream(f);
channel = fs.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
while ((channel.read(byteBuffer)) > 0) {
// do nothing
// System.out.println("reading");
}
return byteBuffer;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
TestJava client = new TestJava();
client.connect("127.0.0.1");
client.insertData();
client.loadData();
client.session.close();
client.close();
}
}
cassandra 在window上的demo的更多相关文章
- jquery.form 和MVC4做无刷新上传DEMO
jquery.form 和MVC4做无刷新上传DEMO HTML: <script src="~/Scripts/jquery-1.10.2.min.js"></ ...
- TensorFlow 在android上的Demo(1)
转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...
- window上利用pip安装pandas
官网推荐的是直接使用Anoconda,它集成了pandas,可以直接使用.安装挺简单的,有windows下的安装包.如果不想安装庞大的Anoconda,那就一步一步用pip来安装pandas.下面我主 ...
- 在window上安装pandas
之前在ubuntu上安装pandas,用的easy_install.这次在window上同样方法装遇到"unable to find vcvarsall.bat",看一些网上帖子好 ...
- iOS UIButton加在window上点击无效果问题
UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible]; self.window = [[UIWindow al ...
- 【原】window上安装elasticserach
[window上安装elasticserach] 系统环境:2008R2 x64测试安装用的服务器IP:192.168.12.52elasticsearch版本:2.3.4JDK版本:jdk 1.8. ...
- 往github上传demo
一直在github上寻找demo,但怎么传demo上githun呢? http://www.2cto.com/kf/201504/390397.html 首先在github上 new一个reposit ...
- 关于Windows高DPI的一些简单总结(Window上一般默认是96 dpi 作为100% 的缩放比率)
我们知道,关于高DPI的支持, Windows XP时代就开始有了, 那时关于高DPI的支持比较简单, 但是从Vista/Win7 到现在Win8 /Win8.1, Windows关于高DPI的支持已 ...
- Jetson TX2上的demo(原创)
Jetson TX2上的demo 一.快速傅里叶-海动图 sample The CUDA samples directory is copied to the home directory on th ...
随机推荐
- Nginx实现文件的上传和下载
文件的上传只要保证特殊的地址先到达Nginx,然后通过Nginx指定至指定的服务器即可,目前配置是本机.文件的下载的做法就是把本机的当前目录下面的文件给返回回去. server { ; server_ ...
- 自定义UICollectionViewLayout 布局实现瀑布流
自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...
- GitHub Desktop 如何创建本地仓库,上传代码,删除仓库
1.创建本地仓库 2.打开本地仓库,将要上传的文件放到本地仓库. 3.ctrl+p push仓库或者菜单栏Repository下push也可以用右上角的publish respository 4.左边 ...
- css3-------:before和:after的作用
1.:before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: <!doctype html> ...
- char 与 String 相等比较
这是一个相当2 相当基础 相当没有意义的帖子:但今天因为这个问题引发了一个bug.小细节也很重要!!! char a='1'; // char b='2dsf'; //cha ...
- 关于eclipse运行TestNG出现: CreateProcess error=206, ÎļþÃû»ò)չÃû的解决办法
最近玩物流宝的一个项目,需要测试下3个系统打通的接口. 不测不要紧,一测吓一跳.我的乖乖:几百个bean被加进来.就凭我这肉机,内存不爆才怪. 于是换一套方案,用了另一个测试接口. 但是这个测试接口, ...
- [坑况]——windows升级node最新版本报错【npm install -g n】
我本来是下载一个vue-cli的,然后技术日新月异,告知我要先把我的node升级到8以上(目前是v6.1.13) 升级就升级,升级就报错 尝试第一种方法,网上最多的一种方法,估计也是成功最多的一种吧( ...
- P2453 [SDOI2006]最短距离
题目描述 一种EDIT字母编辑器,它的功能是可以通过不同的变换操作可以把一个源串X [l..m]变换为新的目标串y[1..n].EDIT提供的变换操作有: 源串中的单个字符可被删除(delete): ...
- MySQL 8 新特性之Invisible Indexes
背景 索引是把双刃剑,在提升查询速度的同时会减慢DML的操作.毕竟,索引的维护需要一定的成本.所以,对于索引,要加上该加的,删除无用的.前者是加法,后者是减法.但在实际工作中,大家似乎更热衷于前者,而 ...
- openssh升级的坑爹之路
安装Zlib http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure --sha ...