from:http://blog.csdn.net/q2365921/article/details/54911890
上一篇说道了如何TitanServer的发布,那么这一章就来说说如何用JavaApI操作Titan,话不多说直接上代码
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.run.tatin</groupId>
<artifactId>tatindemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tatindemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<titan.version>1.0.0</titan.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- titan核心依赖jar -->
<dependency>
<groupId>com.thinkaurelius.titan</groupId>
<artifactId>titan-core</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- titan——Solr-Index 依赖jar -->
<dependency>
<groupId>com.thinkaurelius.titan</groupId>
<artifactId>titan-solr</artifactId>
<version>${titan.version}</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- titan——Hbase-Backed 依赖jar -->
<dependency>
<groupId>com.thinkaurelius.titan</groupId>
<artifactId>titan-hbase</artifactId>
<version>${titan.version}</version>
</dependency>
<!-- 小鬼驱动jar -->
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.0.1-incubating</version>
</dependency>
<!-- hbaseclient版本对应的jar -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.0.0-cdh5.4.8</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 指定guava的版本 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0.1</version>
</dependency>
</dependencies>
<!-- Maven仓库 -->
<repositories>
<repository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>cloudera cdh</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>apache snapshots</id>
<url>https://repository.apache.org/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>sonatypereleases</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
TestTian.java
public class TestTitan {
public static final String INDEX_NAME = "search";
public static void main(String[] args) {
TitanFactory.Builder config = TitanFactory.build();
config.set("storage.backend", "hbase");
config.set("storage.hostname", "hadoop1,hadoop2,hadoop3,hadoop4,hadoop5");
config.set("cache.db-cache", "true");
config.set("cache.db-cache-clean-wait", "20");
config.set("cache.db-cache-time", "180000");
config.set("cache.db-cache-size", "0.5");
config.set("index.search.backend", "solr");
config.set("index.search.solr.mode", "cloud");
config.set("index.search.solr.zookeeper-url", "solr1:2181,solr2:2181,solr3:2181,solr4:2181,solr5:2181");
config.set("index.search.solr.configset", "titan");
TitanGraph graph = config.open();
System.out.println("--------------------" + graph);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
上面就是获取Titan图实例的代码,具体如何创建顶点、边的代码,笔者还在研究。
在研究过程中遇到的问题
一、
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Stopwatch.elapsedTime(Ljava/util/concurrent/TimeUnit;)J
at com.google.common.cache.LocalCache$LoadingValueReference.elapsedNanos(LocalCache.java:3600)
at com.google.common.cache.LocalCache$Segment.getAndRecordStats(LocalCache.java:2412)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2373)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2335)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2250)
at com.google.common.cache.LocalCache.get(LocalCache.java:3985)
at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4788)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx$6$6.call(StandardTitanTx.java:1244)
at com.thinkaurelius.titan.graphdb.query.QueryUtil.processIntersectingRetrievals(QueryUtil.java:268)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx$6.execute(StandardTitanTx.java:1258)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx$6.execute(StandardTitanTx.java:1126)
at com.thinkaurelius.titan.graphdb.query.QueryProcessor$LimitAdjustingIterator.getNewIterator(QueryProcessor.java:198)
at com.thinkaurelius.titan.graphdb.query.LimitAdjustingIterator.hasNext(LimitAdjustingIterator.java:54)
at com.thinkaurelius.titan.graphdb.query.ResultSetIterator.nextInternal(ResultSetIterator.java:40)
at com.thinkaurelius.titan.graphdb.query.ResultSetIterator.<init>(ResultSetIterator.java:30)
at com.thinkaurelius.titan.graphdb.query.QueryProcessor.iterator(QueryProcessor.java:57)
at com.google.common.collect.Iterables$7.iterator(Iterables.java:613)
at com.google.common.collect.Iterables.isEmpty(Iterables.java:1054)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.addProperty(StandardTitanTx.java:779)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.addProperty(StandardTitanTx.java:706)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.makeSchemaVertex(StandardTitanTx.java:836)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.makePropertyKey(StandardTitanTx.java:856)
at com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker.make(StandardPropertyKeyMaker.java:86)
at com.run.tatin.tatindemo.TestTitan.load(TestTitan.java:50)
at com.run.tatin.tatindemo.TestTitan.load(TestTitan.java:43)
at com.run.tatin.tatindemo.TestTitan.main(TestTitan.java:35)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
如发生上面异常是因为titan-hbase-1.0.0.jar中将guava的StopWatch类重新进行了改造并且缺少了elapsedTime这个方法,但是又因为guava12.0.1本身StopWatch类中又缺少titan-hbase包中的一些成员方法.最终解决方案是将titan-hbase-core重新修改并且打包打包后的jar在请到下面下载